dsq_shared/ops/
traits.rs

1//! Core traits and types for operations
2//!
3//! This module defines the fundamental traits and basic types that other operations depend on.
4
5use crate::value::Value;
6use crate::Result;
7
8/// Assignment operators for variable assignments and updates
9#[derive(Debug, Clone, Copy, PartialEq)]
10pub enum AssignmentOperator {
11    /// Add assignment operator (+=)
12    AddAssign,
13    /// Update assignment operator (=)
14    UpdateAssign,
15}
16
17/// Context trait for operations that need additional context
18pub trait Context {
19    /// Get a variable value by name
20    fn get_variable(&self, name: &str) -> Option<&Value>;
21
22    /// Set a variable value
23    fn set_variable(&mut self, name: &str, value: Value);
24
25    /// Downcast to Any for concrete type checking
26    fn as_any(&self) -> &dyn std::any::Any;
27
28    /// Downcast mutably
29    fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
30}
31
32/// Simple context implementation that holds a single value
33pub struct SimpleContext {
34    /// The current value being processed
35    pub value: Value,
36}
37
38impl Context for SimpleContext {
39    /// Returns the current value for any variable name (since SimpleContext has no named variables)
40    fn get_variable(&self, _name: &str) -> Option<&Value> {
41        // For simple context, we don't have named variables, just the current value
42        // But for joins, we might need to access the current row value
43        Some(&self.value)
44    }
45
46    /// Does nothing since SimpleContext doesn't support setting variables
47    fn set_variable(&mut self, _name: &str, _value: Value) {
48        // Simple context doesn't support setting variables
49    }
50
51    fn as_any(&self) -> &dyn std::any::Any {
52        self
53    }
54
55    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
56        self
57    }
58}
59
60/// Trait for operations that can be applied to values
61///
62/// This trait provides a common interface for all data operations,
63/// allowing them to be composed and chained together.
64pub trait Operation {
65    /// Apply the operation to a value
66    fn apply(&self, value: &Value) -> Result<Value>;
67
68    /// Apply the operation to a value with optional context
69    fn apply_with_context(
70        &self,
71        value: &Value,
72        _context: &mut Option<&mut dyn Context>,
73    ) -> Result<Value> {
74        // Default implementation ignores context
75        self.apply(value)
76    }
77
78    /// Get a description of what this operation does
79    fn description(&self) -> String;
80
81    /// Check if this operation can be applied to the given value type
82    fn is_applicable(&self, value: &Value) -> bool {
83        // Default implementation: try to apply and see if it works
84        self.apply(value).is_ok()
85    }
86
87    /// Get as Any for downcasting
88    fn as_any(&self) -> &dyn std::any::Any;
89}