devalang_wasm/language/preprocessor/resolver/
trigger.rs

1use crate::language::preprocessor::resolver::value::resolve_value;
2use crate::language::syntax::ast::{DurationValue, Statement, StatementKind, Value};
3use std::collections::HashMap;
4
5/// Resolve trigger statement: expand duration and effect identifiers
6pub fn resolve_trigger(
7    stmt: &Statement,
8    entity: &str,
9    duration: &DurationValue,
10    effects: &Option<Value>,
11    variables: &HashMap<String, Value>,
12) -> Statement {
13    let final_duration = resolve_duration(duration, variables);
14    let final_effects = effects.as_ref().map(|e| resolve_value(e, variables, 0));
15
16    Statement::new(
17        StatementKind::Trigger {
18            entity: entity.to_string(),
19            duration: final_duration,
20            effects: final_effects,
21        },
22        Value::Null,
23        stmt.indent,
24        stmt.line,
25        stmt.column,
26    )
27}
28
29fn resolve_duration(duration: &DurationValue, variables: &HashMap<String, Value>) -> DurationValue {
30    match duration {
31        DurationValue::Identifier(name) => {
32            if let Some(val) = variables.get(name) {
33                match val {
34                    Value::Number(n) => DurationValue::Milliseconds(*n),
35                    Value::Identifier(s) if s == "auto" => DurationValue::Auto,
36                    Value::String(s) if s.ends_with("ms") => {
37                        if let Ok(ms) = s.trim_end_matches("ms").parse::<f32>() {
38                            return DurationValue::Milliseconds(ms);
39                        }
40                        DurationValue::Identifier(name.clone())
41                    }
42                    _ => DurationValue::Identifier(name.clone()),
43                }
44            } else {
45                DurationValue::Identifier(name.clone())
46            }
47        }
48        other => other.clone(),
49    }
50}
51
52#[cfg(test)]
53#[path = "test_trigger.rs"]
54mod tests;