1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use std::convert::TryInto;
16
17 use log::error;
18
u8_to_bytes(value: u8) -> Vec<u8>19 pub fn u8_to_bytes(value: u8) -> Vec<u8> {
20 value.to_le_bytes().to_vec()
21 }
22
u16_to_bytes(value: u16) -> Vec<u8>23 pub fn u16_to_bytes(value: u16) -> Vec<u8> {
24 value.to_le_bytes().to_vec()
25 }
26
u32_to_bytes(value: u32) -> Vec<u8>27 pub fn u32_to_bytes(value: u32) -> Vec<u8> {
28 value.to_le_bytes().to_vec()
29 }
30
31 #[allow(dead_code)]
u64_to_bytes(value: u64) -> Vec<u8>32 pub fn u64_to_bytes(value: u64) -> Vec<u8> {
33 value.to_le_bytes().to_vec()
34 }
35
bytes_to_u8(value: Vec<u8>) -> Option<u8>36 pub fn bytes_to_u8(value: Vec<u8>) -> Option<u8> {
37 Some(u8::from_le_bytes(value.try_into().ok()?))
38 }
39
40 #[allow(dead_code)]
bytes_to_u16(value: Vec<u8>) -> Option<u16>41 pub fn bytes_to_u16(value: Vec<u8>) -> Option<u16> {
42 Some(u16::from_le_bytes(value.try_into().ok()?))
43 }
44
bytes_to_u32(value: Vec<u8>) -> Option<u32>45 pub fn bytes_to_u32(value: Vec<u8>) -> Option<u32> {
46 Some(u32::from_le_bytes(value.try_into().ok()?))
47 }
48
bytes_to_u64(value: Vec<u8>) -> Option<u64>49 pub fn bytes_to_u64(value: Vec<u8>) -> Option<u64> {
50 Some(u64::from_le_bytes(value.try_into().ok()?))
51 }
52
validate(value: bool, err_msg: &str) -> Option<()>53 pub fn validate(value: bool, err_msg: &str) -> Option<()> {
54 match value {
55 true => Some(()),
56 false => {
57 error!("{}", err_msg);
58 None
59 }
60 }
61 }
62
63 #[cfg(test)]
64 mod tests {
65 use super::*;
66
67 #[test]
test_convert_u8_bytes()68 fn test_convert_u8_bytes() {
69 let value: u8 = 0x57;
70 let arr = u8_to_bytes(value);
71
72 assert_eq!(arr, vec![0x57]);
73 assert_eq!(bytes_to_u8(arr), Some(value));
74 }
75
76 #[test]
test_convert_u16_bytes()77 fn test_convert_u16_bytes() {
78 let value: u16 = 0x1357;
79 let arr = u16_to_bytes(value);
80
81 assert_eq!(arr, vec![0x57, 0x13]);
82 assert_eq!(bytes_to_u16(arr), Some(value));
83 }
84
85 #[test]
test_convert_u32_bytes()86 fn test_convert_u32_bytes() {
87 let value: u32 = 0x12345678;
88 let arr = u32_to_bytes(value);
89
90 assert_eq!(arr, vec![0x78, 0x56, 0x34, 0x12]);
91 assert_eq!(bytes_to_u32(arr), Some(value));
92 }
93
94 #[test]
test_convert_u64_bytes()95 fn test_convert_u64_bytes() {
96 let value: u64 = 0x0123456789ABCDEF;
97 let arr = u64_to_bytes(value);
98
99 assert_eq!(arr, vec![0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01]);
100 assert_eq!(bytes_to_u64(arr), Some(value));
101 }
102 }
103