forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate message formats using serde_reflection
- Loading branch information
Showing
5 changed files
with
350 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Facebook Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use fastpay_core::{error, messages, serialize}; | ||
|
||
use serde_reflection::{Registry, Result, Samples, Tracer, TracerConfig}; | ||
use std::{fs::File, io::Write}; | ||
use structopt::{clap::arg_enum, StructOpt}; | ||
|
||
fn get_registry() -> Result<Registry> { | ||
let mut tracer = Tracer::new(TracerConfig::default()); | ||
let samples = Samples::new(); | ||
// 1. Record samples for types with custom deserializers. | ||
// tracer.trace_value(&mut samples, ...)?; | ||
|
||
// 2. Trace the main entry point(s) + every enum separately. | ||
tracer.trace_type::<messages::Address>(&samples)?; | ||
tracer.trace_type::<error::FastPayError>(&samples)?; | ||
tracer.trace_type::<serialize::SerializedMessage>(&samples)?; | ||
tracer.registry() | ||
} | ||
|
||
arg_enum! { | ||
#[derive(Debug, StructOpt, Clone, Copy)] | ||
enum Action { | ||
Print, | ||
Test, | ||
Record, | ||
} | ||
} | ||
|
||
#[derive(Debug, StructOpt)] | ||
#[structopt( | ||
name = "FastPay format generator", | ||
about = "Trace serde (de)serialization to generate format descriptions for FastPay types" | ||
)] | ||
struct Options { | ||
#[structopt(possible_values = &Action::variants(), default_value = "Print", case_insensitive = true)] | ||
action: Action, | ||
} | ||
|
||
const FILE_PATH: &str = "fastpay_core/tests/staged/fastpay.yaml"; | ||
|
||
fn main() { | ||
let options = Options::from_args(); | ||
let registry = get_registry().unwrap(); | ||
match options.action { | ||
Action::Print => { | ||
let content = serde_yaml::to_string(®istry).unwrap(); | ||
println!("{}", content); | ||
} | ||
Action::Record => { | ||
let content = serde_yaml::to_string(®istry).unwrap(); | ||
let mut f = File::create(FILE_PATH).unwrap(); | ||
writeln!(f, "{}", content).unwrap(); | ||
} | ||
Action::Test => { | ||
let f = File::open(FILE_PATH).unwrap(); | ||
let reference: serde_reflection::Registry = serde_yaml::from_reader(f).unwrap(); | ||
assert_eq!(registry, reference); | ||
} | ||
} | ||
} |
Oops, something went wrong.