codama_korok_visitors/
visitable.rs

1use crate::visitor::KorokVisitor;
2use codama_errors::CodamaResult;
3
4pub trait KorokVisitable {
5    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()>;
6    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable>;
7}
8
9impl KorokVisitable for codama_koroks::KorokMut<'_, '_> {
10    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
11        match self {
12            Self::Crate(k) => k.accept(visitor),
13            Self::Enum(k) => k.accept(visitor),
14            Self::EnumVariant(k) => k.accept(visitor),
15            Self::Field(k) => k.accept(visitor),
16            Self::Fields(k) => k.accept(visitor),
17            Self::FileModule(k) => k.accept(visitor),
18            Self::Item(k) => k.accept(visitor),
19            Self::Module(k) => k.accept(visitor),
20            Self::Root(k) => k.accept(visitor),
21            Self::Struct(k) => k.accept(visitor),
22            Self::UnsupportedItem(k) => k.accept(visitor),
23        }
24    }
25
26    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
27        match self {
28            Self::Crate(k) => k.get_children(),
29            Self::Enum(k) => k.get_children(),
30            Self::EnumVariant(k) => k.get_children(),
31            Self::Field(k) => k.get_children(),
32            Self::Fields(k) => k.get_children(),
33            Self::FileModule(k) => k.get_children(),
34            Self::Item(k) => k.get_children(),
35            Self::Module(k) => k.get_children(),
36            Self::Root(k) => k.get_children(),
37            Self::Struct(k) => k.get_children(),
38            Self::UnsupportedItem(k) => k.get_children(),
39        }
40    }
41}
42
43impl KorokVisitable for codama_koroks::RootKorok<'_> {
44    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
45        visitor.visit_root(self)
46    }
47    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
48        self.crates
49            .iter_mut()
50            .map(|c| c as &mut dyn KorokVisitable)
51            .collect()
52    }
53}
54
55impl KorokVisitable for codama_koroks::CrateKorok<'_> {
56    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
57        visitor.visit_crate(self)
58    }
59    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
60        self.items
61            .iter_mut()
62            .map(|i| i as &mut dyn KorokVisitable)
63            .collect()
64    }
65}
66
67impl KorokVisitable for codama_koroks::ItemKorok<'_> {
68    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
69        visitor.visit_item(self)
70    }
71    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
72        match self {
73            codama_koroks::ItemKorok::FileModule(k) => vec![k as &mut dyn KorokVisitable],
74            codama_koroks::ItemKorok::Module(k) => vec![k as &mut dyn KorokVisitable],
75            codama_koroks::ItemKorok::Struct(k) => vec![k as &mut dyn KorokVisitable],
76            codama_koroks::ItemKorok::Enum(k) => vec![k as &mut dyn KorokVisitable],
77            codama_koroks::ItemKorok::Unsupported(k) => vec![k as &mut dyn KorokVisitable],
78        }
79    }
80}
81
82impl KorokVisitable for codama_koroks::FileModuleKorok<'_> {
83    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
84        visitor.visit_file_module(self)
85    }
86    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
87        self.items
88            .iter_mut()
89            .map(|i| i as &mut dyn KorokVisitable)
90            .collect()
91    }
92}
93
94impl KorokVisitable for codama_koroks::ModuleKorok<'_> {
95    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
96        visitor.visit_module(self)
97    }
98    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
99        self.items
100            .iter_mut()
101            .map(|i| i as &mut dyn KorokVisitable)
102            .collect()
103    }
104}
105
106impl KorokVisitable for codama_koroks::StructKorok<'_> {
107    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
108        visitor.visit_struct(self)
109    }
110    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
111        vec![&mut self.fields as &mut dyn KorokVisitable]
112    }
113}
114
115impl KorokVisitable for codama_koroks::FieldsKorok<'_> {
116    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
117        visitor.visit_fields(self)
118    }
119    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
120        self.all
121            .iter_mut()
122            .map(|f| f as &mut dyn KorokVisitable)
123            .collect()
124    }
125}
126
127impl KorokVisitable for codama_koroks::FieldKorok<'_> {
128    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
129        visitor.visit_field(self)
130    }
131    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
132        Vec::new()
133    }
134}
135
136impl KorokVisitable for codama_koroks::EnumKorok<'_> {
137    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
138        visitor.visit_enum(self)
139    }
140    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
141        self.variants
142            .iter_mut()
143            .map(|v| v as &mut dyn KorokVisitable)
144            .collect()
145    }
146}
147
148impl KorokVisitable for codama_koroks::EnumVariantKorok<'_> {
149    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
150        visitor.visit_enum_variant(self)
151    }
152    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
153        vec![&mut self.fields as &mut dyn KorokVisitable]
154    }
155}
156
157impl KorokVisitable for codama_koroks::UnsupportedItemKorok<'_> {
158    fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
159        visitor.visit_unsupported_item(self)
160    }
161    fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
162        Vec::new()
163    }
164}