Skip to main content

vantage_table/table/impls/
conditions.rs

1use 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    /// Add a permanent condition. A literal `column = value` condition (see
8    /// [`TableSource::condition_equality`]) also becomes a set invariant.
9    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    /// Add a temporary condition that can be removed later
20    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    /// Remove a temporary condition by its handle. A handle this table holds
28    /// no condition for (already removed, or from another table) is
29    /// not-found.
30    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    /// Swap the temporary condition `old` for `new`: `old` is removed when
44    /// given, `new` added when given, and the handle of `new` returned. A
45    /// stale `old` fails as [`temp_remove_condition`](Self::temp_remove_condition)
46    /// does, before `new` is added.
47    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    /// Get all conditions
59    pub fn conditions(&self) -> impl Iterator<Item = &T::Condition> {
60        self.conditions.values()
61    }
62
63    /// Add a condition using the builder pattern
64    pub fn with_condition(mut self, condition: impl Into<T::Condition>) -> Self {
65        self.add_condition(condition);
66        self
67    }
68
69    /// Add a search condition that matches `value` across
70    /// [`search_columns`](Self::search_columns).
71    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    /// Add a search condition using the builder pattern.
80    pub fn with_search(mut self, value: &str) -> Self
81    where
82        T: Sized,
83    {
84        self.add_search(value);
85        self
86    }
87}