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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use another_visitor::{Visitable, Visitor};
#[derive(Visitable)]
struct A {
b1: B,
b2: B,
}
#[derive(Visitable)]
enum B {
Var1(C),
Var2(D),
}
#[derive(Visitable)]
struct C {
#[visit(skip)]
i: i32,
}
#[derive(Visitable)]
struct D {
#[visit(skip)]
msg: String,
}
#[derive(Visitor)]
#[visit(A, B, C, D)]
struct AVisitor {}
impl another_visitor::VisitorHelper for AVisitor {
type Output = String;
#[allow(unused_variables)]
fn aggregate(&mut self, a: Self::Output, b: Self::Output) -> Self::Output {
format!("{a}{b}")
}
}
impl AVisitor {
fn visit_a(&mut self, a: &A) -> <Self as another_visitor::VisitorHelper>::Output {
format!("(A {} {})", self.visit(&a.b1), self.visit(&a.b2))
}
fn visit_b(&mut self, b: &B) -> <Self as another_visitor::VisitorHelper>::Output {
format!("(B {})", self.visit_children(b))
}
fn visit_c(&mut self, c: &C) -> <Self as another_visitor::VisitorHelper>::Output {
format!("(C {})", c.i)
}
fn visit_d(&mut self, d: &D) -> <Self as another_visitor::VisitorHelper>::Output {
format!("(D {})", d.msg)
}
}
fn main() {
let dat = A {
b1: B::Var1(C { i: 1 }),
b2: B::Var2(D { msg: "a".into() }),
};
let mut vis = AVisitor {};
println!("{}", vis.visit(&dat));
}