Added a string printer and updated README
ci/woodpecker/push/woodpecker Pipeline failed Details

+ Fixed a unit test as well
main
Joey Hines 2024-04-28 15:33:19 -06:00
parent 3274386ccf
commit 333a92b729
No known key found for this signature in database
GPG Key ID: 995E531F7A569DDB
5 changed files with 18 additions and 14 deletions

View File

@ -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)

View File

@ -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

View File

@ -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")
}
}

View File

@ -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<T: ByteOrderOperations>(&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 {

View File

@ -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()