icydb_core/visitor/
validate.rs

1use crate::{
2    traits::Visitable,
3    visitor::{PathSegment, Visitor, VisitorAdapter, VisitorContext, VisitorIssues, perform_visit},
4};
5
6///
7/// validate
8/// Validate a visitable tree, collecting issues by path.
9///
10/// Validation is non-failing at the traversal level. All validation
11/// issues are collected and returned to the caller, which may choose
12/// how to interpret them.
13///
14pub(crate) fn validate(node: &dyn Visitable) -> Result<(), VisitorIssues> {
15    let visitor = ValidateVisitor::new();
16    let mut adapter = VisitorAdapter::new(visitor);
17
18    perform_visit(&mut adapter, node, PathSegment::Empty);
19
20    adapter.result()
21}
22
23///
24/// ValidateVisitor
25/// Walks a tree and applies validation at each node.
26///
27#[derive(Debug, Default)]
28pub struct ValidateVisitor;
29
30impl ValidateVisitor {
31    #[must_use]
32    pub const fn new() -> Self {
33        Self
34    }
35}
36
37impl Visitor for ValidateVisitor {
38    fn enter(&mut self, node: &dyn Visitable, ctx: &mut dyn VisitorContext) {
39        node.validate_self(ctx);
40        node.validate_custom(ctx);
41    }
42
43    fn exit(&mut self, _: &dyn Visitable, _: &mut dyn VisitorContext) {}
44}