1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! A very simple example processor which returns a new version of the
//! given AST with all comments changed to upper case.

use crate::generator::ast::*;
use crate::generator::processor::*;

pub struct UpcaseComments {}

impl UpcaseComments {
    #[allow(dead_code)]
    pub fn run(node: &Node) -> Result<Node> {
        let mut p = UpcaseComments {};
        Ok(node.process(&mut p)?.unwrap())
    }
}

impl Processor for UpcaseComments {
    fn on_node(&mut self, node: &Node) -> Result<Return> {
        match &node.attrs {
            Attrs::Comment(level, msg) => {
                let new_node = node.replace_attrs(Attrs::Comment(*level, msg.to_uppercase()));
                Ok(Return::Replace(new_node))
            }
            _ => Ok(Return::ProcessChildren),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::generator::ast::*;
    use crate::generator::processors::*;

    #[test]
    fn it_works() {
        let mut ast = AST::new();
        ast.push_and_open(node!(Test, "t1".to_string()));
        ast.push(node!(Cycle, 1, false));
        ast.push(node!(Comment, 1, "some comment".to_string()));

        let mut expect = AST::new();
        expect.push_and_open(node!(Test, "t1".to_string()));
        expect.push(node!(Cycle, 1, false));
        expect.push(node!(Comment, 1, "SOME COMMENT".to_string()));

        assert_eq!(
            UpcaseComments::run(&ast.to_node()).expect("Comments upcased"),
            expect
        );
    }
}