Skip to main content

sluice/domain/
flags.rs

1/// Bitfield describing how a field is stored/indexed in the index.
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3#[cfg_attr(feature = "serde", derive(serde::Serialize))]
4pub struct FieldFlags(u8);
5
6impl FieldFlags {
7    /// Bit set when the field is indexed (Lucene-searchable).
8    pub const INDEXED: u8 = 0x01;
9    /// Bit set when the field value is tokenized for indexing.
10    pub const TOKENIZED: u8 = 0x02;
11    /// Bit set when the field value is stored verbatim alongside the index.
12    pub const STORED: u8 = 0x04;
13
14    /// Construct from a raw flags byte.
15    #[must_use]
16    pub fn new(bits: u8) -> Self {
17        Self(bits)
18    }
19
20    /// Return the raw flags byte.
21    #[must_use]
22    pub fn bits(self) -> u8 {
23        self.0
24    }
25
26    /// `true` when [`Self::INDEXED`] is set.
27    #[must_use]
28    pub fn is_indexed(self) -> bool {
29        self.0 & Self::INDEXED != 0
30    }
31
32    /// `true` when [`Self::TOKENIZED`] is set.
33    #[must_use]
34    pub fn is_tokenized(self) -> bool {
35        self.0 & Self::TOKENIZED != 0
36    }
37
38    /// `true` when [`Self::STORED`] is set.
39    #[must_use]
40    pub fn is_stored(self) -> bool {
41        self.0 & Self::STORED != 0
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn empty_flags_have_no_bits() {
51        let f = FieldFlags::new(0);
52        assert!(!f.is_indexed());
53        assert!(!f.is_tokenized());
54        assert!(!f.is_stored());
55    }
56
57    #[test]
58    fn indexed_only() {
59        let f = FieldFlags::new(FieldFlags::INDEXED);
60        assert!(f.is_indexed());
61        assert!(!f.is_tokenized());
62        assert!(!f.is_stored());
63    }
64
65    #[test]
66    fn indexed_tokenized_stored() {
67        let f = FieldFlags::new(FieldFlags::INDEXED | FieldFlags::TOKENIZED | FieldFlags::STORED);
68        assert!(f.is_indexed());
69        assert!(f.is_tokenized());
70        assert!(f.is_stored());
71        assert_eq!(f.bits(), 0x07);
72    }
73
74    #[test]
75    fn stored_only() {
76        let f = FieldFlags::new(FieldFlags::STORED);
77        assert!(!f.is_indexed());
78        assert!(!f.is_tokenized());
79        assert!(f.is_stored());
80    }
81}