| 1 | use std::mem; |
| 2 | |
| 3 | use crate::{hwdb, Error, Result}; |
| 4 | |
| 5 | /// Trie node in the hardware database. |
| 6 | #[repr(C, packed(8))] |
| 7 | #[derive(Clone, Copy, Debug, Default, PartialEq)] |
| 8 | pub struct TrieNode { |
| 9 | prefix_off: u64, |
| 10 | children_count: u8, |
| 11 | _padding: [u8; 7], |
| 12 | values_count: u64, |
| 13 | } |
| 14 | |
| 15 | impl TrieNode { |
| 16 | /// Creates a new [TrieNode]. |
| 17 | pub const fn new() -> Self { |
| 18 | Self { |
| 19 | prefix_off: 0, |
| 20 | children_count: 0, |
| 21 | _padding: [0u8; 7], |
| 22 | values_count: 0, |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | /// Gets the length of the encoded [TrieNode]. |
| 27 | pub fn len(&self) -> usize { |
| 28 | hwdb::node_size() |
| 29 | } |
| 30 | |
| 31 | /// Gets whether the [TrieNode] is empty. |
| 32 | pub const fn is_empty(&self) -> bool { |
| 33 | self.children_count == 0 && self.values_count == 0 |
| 34 | } |
| 35 | |
| 36 | /// Gets the prefix of the lookup string, shared by all children. |
| 37 | pub const fn prefix_off(&self) -> u64 { |
| 38 | self.prefix_off |
| 39 | } |
| 40 | |
| 41 | /// Sets the prefix of the lookup string, shared by all children. |
| 42 | pub fn set_prefix_off(&mut self, val: u64) { |
| 43 | self.prefix_off = val; |
| 44 | } |
| 45 | |
| 46 | /// Builder function that sets the prefix of the lookup string, shared by all children. |
| 47 | pub fn with_prefix_off(mut self, val: u64) -> Self { |
| 48 | self.set_prefix_off(val); |
| 49 | self |
| 50 | } |
| 51 | |
| 52 | /// Gets the size of children entry array appended to the node. |
| 53 | pub const fn children_count(&self) -> u8 { |
| 54 | self.children_count |
| 55 | } |
| 56 | |
| 57 | /// Sets the size of children entry array appended to the node. |
| 58 | pub fn set_children_count(&mut self, val: u8) { |
| 59 | self.children_count = val; |
| 60 | } |
| 61 | |
| 62 | /// Builder function that sets the size of children entry array appended to the node. |
| 63 | pub fn with_children_count(mut self, val: u8) -> Self { |
| 64 | self.set_children_count(val); |
| 65 | self |
| 66 | } |
| 67 | |
| 68 | /// Gets the size of value entry array appended to the node. |
| 69 | pub const fn values_count(&self) -> u64 { |
| 70 | self.values_count |
| 71 | } |
| 72 | |
| 73 | /// Sets the size of value entry array appended to the node. |
| 74 | pub fn set_values_count(&mut self, val: u64) { |
| 75 | self.values_count = val; |
| 76 | } |
| 77 | |
| 78 | /// Builder function that sets the size of value entry array appended to the node. |
| 79 | pub fn with_values_count(mut self, val: u64) -> Self { |
| 80 | self.set_values_count(val); |
| 81 | self |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl TryFrom<&[u8]> for TrieNode { |
| 86 | type Error = Error; |
| 87 | |
| 88 | fn try_from(val: &[u8]) -> Result<Self> { |
| 89 | if val.len() < mem::size_of::<Self>() { |
| 90 | Err(Error::InvalidLen(val.len())) |
| 91 | } else { |
| 92 | let mut idx = 0usize; |
| 93 | |
| 94 | let prefix_off = u64::from_le_bytes(val[idx..idx + 8].try_into()?); |
| 95 | idx += mem::size_of::<u64>(); |
| 96 | |
| 97 | let children_count = val[idx]; |
| 98 | let _padding = [0u8; 7]; |
| 99 | |
| 100 | // skip past the children count + padding |
| 101 | idx += 8; |
| 102 | |
| 103 | let values_count = u64::from_le_bytes( |
| 104 | val.get(idx..idx + 8) |
| 105 | .ok_or(Error::InvalidLen(val.len()))? |
| 106 | .try_into()?, |
| 107 | ); |
| 108 | |
| 109 | if values_count > 64 { |
| 110 | Err(Error::InvalidLen(values_count as usize)) |
| 111 | } else { |
| 112 | Ok(Self { |
| 113 | prefix_off, |
| 114 | children_count, |
| 115 | _padding, |
| 116 | values_count, |
| 117 | }) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |