Skip to main content

ahri_tre_protocol/
search.rs

1use crate::refs::TagSelector;
2use serde::{Deserialize, Serialize};
3
4pub const MAX_SEARCH_PREDICATES: usize = 20;
5pub const MAX_TEXT_VALUE_LENGTH: usize = 256;
6pub const MAX_SUMMARY_TAGS: usize = 20;
7pub use crate::pagination::{DEFAULT_PAGE_LIMIT, MAX_PAGE_LIMIT};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum TextMode {
12    Exact,
13    Prefix,
14    Contains,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
18pub struct TextPredicate<F> {
19    pub field: F,
20    pub mode: TextMode,
21    pub value: String,
22}
23
24impl<F> TextPredicate<F> {
25    pub fn validate(&mut self) -> Result<(), String> {
26        self.value = self.value.trim().to_string();
27        if self.value.is_empty() {
28            return Err("text predicate value must not be empty".to_string());
29        }
30        if self.value.len() > MAX_TEXT_VALUE_LENGTH {
31            return Err(format!(
32                "text predicate value must be at most {MAX_TEXT_VALUE_LENGTH} characters"
33            ));
34        }
35        Ok(())
36    }
37}
38
39#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
40pub struct TagPredicate {
41    pub selector: TagSelector,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
45#[serde(tag = "kind", content = "predicates", rename_all = "snake_case")]
46pub enum PredicateSet<P> {
47    AllOf(Vec<P>),
48    AnyOf(Vec<P>),
49}
50
51impl<P> PredicateSet<P> {
52    pub fn predicates(&self) -> &[P] {
53        match self {
54            Self::AllOf(predicates) | Self::AnyOf(predicates) => predicates,
55        }
56    }
57
58    pub fn predicates_mut(&mut self) -> &mut Vec<P> {
59        match self {
60            Self::AllOf(predicates) | Self::AnyOf(predicates) => predicates,
61        }
62    }
63
64    pub fn validate(&self) -> Result<(), String> {
65        let count = self.predicates().len();
66        if count == 0 {
67            return Err("search predicates must not be empty".to_string());
68        }
69        if count > MAX_SEARCH_PREDICATES {
70            return Err(format!(
71                "search predicates must contain at most {MAX_SEARCH_PREDICATES} items"
72            ));
73        }
74        Ok(())
75    }
76}
77
78#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
79pub struct SearchCursorBinding<Q> {
80    pub query: Q,
81    pub page_limit: u16,
82    pub protocol_version: String,
83}