Skip to content

Commit

Permalink
Add decode to Message Pack (compact variant)
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Sep 13, 2024
1 parent b551684 commit cc33fac
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
65 changes: 50 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ clap = { version = "4.2.4", default-features = false, features = ["std", "derive
serde_json = { version = "1.0.89", optional = true }
thiserror = { version = "1.0.37", optional = true }
schemars = { version = "0.8.16", optional = true }
rmp-serde = { version = "1.3.0", optional = true }

[dev-dependencies]
serde_json = "1.0.89"
Expand All @@ -50,7 +51,7 @@ arbitrary = ["std", "dep:arbitrary"]
hex = []

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

[package.metadata.docs.rs]
all-features = true
Expand Down
7 changes: 7 additions & 0 deletions src/cli/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum Error {
ReadFile(#[from] std::io::Error),
#[error("error generating JSON: {0}")]
GenerateJson(#[from] serde_json::Error),
#[error("error generating message pack: {0}")]
GenerateMessagePack(#[from] rmp_serde::encode::Error),
}

#[derive(Args, Debug, Clone)]
Expand Down Expand Up @@ -66,6 +68,7 @@ pub enum OutputFormat {
JsonFormatted,
RustDebug,
RustDebugFormatted,
MessagePack,
}

impl Default for OutputFormat {
Expand Down Expand Up @@ -158,6 +161,10 @@ impl Cmd {
OutputFormat::JsonFormatted => println!("{}", serde_json::to_string_pretty(v)?),
OutputFormat::RustDebug => println!("{v:?}"),
OutputFormat::RustDebugFormatted => println!("{v:#?}"),
OutputFormat::MessagePack => {
let mut out = std::io::stdout();
rmp_serde::encode::write(&mut out, v).map_err(Error::GenerateMessagePack)?;
}
}
Ok(())
}
Expand Down

0 comments on commit cc33fac

Please sign in to comment.