-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add schemars::JsonSchema derives to all types (#347)
* Add schemars::JsonSchema derives to all types * add test * remove unused dev dep * silence clippy warning * regen with new feature and manual impls from willem * upd * fix * upd * fix overflow * add first version of command * ping to draft7 * upd * group new features * custom types * upd * upd ver * update xdr * upd * upd * fix claimable balance id
- Loading branch information
1 parent
9209690
commit 89a8bcd
Showing
15 changed files
with
2,729 additions
and
14 deletions.
There are no files selected for viewing
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
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
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,68 @@ | ||
use clap::{Args, ValueEnum}; | ||
use schemars::gen::SchemaSettings; | ||
|
||
use crate::cli::Channel; | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error("unknown type {0}, choose one of {1:?}")] | ||
UnknownType(String, &'static [&'static str]), | ||
#[error("error generating JSON: {0}")] | ||
GenerateJson(#[from] serde_json::Error), | ||
} | ||
|
||
#[derive(Args, Debug, Clone)] | ||
#[command()] | ||
pub struct Cmd { | ||
/// XDR type to decode | ||
#[arg(long)] | ||
r#type: String, | ||
|
||
// Output format | ||
#[arg(long, value_enum, default_value_t)] | ||
output: OutputFormat, | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)] | ||
pub enum OutputFormat { | ||
JsonSchemaDraft7, | ||
JsonSchemaDraft201909, | ||
} | ||
|
||
impl Default for OutputFormat { | ||
fn default() -> Self { | ||
Self::JsonSchemaDraft201909 | ||
} | ||
} | ||
|
||
macro_rules! run_x { | ||
($f:ident, $m:ident) => { | ||
fn $f(&self) -> Result<(), Error> { | ||
use std::str::FromStr; | ||
let r#type = crate::$m::TypeVariant::from_str(&self.r#type).map_err(|_| { | ||
Error::UnknownType(self.r#type.clone(), &crate::$m::TypeVariant::VARIANTS_STR) | ||
})?; | ||
let settings = match self.output { | ||
OutputFormat::JsonSchemaDraft7 => SchemaSettings::draft07(), | ||
OutputFormat::JsonSchemaDraft201909 => SchemaSettings::draft2019_09(), | ||
}; | ||
let generator = settings.into_generator(); | ||
let schema = r#type.json_schema(generator); | ||
println!("{}", serde_json::to_string_pretty(&schema)?); | ||
Ok(()) | ||
} | ||
}; | ||
} | ||
|
||
impl Cmd { | ||
pub fn run(&self, channel: &Channel) -> Result<(), Error> { | ||
match channel { | ||
Channel::Curr => self.run_curr()?, | ||
Channel::Next => self.run_next()?, | ||
} | ||
Ok(()) | ||
} | ||
|
||
run_x!(run_curr, curr); | ||
run_x!(run_next, next); | ||
} |
Oops, something went wrong.