Skip to main content

yew_datatable_core/features/aggregation/
aggregation_state.rs

1//! Complete aggregation state for the table.
2//!
3//! Manages column-level aggregation function assignments
4//! and provides configuration for aggregation behavior.
5
6use std::collections::HashMap;
7
8use crate::column::column_id::ColumnId;
9use crate::features::aggregation::built_in_aggregation::BuiltInAggregation;
10
11/// Complete aggregation state for the table.
12///
13/// Tracks which columns have aggregation functions assigned
14/// and whether aggregation processing is enabled.
15#[derive(Debug, Clone, Default, PartialEq)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17pub struct AggregationState {
18    /// Map of column ID to aggregation function.
19    column_aggregations: HashMap<ColumnId, BuiltInAggregation>,
20
21    /// Whether aggregation is enabled.
22    enabled: bool,
23}
24
25impl AggregationState {
26    /// Creates a new empty aggregation state.
27    ///
28    /// # Returns
29    ///
30    /// - `AggregationState`: A new aggregation state with no functions assigned.
31    pub fn new() -> Self {
32        Self {
33            column_aggregations: HashMap::new(),
34            enabled: true,
35        }
36    }
37
38    /// Sets whether aggregation is enabled.
39    ///
40    /// # Parameters
41    ///
42    /// - `enabled`: Whether aggregation processing is active.
43    ///
44    /// # Returns
45    ///
46    /// - `Self`: The modified aggregation state.
47    pub fn with_enabled(mut self, enabled: bool) -> Self {
48        // Update the enabled flag.
49        self.enabled = enabled;
50        self
51    }
52
53    /// Returns whether aggregation is enabled.
54    ///
55    /// # Returns
56    ///
57    /// - `bool`: Whether aggregation processing is active.
58    pub fn is_enabled(&self) -> bool {
59        self.enabled
60    }
61
62    /// Returns the aggregation function for a column.
63    ///
64    /// # Parameters
65    ///
66    /// - `column_id`: The column identifier to look up.
67    ///
68    /// # Returns
69    ///
70    /// - `Option<BuiltInAggregation>`: The assigned aggregation function.
71    pub fn get_aggregation(&self, column_id: &ColumnId) -> Option<BuiltInAggregation> {
72        // Look up the column in the aggregation map.
73        self.column_aggregations.get(column_id).copied()
74    }
75
76    /// Sets the aggregation function for a column.
77    ///
78    /// # Parameters
79    ///
80    /// - `column_id`: The column identifier.
81    /// - `function`: The aggregation function to assign.
82    pub fn set_aggregation(&mut self, column_id: ColumnId, function: BuiltInAggregation) {
83        // Insert or update the aggregation function.
84        self.column_aggregations.insert(column_id, function);
85    }
86
87    /// Removes the aggregation function for a column.
88    ///
89    /// # Parameters
90    ///
91    /// - `column_id`: The column identifier to remove.
92    pub fn remove_aggregation(&mut self, column_id: &ColumnId) {
93        // Remove the column from the aggregation map.
94        self.column_aggregations.remove(column_id);
95    }
96
97    /// Clears all aggregation settings.
98    pub fn clear(&mut self) {
99        // Remove all aggregation assignments.
100        self.column_aggregations.clear();
101    }
102
103    /// Resets to initial state.
104    pub fn reset(&mut self) {
105        // Clear all aggregation assignments.
106        self.column_aggregations.clear();
107    }
108}