str_to_bytes/
lib.rs

1use std::error::Error;
2
3pub mod formats;
4
5pub use formats::binary::{is_binary, parse_binary};
6pub use formats::decimal::{is_decimal, parse_decimal};
7pub use formats::hexadecimal::{is_hexadecimal, parse_hexadecimal};
8pub use formats::str::{is_str_ascii, parse_str_ascii};
9
10/// Parse a string that contains one or may of [hexadecimal, decimal, binary, ASCII()] into a Vec<u8>.
11///
12/// Examples:
13/// ```rust
14///     use str_to_bytes::str_to_bytes;
15///
16///     let my_str: &str = "12 0b11 0xff43 ASCII(HELLO)";
17///     let bytes = str_to_bytes(my_str);
18///     println!("{:?}", bytes);
19/// ```
20pub fn str_to_bytes(payload: &str) -> Result<Vec<u8>, Box<dyn Error>> {
21    let mut bytes: Vec<u8> = Vec::new();
22
23    let args = match shlex::split(payload) {
24        Some(a) => a,
25        None => Err("Invalid payload")?,
26    };
27
28    if args.is_empty() {
29        return Err("Empty payload")?;
30    }
31
32    for a in &args {
33        if is_binary(a) {
34            bytes = [bytes, parse_binary(a)].concat();
35        } else if is_hexadecimal(a) {
36            bytes = [bytes, parse_hexadecimal(a)].concat();
37        } else if is_decimal(a) {
38            bytes = [bytes, parse_decimal(a)].concat();
39        } else if is_str_ascii(a) {
40            bytes = [bytes, parse_str_ascii(a)].concat();
41        } else {
42            Err(format!("Invalid payload: {}", a))?;
43        }
44    }
45    Ok(bytes)
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_str_to_bytes() {
54        // binary
55        assert_eq!(str_to_bytes("0b10").unwrap(), [0b10]);
56        assert_eq!(str_to_bytes("0b1").unwrap(), [0b1]);
57        assert_eq!(str_to_bytes("0b01").unwrap(), [0b01]);
58        assert_eq!(str_to_bytes("0b00000001").unwrap(), [0b1]);
59        assert_eq!(str_to_bytes("0b11111111").unwrap(), [0xff]);
60        assert_eq!(str_to_bytes("0b000000001").unwrap(), [0, 1]);
61        assert_eq!(str_to_bytes("0b1111111100").unwrap(), [0xff, 0]);
62        assert_eq!(
63            str_to_bytes("0b111111110000000011111111").unwrap(),
64            [0xff, 0, 0xff]
65        );
66
67        // ascii
68        assert_eq!(str_to_bytes("ASCII(a)").unwrap(), [0x61]);
69        assert_eq!(str_to_bytes("ASCII(abc)").unwrap(), [0x61, 0x62, 0x63]);
70        assert_eq!(str_to_bytes("ASCII(0)").unwrap(), [0x30]);
71        assert_eq!(str_to_bytes("ASCII(1)").unwrap(), [0x31]);
72
73        // hexadecimal
74        assert_eq!(str_to_bytes("0x10").unwrap(), [0x10]);
75        assert_eq!(str_to_bytes("0x6").unwrap(), [0x6]);
76        assert_eq!(str_to_bytes("0xff").unwrap(), [0xff]);
77        assert_eq!(str_to_bytes("0x00").unwrap(), [0x00]);
78        assert_eq!(str_to_bytes("0xab").unwrap(), [0xab]);
79        assert_eq!(str_to_bytes("0x0f").unwrap(), [0x0f]);
80        assert_eq!(str_to_bytes("0xff00ff").unwrap(), [255, 0, 255]);
81        assert_eq!(str_to_bytes("0xabcdef").unwrap(), [0xab, 0xcd, 0xef]);
82        assert_eq!(
83            str_to_bytes("0xabcdef123456").unwrap(),
84            [0xab, 0xcd, 0xef, 0x12, 0x34, 0x56]
85        );
86
87        // decimal
88        assert_eq!(str_to_bytes("10").unwrap(), [10]);
89        assert_eq!(str_to_bytes("6").unwrap(), [6]);
90        assert_eq!(str_to_bytes("1").unwrap(), [1]);
91        assert_eq!(str_to_bytes("256").unwrap(), [1, 0]);
92        assert_eq!(str_to_bytes("1024").unwrap(), [0b100, 0]);
93        assert_eq!(
94            str_to_bytes("123456789").unwrap(),
95            [0b111, 0b01011011, 0b11001101, 0b00010101]
96        );
97
98        // everything
99        assert_eq!(
100            str_to_bytes("10 0b10 ASCII(a) 0xff").unwrap(),
101            [10, 0b10, 97, 255]
102        );
103    }
104}