rekt_common/libs/
utils.rs1use std::collections::HashSet;
2use std::mem::size_of;
3
4use crate::libs::types::TopicId;
5
6pub fn get_bytes_from_slice(
23 buffer: &[u8],
24 from: usize,
25 to: usize,
26) -> Vec<u8> {
27 match () {
29 _ if to < from => panic!("from is greater than to"),
30 _ if to >= buffer.len() => panic!("to is greater than the last index"),
31 _ => (),
32 }
33
34 buffer[from..to + 1].into()
36}
37
38
39pub fn get_u64_at_pos(buffer: &[u8], position: usize) -> Result<u64, &str>
49{
50 let slice = get_bytes_from_slice(buffer, position, position + size_of::<u64>() - 1);
51 if slice.len() != 8 {
52 return Err("Slice len is invalid to convert it into an u64.");
53 }
54 Ok(u64::from_le_bytes(slice.try_into().unwrap()))
55}
56
57pub fn get_u32_at_pos(buffer: &[u8], position: usize) -> Result<u32, &str>
67{
68 let slice = get_bytes_from_slice(buffer, position, position + size_of::<u32>() - 1);
69 if slice.len() != 4 {
70 return Err("Slice len is invalid to convert it into an u32.");
71 }
72 Ok(u32::from_le_bytes(slice.try_into().unwrap()))
73}
74
75pub fn get_u16_at_pos(buffer: &[u8], position: usize) -> Result<u16, &str>
85{
86 let slice = get_bytes_from_slice(buffer, position, position + size_of::<u16>() - 1);
87 if slice.len() != 2 {
88 return Err("Slice len is invalid to convert it into an u16.");
89 }
90 Ok(u16::from_le_bytes(slice.try_into().unwrap()))
91}
92
93
94pub fn diff_hashsets(new_set: &HashSet<TopicId>, current_set: &HashSet<TopicId>) -> (Vec<TopicId>, Vec<TopicId>) {
104 let added_values = new_set.difference(current_set).cloned().collect();
105 let removed_values = current_set.difference(new_set).cloned().collect();
106 (added_values, removed_values)
107}
108
109pub fn vec_to_u8(bitfield: Vec<u8>) -> u8 {
118 if bitfield.len() != 8 {
119 return panic!("Bitfield length is invalid ! It must be exactly 8.");
120 }
121 (bitfield[0] << 7) | (bitfield[1] << 6) | (bitfield[2] << 5) | (bitfield[3] << 4) | (bitfield[4] << 3) | (bitfield[5] << 2) | (bitfield[6] << 1) | (bitfield[7] << 0)
122}
123
124pub fn u8_to_vec_be(number: u8) -> Vec<u8> {
133 let mut bits = Vec::with_capacity(8);
134 for i in 0..8 {
135 bits.push(if (number & (1 << i)) != 0 { 1 } else { 0 });
136 }
137 bits.reverse();
138 bits
139}