vantage_table/table/impls/
conditions.rs1use vantage_core::{Result, error};
2use vantage_types::Entity;
3
4use crate::{conditions::ConditionHandle, table::Table, traits::table_source::TableSource};
5
6impl<T: TableSource, E: Entity<T::Value>> Table<T, E> {
7 pub fn add_condition(&mut self, condition: impl Into<T::Condition>) {
10 let condition = condition.into();
11 if let Some((column, value)) = self.data_source.condition_equality(&condition) {
12 self.add_invariant(column, value);
13 }
14 let id = -self.next_condition_id;
15 self.next_condition_id += 1;
16 self.conditions.insert(id, condition);
17 }
18
19 pub fn temp_add_condition(&mut self, condition: impl Into<T::Condition>) -> ConditionHandle {
21 let id = self.next_condition_id;
22 self.next_condition_id += 1;
23 self.conditions.insert(id, condition.into());
24 ConditionHandle::new(id)
25 }
26
27 pub fn temp_remove_condition(&mut self, handle: ConditionHandle) -> Result<()> {
31 if handle.0 <= 0 {
32 return Err(error!("Cannot remove permanent condition"));
33 }
34 match self.conditions.shift_remove(&handle.0) {
35 Some(_) => Ok(()),
36 None => Err(
37 error!("No temporary condition for this handle", handle = handle.0)
38 .mark_not_found(),
39 ),
40 }
41 }
42
43 pub fn replace_temp_condition(
48 &mut self,
49 old: Option<ConditionHandle>,
50 new: Option<T::Condition>,
51 ) -> Result<Option<ConditionHandle>> {
52 if let Some(handle) = old {
53 self.temp_remove_condition(handle)?;
54 }
55 Ok(new.map(|condition| self.temp_add_condition(condition)))
56 }
57
58 pub fn conditions(&self) -> impl Iterator<Item = &T::Condition> {
60 self.conditions.values()
61 }
62
63 pub fn with_condition(mut self, condition: impl Into<T::Condition>) -> Self {
65 self.add_condition(condition);
66 self
67 }
68
69 pub fn add_search(&mut self, value: &str)
72 where
73 T: Sized,
74 {
75 let condition = self.data_source().search_table_condition(self, value);
76 self.add_condition(condition);
77 }
78
79 pub fn with_search(mut self, value: &str) -> Self
81 where
82 T: Sized,
83 {
84 self.add_search(value);
85 self
86 }
87}