helix/dna/ops/
engine.rs

1use crate::dna::hel::error::HlxError;
2use crate::ops::conditional::ConditionalOperators;
3use crate::ops::string_processing::StringOperators;
4use crate::ops::fundamental::OperatorRegistry;
5use crate::ops::validation::ValidationOperators;
6use crate::ops::math::MathOperators;
7use crate::dna::atp::value::Value;
8
9pub struct OperatorEngine {
10    conditional_operators: ConditionalOperators,
11    string_operators: StringOperators,
12    operator_registry: OperatorRegistry,
13    validation_operators: ValidationOperators,
14    math_operators: MathOperators,
15}
16impl OperatorEngine {
17    pub async fn new() -> Result<Self, HlxError> {
18        Ok(Self {
19            conditional_operators: ConditionalOperators::new().await?,
20            string_operators: StringOperators::new().await?,
21            operator_registry: OperatorRegistry::new().await?,
22            validation_operators: ValidationOperators::new().await?,
23            math_operators: MathOperators::new().await?,
24        })
25    }
26    pub async fn execute_operator(
27        &self,
28        operator: &str,
29        params: &str,
30    ) -> Result<Value, HlxError> {
31        if operator.starts_with('@') {
32            return self.operator_registry.execute(operator, params).await;
33        }
34        match operator {
35            "var" => self.operator_registry.execute("variable", params).await,
36            "date" => self.operator_registry.execute("date", params).await,
37            "file" => self.operator_registry.execute("file", params).await,
38            "json" => self.operator_registry.execute("json", params).await,
39            "query" => self.operator_registry.execute("query", params).await,
40            "base64" => self.operator_registry.execute("base64", params).await,
41            "uuid" => self.operator_registry.execute("uuid", params).await,
42            "if" => self.conditional_operators.execute("if", params).await,
43            "switch" => self.conditional_operators.execute("switch", params).await,
44            "loop" => self.conditional_operators.execute("loop", params).await,
45            "filter" => self.conditional_operators.execute("filter", params).await,
46            "map" => self.conditional_operators.execute("map", params).await,
47            "reduce" => self.conditional_operators.execute("reduce", params).await,
48            "concat" => self.string_operators.execute("concat", params).await,
49            "split" => self.string_operators.execute("split", params).await,
50            "replace" => self.string_operators.execute("replace", params).await,
51            "trim" => self.string_operators.execute("trim", params).await,
52            "upper" => self.string_operators.execute("upper", params).await,
53            "lower" => self.string_operators.execute("lower", params).await,
54            "hash" => self.string_operators.execute("hash", params).await,
55            "format" => self.string_operators.execute("format", params).await,
56            "calc" => self.math_operators.execute("calc", params).await,
57            "eval" => self.math_operators.execute("eval", params).await,
58            _ => Err(HlxError::unknown_operator(operator)),
59        }
60    }
61    pub fn operator_registry(&self) -> &OperatorRegistry {
62        &self.operator_registry
63    }
64    pub fn get_variable(&self, name: &str) -> Result<Value, HlxError> {
65        self.operator_registry.get_variable(name)
66    }
67}