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 to limit what records the table represents
8    pub fn add_condition(&mut self, condition: impl Into<T::Condition>) {
9        let id = -self.next_condition_id;
10        self.next_condition_id += 1;
11        self.conditions.insert(id, condition.into());
12    }
13
14    /// Add a temporary condition that can be removed later
15    pub fn temp_add_condition(&mut self, condition: impl Into<T::Condition>) -> ConditionHandle {
16        let id = self.next_condition_id;
17        self.next_condition_id += 1;
18        self.conditions.insert(id, condition.into());
19        ConditionHandle::new(id)
20    }
21
22    /// Remove a temporary condition by its handle
23    pub fn temp_remove_condition(&mut self, handle: ConditionHandle) -> Result<()> {
24        if handle.0 <= 0 {
25            return Err(error!("Cannot remove permanent condition"));
26        }
27        self.conditions.shift_remove(&handle.0);
28        Ok(())
29    }
30
31    /// Get all conditions
32    pub fn conditions(&self) -> impl Iterator<Item = &T::Condition> {
33        self.conditions.values()
34    }
35
36    /// Add a condition using the builder pattern
37    pub fn with_condition(mut self, condition: impl Into<T::Condition>) -> Self {
38        self.add_condition(condition);
39        self
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use crate::mocks::mock_table_source::MockTableSource;
46
47    use super::*;
48    use vantage_expressions::expr_any;
49    use vantage_types::EmptyEntity;
50
51    #[test]
52    fn test_temp_conditions() {
53        let ds = MockTableSource::new();
54        let mut table = Table::<_, EmptyEntity>::new("test", ds);
55
56        // Add permanent condition
57        table.add_condition(expr_any!("perm1"));
58        assert_eq!(table.conditions().count(), 1);
59
60        // Add temp conditions
61        let handle1 = table.temp_add_condition(expr_any!("temp1"));
62        let handle2 = table.temp_add_condition(expr_any!("temp2"));
63        assert_eq!(table.conditions().count(), 3);
64
65        // Remove one temp condition
66        table.temp_remove_condition(handle1).unwrap();
67        assert_eq!(table.conditions().count(), 2);
68
69        // Add another permanent
70        table.add_condition(expr_any!("perm2"));
71        assert_eq!(table.conditions().count(), 3);
72
73        // Remove second temp
74        table.temp_remove_condition(handle2).unwrap();
75        assert_eq!(table.conditions().count(), 2);
76
77        // Verify we have exactly 2 conditions left (both permanent)
78        assert_eq!(table.conditions().count(), 2);
79    }
80
81    #[test]
82    fn test_cannot_remove_permanent_condition() {
83        let ds = MockTableSource::new();
84        let mut table = Table::<_, EmptyEntity>::new("test", ds);
85
86        table.add_condition(expr_any!("perm"));
87        let _handle = table.temp_add_condition(expr_any!("temp"));
88
89        // Try to forge a handle to permanent condition (negative ID)
90        let fake_handle = ConditionHandle::new(-1);
91        let result = table.temp_remove_condition(fake_handle);
92        assert!(result.is_err());
93    }
94}