feat: allow raw input with stdin
cargo devel CI / cargo CI (push) Successful in 1m39s Details

This commit is contained in:
Christoph J. Scherr 2024-09-06 12:14:15 +02:00
parent c18d1c09e9
commit 24ad56c111
Signed by: cscherrNT
GPG Key ID: 8E2B45BC51A27EA7
1 changed files with 15 additions and 7 deletions

View File

@ -17,8 +17,18 @@
//! assert_eq!(Format::Base32.format_str(0x41414242, &options), "032sIFAUEQQ="); //! assert_eq!(Format::Base32.format_str(0x41414242, &options), "032sIFAUEQQ=");
//! assert_eq!(Format::Base64.format_str(0x41414242, &options), "0sQUFCQg=="); //! assert_eq!(Format::Base64.format_str(0x41414242, &options), "0sQUFCQg==");
//! // sometimes you might need the raw bytes instead of a String //! // sometimes you might need the raw bytes instead of a String
//! assert_eq!(Format::Raw.format(0x1337, &options), vec![0x13, 0x37]); //! assert_eq!(Format::Raw.format(0x1337, &options), vec![0x00, 0x13, 0x37]);
//! assert_eq!(Format::Hex.format(0x1337, &options), vec![48, 120, 49, 51, 51, 55]); //! assert_eq!(Format::Hex.format(0x1337, &options), vec![48, 120, 49, 51, 51, 55]);
//!
//! options.set_prefix(false);
//! options.set_padding(false);
//!
//! assert_eq!(Format::Hex.format_str(0x1337, &options), "1337");
//! assert_eq!(Format::Base32.format_str(0x41414242, &options), "IFAUEQQ=");
//! assert_eq!(Format::Base64.format_str(0x41414242, &options), "QUFCQg==");
//!
//! assert_eq!(Format::Raw.format(0x1337, &options), vec![0x13, 0x37]);
//! assert_eq!(Format::Hex.format(0x1337, &options), vec![49, 51, 51, 55]);
//! ``` //! ```
#![allow(dead_code)] #![allow(dead_code)]
@ -276,7 +286,7 @@ impl Format {
// apperently used nowhere, sometimes 0 is used as a prefix but I // apperently used nowhere, sometimes 0 is used as a prefix but I
// think this makes it more clear that this is decimal // think this makes it more clear that this is decimal
Format::Dec => b"0d".to_vec(), Format::Dec => b"0d".to_vec(),
Format::Raw => [].to_vec(), // TODO: find a better way to deal with this Format::Raw => [0x00].to_vec(),
// very common // very common
Format::Hex => b"0x".to_vec(), Format::Hex => b"0x".to_vec(),
// very common // very common
@ -300,6 +310,7 @@ impl Format {
let mut buf: Vec<u8> = Vec::new(); let mut buf: Vec<u8> = Vec::new();
if options.prefix() { if options.prefix() {
buf.append(&mut self.prefix()); buf.append(&mut self.prefix());
debug!("prefix the buffer: {buf:X?}");
} }
match self { match self {
Format::Hex => { Format::Hex => {
@ -335,10 +346,7 @@ impl Format {
.to_owned(), .to_owned(),
), ),
// Format::Raw => buf.append(&mut split::unsigned_to_vec(num)), // Format::Raw => buf.append(&mut split::unsigned_to_vec(num)),
Format::Raw => { Format::Raw => buf.append(&mut split::unsigned_to_vec(num)),
debug!("do the raw thing");
buf.append(&mut split::unsigned_to_vec(num))
}
} }
buf buf
} }
@ -460,7 +468,7 @@ where
Some(sr) => sr, Some(sr) => sr,
None => s, None => s,
}; };
todo!("reading raw not implemented") Ok(join::array_to_unsigned(s.as_bytes())?)
} else { } else {
let e = "could not determine the format of the value".to_string(); let e = "could not determine the format of the value".to_string();
Err(anyhow!(e)) Err(anyhow!(e))