Skip to main content

lxdb_format/
section.rs

1/// Identifies a section inside an LXDB binary file.
2#[repr(u8)]
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum Section {
5    Metadata = 1,
6    Tokens = 2,
7    TokenStringTable = 3,
8    Relations = 4,
9    Adjacency = 5,
10}
11
12impl Section {
13    pub const fn as_u8(self) -> u8 {
14        self as u8
15    }
16
17    pub const fn from_u8(value: u8) -> Option<Self> {
18        match value {
19            1 => Some(Self::Metadata),
20            2 => Some(Self::Tokens),
21            3 => Some(Self::TokenStringTable),
22            4 => Some(Self::Relations),
23            5 => Some(Self::Adjacency),
24            _ => None,
25        }
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::Section;
32
33    #[test]
34    fn converts_known_section_from_byte() {
35        assert_eq!(Section::from_u8(Section::Tokens.as_u8()), Some(Section::Tokens),);
36
37        assert_eq!(Section::from_u8(Section::Adjacency.as_u8()), Some(Section::Adjacency),);
38    }
39
40    #[test]
41    fn rejects_unknown_section_byte() {
42        assert_eq!(Section::from_u8(255), None);
43    }
44}