Fixed issue with array parse in CLI
ci/woodpecker/push/woodpecker Pipeline was successful Details
ci/woodpecker/tag/woodpecker Pipeline was successful Details

+ octals were not ignoring base 10 zeros
+ binary parsing was trying to parse base 1
+ clippy + fmt
main
Joey Hines 2022-04-11 21:18:56 -06:00
parent 70a3c260e6
commit 415a9c6ee1
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
1 changed files with 77 additions and 5 deletions

View File

@ -30,13 +30,13 @@ fn bytes_from_str_array(src: Vec<String>) -> Result<Vec<u8>, ByteArrayParseErr>
if element.starts_with("0x") || element.starts_with("0X") {
u8::from_str_radix(&element[2..], 16)
} else if element.starts_with("0b") || element.starts_with("0B") {
u8::from_str_radix(&element[2..], 1)
} else if element.starts_with('h') || element.starts_with('h') {
u8::from_str_radix(&element[2..], 2)
} else if element.starts_with('h') || element.starts_with('H') {
u8::from_str_radix(&element[1..], 16)
} else if let Some(value) = element.strip_prefix('0') {
u8::from_str_radix(value, 8)
} else if element.len() > 1 && element.starts_with('0') {
u8::from_str_radix(&element[1..], 8)
} else {
str::parse(element)
element.parse()
}
})
.map(|e| e.map_err(ByteArrayParseErr::from))
@ -62,3 +62,75 @@ pub fn parse_bytes_from_input_arg(src: Vec<String>) -> Result<Vec<u8>, ByteArray
bytes_from_str_array(str_arr)
}
#[cfg(test)]
mod tests {
use crate::parser::parse_bytes_from_input_arg;
#[test]
fn parse_base_10_array() {
let array = vec!["[0, 1, 2, 3, 4]".to_string()];
let out = parse_bytes_from_input_arg(array).unwrap();
assert_eq!(out, vec![0, 1, 2, 3, 4])
}
#[test]
fn parse_base_2_array() {
let array = vec!["[0b0, 0b1, 0b10, 0b11, 0b100]".to_string()];
let out = parse_bytes_from_input_arg(array).unwrap();
assert_eq!(out, vec![0, 1, 2, 3, 4])
}
#[test]
fn parse_base_8_array() {
let array = vec!["[00, 01, 02, 03, 04]".to_string()];
let out = parse_bytes_from_input_arg(array).unwrap();
assert_eq!(out, vec![0, 1, 2, 3, 4])
}
#[test]
fn parse_base_16_array() {
let array = vec!["[0x0, 0x1, 0x2, h3, H4]".to_string()];
let out = parse_bytes_from_input_arg(array).unwrap();
assert_eq!(out, vec![0, 1, 2, 3, 4])
}
#[test]
fn parse_base_mixed() {
let array = vec!["[00 0x1, 2, h3, H4]".to_string()];
let out = parse_bytes_from_input_arg(array).unwrap();
assert_eq!(out, vec![0, 1, 2, 3, 4])
}
#[test]
fn parse_no_braces_multi_args() {
let array = vec![
"0".to_string(),
"1".to_string(),
"2".to_string(),
"3".to_string(),
"4".to_string(),
];
let out = parse_bytes_from_input_arg(array).unwrap();
assert_eq!(out, vec![0, 1, 2, 3, 4])
}
#[test]
fn parse_no_braces_one_arg() {
let array = vec!["0 1 2 3 4".to_string()];
let out = parse_bytes_from_input_arg(array).unwrap();
assert_eq!(out, vec![0, 1, 2, 3, 4])
}
}