Skip to content

Commit

Permalink
Support encoding streams in the cli (#387)
Browse files Browse the repository at this point in the history
* Support encoding streams in the cli

* remove unnecessary clone

* upd hash

* add todos for other streaming types
  • Loading branch information
leighmcculloch authored Aug 29, 2024
1 parent 550743b commit 1746499
Show file tree
Hide file tree
Showing 4 changed files with 2,978 additions and 11 deletions.
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,
// 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

0 comments on commit 1746499

Please sign in to comment.