yew_datatable_core/features/aggregation/aggregation_fn.rs
1//! Custom aggregation function type for column-specific aggregation logic.
2//!
3//! Allows users to provide custom aggregation functions for columns
4//! that require specialized aggregation behavior in grouped rows.
5
6use std::fmt;
7
8/// Type alias for the boxed aggregation closure.
9type AggregateFn<T> = Box<dyn Fn(&[&T]) -> String + Send + Sync>;
10
11/// Custom aggregation function type.
12///
13/// Wraps a function that aggregates a slice of row references
14/// into a string representation.
15pub struct AggregationFn<T> {
16 /// The aggregation function.
17 aggregate: AggregateFn<T>,
18}
19
20impl<T> AggregationFn<T> {
21 /// Creates a new aggregation function.
22 ///
23 /// # Parameters
24 ///
25 /// - `f`: A function that aggregates row references into a string.
26 ///
27 /// # Returns
28 ///
29 /// - `AggregationFn<T>`: A new aggregation function instance.
30 pub fn new<F>(f: F) -> Self
31 where
32 F: Fn(&[&T]) -> String + Send + Sync + 'static,
33 {
34 Self { aggregate: Box::new(f) }
35 }
36
37 /// Aggregates a list of rows.
38 ///
39 /// # Parameters
40 ///
41 /// - `rows`: A slice of row references to aggregate.
42 ///
43 /// # Returns
44 ///
45 /// - `String`: The aggregated result as a string.
46 pub fn aggregate(&self, rows: &[&T]) -> String {
47 // Invoke the aggregation function.
48 (self.aggregate)(rows)
49 }
50}
51
52/// Formats the aggregation function for debug output.
53impl<T> fmt::Debug for AggregationFn<T> {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 f.debug_struct("AggregationFn").finish_non_exhaustive()
56 }
57}