From 333a92b729daf85c5c34c0a856ae816cbefcaf8a Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 28 Apr 2024 15:33:19 -0600 Subject: [PATCH] Added a string printer and updated README + Fixed a unit test as well --- README.md | 6 ++++++ formats/example.toml | 1 + src/formatter/format.rs | 2 +- src/formatter/printers.rs | 20 +++++++++----------- src/parser/mod.rs | 3 +-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 11de4bf..b0d632f 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,12 @@ Members: * `Format`: Allows formats to include other formats * `format_name`: Format definition to use +#### Print Types +* `Base`: print out the field as of a number of `base` N +* `ByteArray`: display the field as an array of bytes +* `String`: view fields as a string field +* `Default`: use the default printer for the source type + #### Example Config [Example](./formats/example.toml) diff --git a/formats/example.toml b/formats/example.toml index 9512433..fb90bcb 100644 --- a/formats/example.toml +++ b/formats/example.toml @@ -48,6 +48,7 @@ field_type = {type = "String", endianness = "BigEndian", max_len = 55} # byte field [[formats.fields]] name = "bytes field" +print_type = {print = "String"} field_type = {type = "Bytes", endianness = "BigEndian", max_len = 5} # Example of layer formats diff --git a/src/formatter/format.rs b/src/formatter/format.rs index 04452b4..06ed90e 100644 --- a/src/formatter/format.rs +++ b/src/formatter/format.rs @@ -745,6 +745,6 @@ mod tests { let (output, width) = field.format_data(&mut byte_stream, 0, &config).unwrap(); assert_eq!(width, 40); - assert_eq!(output, "test: Test\n") + assert_eq!(output, "\ntest: Test\n") } } diff --git a/src/formatter/printers.rs b/src/formatter/printers.rs index b0b7350..53d473f 100644 --- a/src/formatter/printers.rs +++ b/src/formatter/printers.rs @@ -18,20 +18,15 @@ pub fn base_notation(b: u32) -> String { } } -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Deserialize, Clone, Default)] #[serde(tag = "print")] pub enum PrintType { Base { base: u32 }, ByteArray, + String, + #[default] Default, } - -impl Default for PrintType { - fn default() -> Self { - PrintType::Default - } -} - impl PrintType { pub fn print_big_int(&self, big_int: BigInt) -> String { match self { @@ -39,7 +34,7 @@ impl PrintType { format!("{}{}", base_notation(*b), big_int.to_str_radix(*b)) } PrintType::ByteArray => print_bytes_as_array(&T::big_int_to_bytes(big_int)), - PrintType::Default => big_int.to_string(), + _ => big_int.to_string(), } } @@ -49,7 +44,7 @@ impl PrintType { format!("{}{}", base_notation(*b), big_int.to_str_radix(*b)) } PrintType::ByteArray => print_bytes_as_array(&T::big_u_int_to_bytes(big_int)), - PrintType::Default => big_int.to_string(), + _ => big_int.to_string(), } } @@ -76,7 +71,10 @@ impl PrintType { } pub fn print_bytes(&self, bytes: &[u8]) -> String { - print_bytes_as_array(bytes) + match self { + PrintType::String => std::str::from_utf8(bytes).unwrap().to_string(), + _ => print_bytes_as_array(bytes) + } } pub fn print_string(&self, s: &str) -> String { diff --git a/src/parser/mod.rs b/src/parser/mod.rs index fd41f6d..614583b 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -72,8 +72,7 @@ impl InputTypes { let str_arr = if src.len() == 1 { src[0] .replace(',', " ") - .replace('[', "") - .replace(']', "") + .replace(['[', ']'], "") .split_whitespace() .map(|s| s.to_string()) .collect()