versatile_data/
search.rs

1mod enums;
2mod result;
3
4use crate::FieldName;
5
6use super::{Activity, Data};
7
8pub use enums::*;
9
10pub struct Search<'a> {
11    data: &'a Data,
12    conditions: Vec<Condition<'a>>,
13}
14impl<'a> Search<'a> {
15    fn new(data: &'a Data) -> Self {
16        Search {
17            data,
18            conditions: Vec::new(),
19        }
20    }
21
22    /// Searches for data whose term is greater than or equal to the current date and time and is active.
23    pub fn search_default(mut self) -> Self {
24        if self.data.term_begin.is_some() {
25            self.conditions.push(Condition::Term(Term::default()));
26        }
27        if self.data.activity.is_some() {
28            self.conditions.push(Condition::Activity(Activity::Active));
29        }
30        self
31    }
32
33    /// Search field.
34    pub fn search_field(self, name: FieldName, condition: &'a Field) -> Self {
35        self.search(Condition::Field(name, condition))
36    }
37
38    /// Search by data publication period.
39    pub fn search_term(self, condition: Term) -> Self {
40        if self.data.term_begin.is_some() {
41            self.search(Condition::Term(condition))
42        } else {
43            self
44        }
45    }
46
47    /// Search by data activity.
48    pub fn search_activity(self, condition: Activity) -> Self {
49        if self.data.term_begin.is_some() {
50            self.search(Condition::Activity(condition))
51        } else {
52            self
53        }
54    }
55
56    /// Search by row.
57    pub fn search_row(self, condition: &'a Number) -> Self {
58        self.search(Condition::Row(condition))
59    }
60
61    /// Search with any condition.
62    pub fn search(mut self, condition: Condition<'a>) -> Self {
63        self.conditions.push(condition);
64        self
65    }
66}
67
68impl Data {
69    /// Create a new [Search] object.
70    pub fn begin_search(&self) -> Search {
71        Search::new(self)
72    }
73
74    /// Create a [Search] object with the field search set.
75    pub fn search_field<'a>(&'a self, name: FieldName, condition: &'a Field) -> Search {
76        Search::new(self).search_field(name, condition)
77    }
78
79    /// Create a [Search] object with the activity search set.
80    pub fn search_activity<'a>(&'a self, condition: Activity) -> Search {
81        Search::new(self).search_activity(condition)
82    }
83
84    /// Create a [Search] object with the term search set.
85    pub fn search_term<'a>(&'a self, condition: Term) -> Search {
86        Search::new(self).search_term(condition)
87    }
88
89    /// Create a [Search] object with the row search set.
90    pub fn search_row<'a>(&'a self, condition: &'a Number) -> Search {
91        Search::new(self).search_row(condition)
92    }
93
94    /// Creates a [Search] object with a default search set. Searches for data whose term is greater than or equal to the current date and time and is active.
95    pub fn search_default(&self) -> Search {
96        Search::new(self).search_default()
97    }
98}