1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::ops::BitOr;

use serde::{Deserialize, Serialize};

use crate::schema::flags::{IndexedFlag, SchemaFlagList, StoredFlag};

/// Define how a facet field should be handled by tantivy.
///
/// Note that a Facet is always indexed and stored as a fastfield.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct FacetOptions {
    stored: bool,
}

impl FacetOptions {
    /// Returns true if the value is stored.
    #[inline]
    pub fn is_stored(&self) -> bool {
        self.stored
    }

    /// Set the field as stored.
    ///
    /// Only the fields that are set as *stored* are
    /// persisted into the Tantivy's store.
    #[must_use]
    pub fn set_stored(mut self) -> FacetOptions {
        self.stored = true;
        self
    }
}

impl From<()> for FacetOptions {
    fn from(_: ()) -> FacetOptions {
        FacetOptions::default()
    }
}

impl From<StoredFlag> for FacetOptions {
    fn from(_: StoredFlag) -> Self {
        FacetOptions { stored: true }
    }
}

impl<T: Into<FacetOptions>> BitOr<T> for FacetOptions {
    type Output = FacetOptions;

    fn bitor(self, other: T) -> FacetOptions {
        let other = other.into();
        FacetOptions {
            stored: self.stored | other.stored,
        }
    }
}

impl<Head, Tail> From<SchemaFlagList<Head, Tail>> for FacetOptions
where
    Head: Clone,
    Tail: Clone,
    Self: BitOr<Output = Self> + From<Head> + From<Tail>,
{
    fn from(head_tail: SchemaFlagList<Head, Tail>) -> Self {
        Self::from(head_tail.head) | Self::from(head_tail.tail)
    }
}

impl From<IndexedFlag> for FacetOptions {
    fn from(_: IndexedFlag) -> Self {
        FacetOptions { stored: false }
    }
}

#[cfg(test)]
mod tests {
    use crate::schema::{FacetOptions, INDEXED};

    #[test]
    fn test_from_index_flag() {
        let facet_option = FacetOptions::from(INDEXED);
        assert_eq!(facet_option, FacetOptions::default());
    }
}