codama_korok_visitors/
visitable.rs1use 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::FileModule(k) => k.accept(visitor),
17 Self::Item(k) => k.accept(visitor),
18 Self::Module(k) => k.accept(visitor),
19 Self::Root(k) => k.accept(visitor),
20 Self::Struct(k) => k.accept(visitor),
21 Self::Const(k) => k.accept(visitor),
22 Self::UnsupportedItem(k) => k.accept(visitor),
23 Self::ImplItem(k) => k.accept(visitor),
24 Self::UnsupportedImplItem(k) => k.accept(visitor),
25 }
26 }
27
28 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
29 match self {
30 Self::Crate(k) => k.get_children(),
31 Self::Enum(k) => k.get_children(),
32 Self::EnumVariant(k) => k.get_children(),
33 Self::Field(k) => k.get_children(),
34 Self::FileModule(k) => k.get_children(),
35 Self::Item(k) => k.get_children(),
36 Self::Module(k) => k.get_children(),
37 Self::Root(k) => k.get_children(),
38 Self::Struct(k) => k.get_children(),
39 Self::Const(k) => k.get_children(),
40 Self::UnsupportedItem(k) => k.get_children(),
41 Self::ImplItem(k) => k.get_children(),
42 Self::UnsupportedImplItem(k) => k.get_children(),
43 }
44 }
45}
46
47impl KorokVisitable for codama_koroks::RootKorok<'_> {
48 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
49 visitor.visit_root(self)
50 }
51 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
52 self.crates
53 .iter_mut()
54 .map(|c| c as &mut dyn KorokVisitable)
55 .collect()
56 }
57}
58
59impl KorokVisitable for codama_koroks::CrateKorok<'_> {
60 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
61 visitor.visit_crate(self)
62 }
63 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
64 self.items
65 .iter_mut()
66 .map(|i| i as &mut dyn KorokVisitable)
67 .collect()
68 }
69}
70
71impl KorokVisitable for codama_koroks::ItemKorok<'_> {
72 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
73 visitor.visit_item(self)
74 }
75 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
76 match self {
77 codama_koroks::ItemKorok::FileModule(k) => vec![k as &mut dyn KorokVisitable],
78 codama_koroks::ItemKorok::Module(k) => vec![k as &mut dyn KorokVisitable],
79 codama_koroks::ItemKorok::Struct(k) => vec![k as &mut dyn KorokVisitable],
80 codama_koroks::ItemKorok::Enum(k) => vec![k as &mut dyn KorokVisitable],
81 codama_koroks::ItemKorok::Impl(k) => vec![k as &mut dyn KorokVisitable],
82 codama_koroks::ItemKorok::Const(k) => vec![k as &mut dyn KorokVisitable],
83 codama_koroks::ItemKorok::Unsupported(k) => vec![k as &mut dyn KorokVisitable],
84 }
85 }
86}
87
88impl KorokVisitable for codama_koroks::FileModuleKorok<'_> {
89 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
90 visitor.visit_file_module(self)
91 }
92 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
93 self.items
94 .iter_mut()
95 .map(|i| i as &mut dyn KorokVisitable)
96 .collect()
97 }
98}
99
100impl KorokVisitable for codama_koroks::ModuleKorok<'_> {
101 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
102 visitor.visit_module(self)
103 }
104 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
105 self.items
106 .iter_mut()
107 .map(|i| i as &mut dyn KorokVisitable)
108 .collect()
109 }
110}
111
112impl KorokVisitable for codama_koroks::StructKorok<'_> {
113 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
114 visitor.visit_struct(self)
115 }
116 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
117 self.fields
118 .iter_mut()
119 .map(|f| f as &mut dyn KorokVisitable)
120 .collect()
121 }
122}
123
124impl KorokVisitable for codama_koroks::FieldKorok<'_> {
125 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
126 visitor.visit_field(self)
127 }
128 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
129 Vec::new()
130 }
131}
132
133impl KorokVisitable for codama_koroks::EnumKorok<'_> {
134 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
135 visitor.visit_enum(self)
136 }
137 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
138 self.variants
139 .iter_mut()
140 .map(|v| v as &mut dyn KorokVisitable)
141 .collect()
142 }
143}
144
145impl KorokVisitable for codama_koroks::EnumVariantKorok<'_> {
146 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
147 visitor.visit_enum_variant(self)
148 }
149 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
150 self.fields
151 .iter_mut()
152 .map(|f| f as &mut dyn KorokVisitable)
153 .collect()
154 }
155}
156
157impl KorokVisitable for codama_koroks::ImplKorok<'_> {
158 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
159 visitor.visit_impl(self)
160 }
161 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
162 self.items
163 .iter_mut()
164 .map(|c| c as &mut dyn KorokVisitable)
165 .collect()
166 }
167}
168
169impl KorokVisitable for codama_koroks::ImplItemKorok<'_> {
170 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
171 visitor.visit_impl_item(self)
172 }
173 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
174 match self {
175 codama_koroks::ImplItemKorok::Const(k) => vec![k as &mut dyn KorokVisitable],
176 codama_koroks::ImplItemKorok::Unsupported(k) => vec![k as &mut dyn KorokVisitable],
177 }
178 }
179}
180
181impl KorokVisitable for codama_koroks::UnsupportedImplItemKorok<'_> {
182 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
183 visitor.visit_unsupported_impl_item(self)
184 }
185 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
186 Vec::new()
187 }
188}
189
190impl KorokVisitable for codama_koroks::ConstKorok<'_> {
191 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
192 visitor.visit_const(self)
193 }
194 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
195 Vec::new()
196 }
197}
198
199impl KorokVisitable for codama_koroks::UnsupportedItemKorok<'_> {
200 fn accept(&mut self, visitor: &mut dyn KorokVisitor) -> CodamaResult<()> {
201 visitor.visit_unsupported_item(self)
202 }
203 fn get_children(&mut self) -> Vec<&mut dyn KorokVisitable> {
204 Vec::new()
205 }
206}