Skip to main content

source2_demo/string_table/
row.rs

1use std::rc::Rc;
2
3/// A row in a string table.
4///
5/// Each row contains an index, a key (string), and optional binary value data.
6///
7/// # Examples
8///
9/// ```no_run
10/// use source2_demo::prelude::*;
11///
12/// # fn example(table: &StringTable) {
13/// for row in table.iter() {
14///     println!("Key: {}", row.key());
15///     if let Some(value) = row.value() {
16///         println!("Value size: {} bytes", value.len());
17///     }
18/// }
19/// # }
20/// ```
21#[derive(Clone, Default)]
22pub struct StringTableRow {
23    pub(crate) index: i32,
24    pub(crate) key: String,
25    pub(crate) value: Option<Rc<Vec<u8>>>,
26}
27
28impl StringTableRow {
29    pub(crate) fn new(index: i32, key: String, value: Option<Rc<Vec<u8>>>) -> Self {
30        StringTableRow { index, key, value }
31    }
32
33    /// Returns the row's index in the string table.
34    pub fn index(&self) -> i32 {
35        self.index
36    }
37
38    /// Returns the row's key (string identifier).
39    pub fn key(&self) -> &str {
40        self.key.as_str()
41    }
42
43    /// Returns the row's value as a byte slice, if present.
44    ///
45    /// String table values are stored as binary data. The interpretation
46    /// depends on the specific string table and its purpose.
47    pub fn value(&self) -> Option<&[u8]> {
48        self.value.as_ref().map(|x| x.as_slice())
49    }
50}