Skip to content

Commit

Permalink
v0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
encody committed Dec 20, 2022
1 parent 37db793 commit 1f72fee
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = "Command-line utility for manipulating Borsh-serialized data"
edition = "2021"
license = "GPL-3.0"
name = "borsh-cli"
version = "0.1.2"
version = "0.1.3"

[dependencies]
anyhow = "1.0.66"
Expand Down
10 changes: 9 additions & 1 deletion src/command/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ pub struct DecodeArgs {

/// Write output to this file, otherwise to stdout.
pub output_path: Option<PathBuf>,

/// Format output
#[arg(short, long)]
pub pretty: bool,
}

pub struct Decode<'a> {
pub input: Vec<u8>,
pub output: Box<dyn Write + 'a>,
pub pretty: bool,
}

impl TryFrom<&'_ DecodeArgs> for Decode<'_> {
Expand All @@ -29,11 +34,13 @@ impl TryFrom<&'_ DecodeArgs> for Decode<'_> {
DecodeArgs {
input_path,
output_path,
pretty,
}: &'_ DecodeArgs,
) -> Result<Self, Self::Error> {
Ok(Self {
input: get_input_bytes(input_path.as_ref())?,
output: output_writer(output_path.as_ref())?,
pretty: *pretty,
})
}
}
Expand All @@ -48,7 +55,7 @@ impl Execute for Decode<'_> {
let value = crate::dynamic_schema::deserialize_from_schema(&mut buf, &schema)
.map_err(|_| IOError::DeserializeBorsh("data according to embedded schema"))?;

output_json(&mut self.output, &value)
output_json(&mut self.output, &value, self.pretty)
}
}

Expand Down Expand Up @@ -95,6 +102,7 @@ mod tests {
let mut p = Decode {
input: borsh::try_to_vec_with_schema(&value).unwrap(),
output: Box::new(writer),
pretty: false,
};

p.execute().unwrap();
Expand Down
32 changes: 22 additions & 10 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ pub enum Command {

impl Command {
pub fn run(&self) {
match self {
Command::Pack(args) => Pack::execute(&mut args.try_into().unwrap()).unwrap(),
Command::Unpack(args) => Unpack::execute(&mut args.try_into().unwrap()).unwrap(),
Command::Encode(args) => Encode::execute(&mut args.try_into().unwrap()).unwrap(),
Command::Decode(args) => Decode::execute(&mut args.try_into().unwrap()).unwrap(),
Command::Extract(args) => Extract::execute(&mut args.try_into().unwrap()).unwrap(),
Command::Strip(args) => Strip::execute(&mut args.try_into().unwrap()).unwrap(),
#[inline]
fn run_args<E: Execute>(args: impl TryInto<E, Error = IOError>) -> Result<(), IOError> {
E::execute(&mut args.try_into()?)
}

if let Err(e) = match self {
Command::Pack(args) => run_args::<Pack>(args),
Command::Unpack(args) => run_args::<Unpack>(args),
Command::Encode(args) => run_args::<Encode>(args),
Command::Decode(args) => run_args::<Decode>(args),
Command::Extract(args) => run_args::<Extract>(args),
Command::Strip(args) => run_args::<Strip>(args),
} {
eprintln!("Error: {e}");
}
}
}
Expand All @@ -63,7 +70,7 @@ pub enum IOError {
WriteBytes,
#[error("Failed to deserialize input as Borsh {0}")]
DeserializeBorsh(&'static str),
#[error("Failed to deserialize input as Json")]
#[error("Failed to deserialize input as JSON")]
DeserializeJson,
#[error("Unexpected schema header: {0}")]
IncorrectBorshSchemaHeader(String),
Expand Down Expand Up @@ -101,8 +108,12 @@ fn output_borsh(writer: impl Write, value: impl BorshSerialize) -> Result<(), IO
borsh::to_writer(writer, &value).map_err(|_| IOError::WriteBorsh)
}

fn output_json(writer: impl Write, value: &impl Serialize) -> Result<(), IOError> {
serde_json::to_writer(writer, value).map_err(|_| IOError::WriteJson)
fn output_json(writer: impl Write, value: &impl Serialize, pretty: bool) -> Result<(), IOError> {
if pretty {
serde_json::to_writer_pretty(writer, value).map_err(|_| IOError::WriteJson)
} else {
serde_json::to_writer(writer, value).map_err(|_| IOError::WriteJson)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -162,6 +173,7 @@ mod tests {
output_json(
&mut output_writer(Some(&"./dataonly.json".into())).unwrap(),
&v,
false,
// Some(&First::schema_container()),
);
output_borsh(
Expand Down

0 comments on commit 1f72fee

Please sign in to comment.