1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::convert::TryInto;

/// Parse a vec of bytes as hex String
pub fn parse_hex(input: &Vec<u8>) -> String {
    let value: Vec<char> = hex::encode_upper(input).chars().collect();
    let split = value
        .chunks(2)
        .map(|chunk| chunk.iter().collect::<String>())
        .collect::<Vec<String>>();

    split.join(":")
}

/// Parse a vec of bytes as a String
pub fn parse_string(input: &Vec<u8>) -> String {
    String::from_utf8_lossy(&input).to_owned().to_string()
}

/// Parse a vec of bytes as u8
pub fn parse_u8(input: &Vec<u8>) -> u8 {
    let to_array =
        |slice: &[u8]| -> [u8; 1] { slice.try_into().expect("slice with incorrect length") };

    u8::from_le_bytes(to_array(input))
}

/// Parse a vec of bytes as i8
pub fn parse_i8(input: &Vec<u8>) -> i8 {
    let to_array =
        |slice: &[u8]| -> [u8; 1] { slice.try_into().expect("slice with incorrect length") };

    i8::from_le_bytes(to_array(input))
}

/// Parse a vec of bytes as u16
pub fn parse_u16(input: &Vec<u8>) -> u16 {
    let to_array =
        |slice: &[u8]| -> [u8; 2] { slice.try_into().expect("slice with incorrect length") };

    u16::from_le_bytes(to_array(input))
}

/// Parse a vec of bytes as u32
pub fn parse_u32(input: &Vec<u8>) -> u32 {
    let to_array =
        |slice: &[u8]| -> [u8; 4] { slice.try_into().expect("slice with incorrect length") };

    u32::from_le_bytes(to_array(input))
}

/// Parse a vec of bytes as i32
pub fn parse_i32(input: &Vec<u8>) -> i32 {
    let to_array =
        |slice: &[u8]| -> [u8; 4] { slice.try_into().expect("slice with incorrect length") };

    i32::from_le_bytes(to_array(input))
}

/// Parse a vec of bytes as u64
pub fn parse_u64(input: &Vec<u8>) -> u64 {
    let to_array =
        |slice: &[u8]| -> [u8; 8] { slice.try_into().expect("slice with incorrect length") };

    u64::from_le_bytes(to_array(input))
}

#[cfg(test)]
mod test_type_conversion {
    use super::*;

    #[test]
    fn test_parse_hex() {
        let bytes_input: Vec<u8> = vec![255, 255, 255, 255, 255, 255];
        assert_eq!(parse_hex(&bytes_input), "FF:FF:FF:FF:FF:FF".to_string());
    }

    #[test]
    fn test_parse_string() {
        let input_string = "test".to_string();
        let bytes_string = input_string.as_bytes().to_vec();
        assert_eq!(parse_string(&bytes_string), input_string);
    }

    #[test]
    fn test_parse_u8() {
        assert_eq!(parse_u8(&vec![8]), 8 as u8);
    }

    #[test]
    #[should_panic]
    fn test_parse_u8_should_panic() {
        assert_eq!(parse_u8(&vec![8, 0]), 8 as u8);
    }

    #[test]
    fn test_parse_i8() {
        assert_eq!(parse_i8(&vec![8]), 8 as i8);
    }

    #[test]
    #[should_panic]
    fn test_parse_i8_should_panic() {
        assert_eq!(parse_i8(&vec![8, 0]), 8 as i8);
    }

    #[test]
    fn test_parse_u16() {
        assert_eq!(parse_u16(&vec![1, 0]), 1 as u16);
    }

    #[test]
    #[should_panic]
    fn test_parse_u16_should_panic() {
        assert_eq!(parse_u16(&vec![1, 0, 0]), 1 as u16);
        assert_eq!(parse_u16(&vec![1]), 1 as u16);
    }

    #[test]
    fn test_parse_u32() {
        assert_eq!(parse_u32(&vec![1, 0, 0, 0]), 1 as u32);
    }

    #[test]
    #[should_panic]
    fn test_parse_u32_should_panic() {
        assert_eq!(parse_u32(&vec![1, 0, 0, 0, 0]), 1 as u32);
        assert_eq!(parse_u32(&vec![1, 0, 0]), 1 as u32);
    }

    #[test]
    fn test_parse_i32() {
        assert_eq!(parse_i32(&vec![1, 0, 0, 0]), 1 as i32);
    }

    #[test]
    #[should_panic]
    fn test_parse_i32_should_panic() {
        assert_eq!(parse_i32(&vec![1, 0, 0, 0, 0]), 1 as i32);
        assert_eq!(parse_i32(&vec![1, 0, 0]), 1 as i32);
    }

    #[test]
    fn test_parse_u64() {
        assert_eq!(parse_u64(&vec![1, 0, 0, 0, 0, 0, 0, 0]), 1 as u64);
    }

    #[test]
    #[should_panic]
    fn test_parse_u64_should_panic() {
        assert_eq!(parse_u64(&vec![1, 0, 0, 0, 0, 0, 0, 0, 0]), 1 as u64);
        assert_eq!(parse_u64(&vec![1, 0, 0]), 1 as u64);
    }
}