Skip to content

Commit

Permalink
add first version of command
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Mar 13, 2024
1 parent f576c2a commit 7575d7f
Show file tree
Hide file tree
Showing 8 changed files with 1,786 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ arbitrary = ["std", "dep:arbitrary"]
hex = []

# Features for the CLI.
cli = ["std", "curr", "next", "base64", "serde", "serde_json", "dep:clap", "dep:thiserror"]
cli = ["std", "curr", "next", "base64", "serde", "serde_json", "schemars", "dep:clap", "dep:thiserror"]

[package.metadata.docs.rs]
all-features = true
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CARGO_HACK_ARGS=--feature-powerset --exclude-features default --group-features b

CARGO_DOC_ARGS?=--open

XDRGEN_VERSION=3f37191c1b26b39a4a8265a3824df6c3236cb120
XDRGEN_VERSION=87d633c780260e2a30a28d7dc9571a790f5138fa
# XDRGEN_LOCAL=1
XDRGEN_TYPES_CUSTOM_STR_IMPL_CURR=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12
XDRGEN_TYPES_CUSTOM_STR_IMPL_NEXT=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12
Expand All @@ -23,7 +23,7 @@ doc:
RUSTDOCFLAGS="--cfg docs" cargo +nightly doc --package stellar-xdr --all-features $(CARGO_DOC_ARGS)

install:
cargo install --path . --force --features cli
cargo install --locked --path . --force --features cli

readme:
cargo readme > README.md
Expand Down
5 changes: 4 additions & 1 deletion src/bin/stellar-xdr/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ fn main() {
if let Err(e) = cli::run(env::args_os()) {
match e {
cli::Error::Clap(e) => e.exit(),
cli::Error::Guess(_) | cli::Error::Decode(_) | cli::Error::Encode(_) => {
cli::Error::Types(_)
| cli::Error::Guess(_)
| cli::Error::Decode(_)
| cli::Error::Encode(_) => {
Error::raw(clap::error::ErrorKind::ValueValidation, e).exit()
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Root {
/// If the root command is configured with state that is invalid.
pub fn run(&self) -> Result<(), Error> {
match &self.cmd {
Cmd::Types(c) => c.run(&self.channel),
Cmd::Types(c) => c.run(&self.channel)?,
Cmd::Guess(c) => c.run(&self.channel)?,
Cmd::Decode(c) => c.run(&self.channel)?,
Cmd::Encode(c) => c.run(&self.channel)?,
Expand Down Expand Up @@ -77,6 +77,8 @@ pub enum Cmd {
pub enum Error {
#[error("{0}")]
Clap(#[from] clap::Error),
#[error("{0}")]
Types(#[from] types::Error),
#[error("error decoding XDR: {0}")]
Guess(#[from] guess::Error),
#[error("error reading file: {0}")]
Expand Down
12 changes: 11 additions & 1 deletion src/cli/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
mod list;
mod schema;

use clap::{Args, Subcommand};

use crate::cli::Channel;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("{0}")]
SchemaError(#[from] schema::Error),
}

#[derive(Args, Debug, Clone)]
#[command()]
pub struct Cmd {
Expand All @@ -14,12 +21,15 @@ pub struct Cmd {
#[derive(Subcommand, Clone, Debug)]
pub enum Sub {
List(list::Cmd),
Schema(schema::Cmd),
}

impl Cmd {
pub fn run(&self, channel: &Channel) {
pub fn run(&self, channel: &Channel) -> Result<(), Error> {
match &self.sub {
Sub::List(c) => c.run(channel),
Sub::Schema(c) => c.run(channel)?,
}
Ok(())
}
}
73 changes: 73 additions & 0 deletions src/cli/types/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use clap::{Args, ValueEnum};

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,
}

impl Default for OutputFormat {
fn default() -> Self {
Self::JsonSchemaDraft7
}
}

macro_rules! run_x_case_for_type {
($type:tt, $m:tt, $type_name:expr, $output:expr) => {
if $type_name == stringify!($type) {
match $output {
OutputFormat::JsonSchemaDraft7 => {
let schema = schemars::schema_for!(crate::$m::$type);
println!("{}", serde_json::to_string_pretty(&schema)?);
},
}
}
};
}

macro_rules! run_x {
($f:ident, $m:ident) => {
fn $f(&self) -> Result<(), Error> {
use std::str::FromStr;
let _ = crate::$m::TypeVariant::from_str(&self.r#type).map_err(|_| {
Error::UnknownType(self.r#type.clone(), &crate::$m::TypeVariant::VARIANTS_STR)
})?;
crate::$m::call_macro_with_each_type! { run_x_case_for_type, $m, {&self.r#type}, {self.output} }
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);
}
Loading

0 comments on commit 7575d7f

Please sign in to comment.