Debian Codegraph @

src/hwdb/trie/value_entry.rs

raw symbols SCIP
1 use std::mem;
2
3 use crate::{hwdb, Error, Result};
4
5 /// Trie value entry in the hardware database.
6 ///
7 /// Array of value entries that directly follows the node record.
8 #[repr(C, packed(8))]
9 #[derive(Clone, Copy, Debug, Default, PartialEq)]
10 pub struct TrieValueEntry {
11 key_off: u64,
12 value_off: u64,
13 }
14
15 impl TrieValueEntry {
16 /// Creates a new [TrieValueEntry].
17 pub const fn new() -> Self {
18 Self {
19 key_off: 0,
20 value_off: 0,
21 }
22 }
23
24 /// Gets the length of the encoded [TrieValueEntry].
25 pub fn len(&self) -> usize {
26 hwdb::value_entry_size()
27 }
28
29 /// Gets whether the [TrieValueEntry] is empty.
30 pub const fn is_empty(&self) -> bool {
31 false
32 }
33
34 /// Gets key offset.
35 pub const fn key_off(&self) -> u64 {
36 self.key_off
37 }
38
39 /// Sets key offset.
40 pub fn set_key_off(&mut self, val: u64) {
41 self.key_off = val;
42 }
43
44 /// Builder function that sets the key offset.
45 pub fn with_key_off(mut self, val: u64) -> Self {
46 self.set_key_off(val);
47 self
48 }
49
50 /// Gets value offset.
51 pub const fn value_off(&self) -> u64 {
52 self.value_off
53 }
54
55 /// Sets value offset.
56 pub fn set_value_off(&mut self, val: u64) {
57 self.value_off = val;
58 }
59
60 /// Builder function that sets the value offset.
61 pub fn with_value_off(mut self, val: u64) -> Self {
62 self.set_value_off(val);
63 self
64 }
65 }
66
67 impl TryFrom<&[u8]> for TrieValueEntry {
68 type Error = Error;
69
70 fn try_from(val: &[u8]) -> Result<Self> {
71 if val.len() < hwdb::value_entry_size() {
72 Err(Error::InvalidLen(val.len()))
73 } else {
74 // TODO: parse use get ranges and offsets
75 let mut idx = 0usize;
76 let mut idx_end = mem::size_of::<u64>();
77
78 let key_off = u64::from_le_bytes(val[idx..idx_end].try_into()?);
79 idx += idx_end;
80 idx_end += idx_end;
81
82 let value_off = u64::from_le_bytes(val[idx..idx_end].try_into()?);
83
84 Ok(Self { key_off, value_off })
85 }
86 }
87 }