Skip to main content

voce_validator/passes/
motion.rs

1//! Motion safety validation pass.
2//!
3//! Every animation must have a ReducedMotion alternative.
4//! Missing ReducedMotion is a compile error — accessibility
5//! is non-negotiable.
6
7use crate::errors::{Diagnostic, Severity, ValidationResult};
8use crate::index::NodeIndex;
9use crate::ir::{ChildNode, VoceIr};
10use crate::passes::ValidationPass;
11
12pub struct MotionPass;
13
14impl ValidationPass for MotionPass {
15    fn name(&self) -> &'static str {
16        "motion"
17    }
18
19    fn run(&self, ir: &VoceIr, _index: &NodeIndex, result: &mut ValidationResult) {
20        let root = match &ir.root {
21            Some(r) => r,
22            None => return,
23        };
24
25        if let Some(ref children) = root.children {
26            self.check_children(children, "/root/children", result);
27        }
28    }
29}
30
31impl MotionPass {
32    fn check_children(
33        &self,
34        children: &[ChildNode],
35        parent_path: &str,
36        result: &mut ValidationResult,
37    ) {
38        for (i, child) in children.iter().enumerate() {
39            let path = format!("{parent_path}/{i}");
40
41            match child.type_name() {
42                // MOT001: AnimationTransition must have ReducedMotion
43                "AnimationTransition" => {
44                    if let Some(anim) = child.as_type::<crate::ir::AnimationTransition>() {
45                        if anim.reduced_motion.is_none() {
46                            result.diagnostics.push(Diagnostic {
47                                severity: Severity::Error,
48                                code: "MOT001".to_string(),
49                                message:
50                                    "AnimationTransition must have a reduced_motion alternative"
51                                        .to_string(),
52                                node_path: path.clone(),
53                                pass: self.name().to_string(),
54                                hint: None,
55                            });
56                        }
57
58                        // MOT004: Duration warning
59                        if let Some(ref dur) = anim.duration {
60                            if let Some(ms) = dur.get("ms").and_then(|v| v.as_f64()) {
61                                if ms > 5000.0 {
62                                    result.diagnostics.push(Diagnostic {
63                                        severity: Severity::Warning,
64                                        code: "MOT004".to_string(),
65                                        message: format!("Animation duration is {ms}ms — consider shorter durations for better UX"),
66                                        node_path: path.clone(),
67                                        pass: self.name().to_string(),
68                    hint: None,
69                                    });
70                                }
71                            }
72                        }
73                    }
74                }
75                // MOT002: Sequence must have ReducedMotion
76                "Sequence" => {
77                    if let Some(seq) = child.as_type::<crate::ir::Sequence>() {
78                        if seq.reduced_motion.is_none() {
79                            result.diagnostics.push(Diagnostic {
80                                severity: Severity::Error,
81                                code: "MOT002".to_string(),
82                                message: "Sequence must have a reduced_motion alternative"
83                                    .to_string(),
84                                node_path: path.clone(),
85                                pass: self.name().to_string(),
86                                hint: None,
87                            });
88                        }
89                    }
90                }
91                // MOT005: ScrollBinding must have ReducedMotion
92                "ScrollBinding" => {
93                    if let Some(sb) = child.as_type::<crate::ir::ScrollBinding>() {
94                        if sb.reduced_motion.is_none() {
95                            result.diagnostics.push(Diagnostic {
96                                severity: Severity::Error,
97                                code: "MOT005".to_string(),
98                                message: "ScrollBinding must have a reduced_motion alternative"
99                                    .to_string(),
100                                node_path: path.clone(),
101                                pass: self.name().to_string(),
102                                hint: None,
103                            });
104                        }
105                    }
106                }
107                // MOT003: PhysicsBody damping must be > 0
108                "PhysicsBody" => {
109                    let damping = child
110                        .value
111                        .get("damping")
112                        .and_then(|v| v.as_f64())
113                        .unwrap_or(25.0);
114                    if damping <= 0.0 {
115                        result.diagnostics.push(Diagnostic {
116                            severity: Severity::Error,
117                            code: "MOT003".to_string(),
118                            message:
119                                "PhysicsBody damping must be > 0 to prevent infinite oscillation"
120                                    .to_string(),
121                            node_path: path.clone(),
122                            pass: self.name().to_string(),
123                            hint: None,
124                        });
125                    }
126                }
127                _ => {}
128            }
129
130            if let Some(grandchildren) = child.children() {
131                self.check_children(&grandchildren, &format!("{path}/children"), result);
132            }
133        }
134    }
135}