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
bytes_to_u16(value: Vec<u8>) -> Option<u16>40 pub fn bytes_to_u16(value: Vec<u8>) -> Option<u16> {
41 Some(u16::from_le_bytes(value.try_into().ok()?))
42 }
43
bytes_to_u32(value: Vec<u8>) -> Option<u32>44 pub fn bytes_to_u32(value: Vec<u8>) -> Option<u32> {
45 Some(u32::from_le_bytes(value.try_into().ok()?))
46 }
47
bytes_to_u64(value: Vec<u8>) -> Option<u64>48 pub fn bytes_to_u64(value: Vec<u8>) -> Option<u64> {
49 Some(u64::from_le_bytes(value.try_into().ok()?))
50 }
51
validate(value: bool, err_msg: &str) -> Option<()>52 pub fn validate(value: bool, err_msg: &str) -> Option<()> {
53 match value {
54 true => Some(()),
55 false => {
56 error!("{}", err_msg);
57 None
58 }
59 }
60 }
61
62 #[cfg(test)]
63 mod tests {
64 use super::*;
65
66 #[test]
test_convert_u8_bytes()67 fn test_convert_u8_bytes() {
68 let value: u8 = 0x57;
69 let arr = u8_to_bytes(value);
70
71 assert_eq!(arr, vec![0x57]);
72 assert_eq!(bytes_to_u8(arr), Some(value));
73 }
74
75 #[test]
test_convert_u16_bytes()76 fn test_convert_u16_bytes() {
77 let value: u16 = 0x1357;
78 let arr = u16_to_bytes(value);
79
80 assert_eq!(arr, vec![0x57, 0x13]);
81 assert_eq!(bytes_to_u16(arr), Some(value));
82 }
83
84 #[test]
test_convert_u32_bytes()85 fn test_convert_u32_bytes() {
86 let value: u32 = 0x12345678;
87 let arr = u32_to_bytes(value);
88
89 assert_eq!(arr, vec![0x78, 0x56, 0x34, 0x12]);
90 assert_eq!(bytes_to_u32(arr), Some(value));
91 }
92
93 #[test]
test_convert_u64_bytes()94 fn test_convert_u64_bytes() {
95 let value: u64 = 0x0123456789ABCDEF;
96 let arr = u64_to_bytes(value);
97
98 assert_eq!(arr, vec![0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01]);
99 assert_eq!(bytes_to_u64(arr), Some(value));
100 }
101 }
102