Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support encoding streams in the cli #387

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 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=dda3c5ecea32847b7ab2333cfec602e892ae1478
XDRGEN_VERSION=b7bc57ecdd277c9575930d3e17c12dfaa76655fc
# XDRGEN_LOCAL=1
XDRGEN_TYPES_CUSTOM_STR_IMPL_CURR=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12,ClaimableBalanceId
XDRGEN_TYPES_CUSTOM_STR_IMPL_NEXT=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress,AssetCode,AssetCode4,AssetCode12,ClaimableBalanceId
Expand Down
37 changes: 29 additions & 8 deletions src/cli/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ impl Default for InputFormat {
pub enum OutputFormat {
Single,
SingleBase64,
Stream,
// TODO: StreamBase64,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to open issues for these?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but there is no pressing need for these things. So I think leaving the TODOs here as a way to signal if that feature is added here's the area to add it is okay. But I can also remove the TODOs too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// TODO: StreamFramed,
}

impl Default for OutputFormat {
Expand All @@ -114,15 +117,33 @@ macro_rules! run_x {
})?;
for f in &mut files {
match self.input {
InputFormat::Json => {
let t = crate::$m::Type::read_json(r#type, f)?;
let l = crate::$m::Limits::none();

match self.output {
OutputFormat::Single => stdout().write_all(&t.to_xdr(l)?)?,
OutputFormat::SingleBase64 => println!("{}", t.to_xdr_base64(l)?),
InputFormat::Json => match self.output {
OutputFormat::Single => {
let t = crate::$m::Type::from_json(r#type, f)?;
let l = crate::$m::Limits::none();
stdout().write_all(&t.to_xdr(l)?)?
}
OutputFormat::SingleBase64 => {
let t = crate::$m::Type::from_json(r#type, f)?;
let l = crate::$m::Limits::none();
println!("{}", t.to_xdr_base64(l)?)
}
OutputFormat::Stream => {
let mut de =
serde_json::Deserializer::new(serde_json::de::IoRead::new(f));
loop {
let t = match crate::$m::Type::deserialize_json(r#type, &mut de) {
Ok(t) => t,
Err(crate::$m::Error::Json(ref inner)) if inner.is_eof() => {
break;
}
Err(e) => Err(e)?,
};
let l = crate::$m::Limits::none();
stdout().write_all(&t.to_xdr(l)?)?
}
}
}
},
};
}
Ok(())
Expand Down
Loading
Loading