Skip to content

Commit

Permalink
Changes binary output to Ion blob
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt committed Jul 16, 2024
1 parent 6d6db00 commit e5fb7e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

40 changes: 24 additions & 16 deletions src/bin/ion/commands/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ impl IonCliCommand for HashCommand {
// and an ArgGroup to ensure only one is selected.
// Default right now is to emit base16 strings of the digest.
.arg(
Arg::new("raw")
.long("raw")
.help("Emit the digest(s) as raw bytes.")
Arg::new("blob")
.long("blob")
.help("Emit the digest(s) as Ion blob values.")
.action(ArgAction::SetTrue),
)
}
Expand All @@ -91,20 +91,28 @@ impl IonCliCommand for HashCommand {
CommandIo::new(args).for_each_input(|output, input| {
let mut reader = Reader::new(AnyEncoding, input.into_source())?;

for elem in reader.elements() {
let elem = elem?;
if let Some(hash) = args.get_one::<DigestType>("hash") {
let digest = hash.hash_it(&elem)?;
if args.get_flag("raw") {
output.write_all(&digest)?;
} else {
let digest_string: String =
digest.iter().map(|b| format!("{:02x?}", b)).collect();
output.write_all(digest_string.as_bytes())?;
};
let hasher = if let Some(hasher) = args.get_one::<DigestType>("hash") {
hasher
} else {
unreachable!("clap ensures that there is a valid argument")
};

if args.get_flag("blob") {
let mut writer = Writer::new(v1_0::Text.with_format(TextFormat::Lines), output)?;
for elem in reader.elements() {
let elem = elem?;
let digest = hasher.hash_it(&elem)?;
writer.write_blob(&digest)?;
}
writer.close()?;
} else {
for elem in reader.elements() {
let elem = elem?;
let digest = hasher.hash_it(&elem)?;
let digest_string: String =
digest.iter().map(|b| format!("{:02x?}", b)).collect();
output.write_all(digest_string.as_bytes())?;
output.write_all("\n".as_bytes())?;
} else {
unreachable!("clap ensures that there is a valid argument")
}
}
Ok(())
Expand Down

0 comments on commit e5fb7e7

Please sign in to comment.