flams_backend_types/
search.rs

1#![allow(clippy::wildcard_imports)]
2
3use ftml_ontology::narrative::elements::paragraphs::ParagraphKind;
4use ftml_uris::{DocumentElementUri, DocumentUri, SymbolUri};
5
6#[allow(dead_code)]
7const fn get_true() -> bool {
8    true
9}
10
11#[allow(clippy::struct_excessive_bools)]
12#[derive(Copy, Clone, Debug, PartialEq, Eq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14#[cfg_attr(feature = "typescript", derive(tsify::Tsify))]
15#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
16pub struct QueryFilter {
17    #[cfg_attr(feature = "serde", serde(default = "get_true"))]
18    pub allow_documents: bool,
19    #[cfg_attr(feature = "serde", serde(default = "get_true"))]
20    pub allow_paragraphs: bool,
21    #[cfg_attr(feature = "serde", serde(default = "get_true"))]
22    pub allow_definitions: bool,
23    #[cfg_attr(feature = "serde", serde(default = "get_true"))]
24    pub allow_examples: bool,
25    #[cfg_attr(feature = "serde", serde(default = "get_true"))]
26    pub allow_assertions: bool,
27    #[cfg_attr(feature = "serde", serde(default = "get_true"))]
28    pub allow_problems: bool,
29    #[cfg_attr(feature = "serde", serde(default))]
30    pub definition_like_only: bool,
31}
32
33impl Default for QueryFilter {
34    fn default() -> Self {
35        Self {
36            allow_documents: true,
37            allow_paragraphs: true,
38            allow_definitions: true,
39            allow_examples: true,
40            allow_assertions: true,
41            allow_problems: true,
42            definition_like_only: false,
43        }
44    }
45}
46
47#[derive(Debug, Clone)]
48#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
49#[cfg_attr(feature = "typescript", derive(tsify::Tsify))]
50#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
51pub enum SearchResult {
52    Document(DocumentUri),
53    Paragraph {
54        uri: DocumentElementUri,
55        fors: Vec<SymbolUri>,
56        def_like: bool,
57        kind: SearchResultKind,
58    },
59}
60
61#[derive(Copy, Clone, Debug)]
62#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
63#[cfg_attr(feature = "typescript", derive(tsify::Tsify))]
64#[cfg_attr(feature = "typescript", tsify(into_wasm_abi, from_wasm_abi))]
65pub enum SearchResultKind {
66    Document = 0,
67    Paragraph = 1,
68    Definition = 2,
69    Example = 3,
70    Assertion = 4,
71    Problem = 5,
72}
73impl SearchResultKind {
74    #[must_use]
75    pub const fn as_str(&self) -> &'static str {
76        match self {
77            Self::Document => "Document",
78            Self::Paragraph => "Paragraph",
79            Self::Definition => "Definition",
80            Self::Example => "Example",
81            Self::Assertion => "Assertion",
82            Self::Problem => "Problem",
83        }
84    }
85}
86
87impl From<SearchResultKind> for u64 {
88    fn from(value: SearchResultKind) -> Self {
89        match value {
90            SearchResultKind::Document => 0,
91            SearchResultKind::Paragraph => 1,
92            SearchResultKind::Definition => 2,
93            SearchResultKind::Example => 3,
94            SearchResultKind::Assertion => 4,
95            SearchResultKind::Problem => 5,
96        }
97    }
98}
99
100impl TryFrom<u64> for SearchResultKind {
101    type Error = ();
102    fn try_from(value: u64) -> Result<Self, Self::Error> {
103        Ok(match value {
104            0 => Self::Document,
105            1 => Self::Paragraph,
106            2 => Self::Definition,
107            3 => Self::Example,
108            4 => Self::Assertion,
109            5 => Self::Problem,
110            _ => return Err(()),
111        })
112    }
113}
114impl TryFrom<ParagraphKind> for SearchResultKind {
115    type Error = ();
116    fn try_from(value: ParagraphKind) -> Result<Self, Self::Error> {
117        Ok(match value {
118            ParagraphKind::Assertion => Self::Assertion,
119            ParagraphKind::Definition => Self::Definition,
120            ParagraphKind::Example => Self::Example,
121            ParagraphKind::Paragraph => Self::Paragraph,
122            _ => return Err(()),
123        })
124    }
125}
126
127#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
128#[derive(Debug, Clone)]
129pub enum SearchIndex {
130    Document {
131        uri: DocumentUri,
132        title: Option<String>,
133        body: String,
134    },
135    Paragraph {
136        uri: DocumentElementUri,
137        kind: SearchResultKind,
138        definition_like: bool,
139        title: Option<String>,
140        fors: Vec<SymbolUri>,
141        body: String,
142    },
143}