codama_korok_visitors/
debug_visitor.rs1use crate::KorokVisitor;
2use codama_errors::CodamaResult;
3use codama_koroks::KorokTrait;
4
5#[derive(Default)]
7pub struct DebugVisitor {
8 current_result: String,
9 current_indent: usize,
10}
11
12impl DebugVisitor {
13 const INDENT: &'static str = "| ";
14
15 pub fn new() -> Self {
16 Self::default()
17 }
18
19 pub fn get_result(self) -> String {
20 self.current_result
21 }
22
23 pub fn clear(&mut self) {
24 self.current_result.clear();
25 self.current_indent = 0;
26 }
27
28 fn write(
29 &mut self,
30 identifier: &str,
31 options: Option<&str>,
32 mut korok: codama_koroks::KorokMut,
33 ) -> CodamaResult<()> {
34 self.write_indent();
35 self.current_result.push_str(identifier);
36 if let Some(text) = options {
37 self.current_result.push_str(&format!(" ({text})"));
38 }
39
40 let json = serde_json::to_string(&korok.node())?;
41 self.current_result.push_str(&format!(": {json}\n"));
42
43 self.current_indent += 1;
44 self.visit_children(&mut korok)?;
45 self.current_indent -= 1;
46 Ok(())
47 }
48
49 fn write_indent(&mut self) {
50 self.current_result
51 .push_str(&Self::INDENT.repeat(self.current_indent));
52 }
53}
54
55impl KorokVisitor for DebugVisitor {
56 fn visit_root(&mut self, korok: &mut codama_koroks::RootKorok) -> CodamaResult<()> {
57 self.write("Root", None, korok.into())
58 }
59
60 fn visit_crate(&mut self, korok: &mut codama_koroks::CrateKorok) -> CodamaResult<()> {
61 self.write(
62 "Crate",
63 Some(&korok.store.path.display().to_string()),
64 korok.into(),
65 )
66 }
67
68 fn visit_item(&mut self, korok: &mut codama_koroks::ItemKorok) -> CodamaResult<()> {
69 self.write("Item", None, korok.into())
70 }
71
72 fn visit_file_module(
73 &mut self,
74 korok: &mut codama_koroks::FileModuleKorok,
75 ) -> CodamaResult<()> {
76 self.write(
77 "FileModule",
78 Some(&format!(
79 "{} -> {}",
80 korok.ast.ident,
81 korok.store.path.display(),
82 )),
83 korok.into(),
84 )
85 }
86
87 fn visit_module(&mut self, korok: &mut codama_koroks::ModuleKorok) -> CodamaResult<()> {
88 self.write("Module", Some(&korok.ast.ident.to_string()), korok.into())
89 }
90
91 fn visit_struct(&mut self, korok: &mut codama_koroks::StructKorok) -> CodamaResult<()> {
92 self.write("Struct", Some(&korok.ast.ident.to_string()), korok.into())
93 }
94
95 fn visit_enum(&mut self, korok: &mut codama_koroks::EnumKorok) -> CodamaResult<()> {
96 self.write("Enum", Some(&korok.ast.ident.to_string()), korok.into())
97 }
98
99 fn visit_enum_variant(
100 &mut self,
101 korok: &mut codama_koroks::EnumVariantKorok,
102 ) -> CodamaResult<()> {
103 self.write(
104 "EnumVariant",
105 Some(&korok.ast.ident.to_string()),
106 korok.into(),
107 )
108 }
109
110 fn visit_const(&mut self, korok: &mut codama_koroks::ConstKorok) -> CodamaResult<()> {
111 let ident = match korok.ast {
112 codama_koroks::ConstAst::Item(item) => &item.ident,
113 codama_koroks::ConstAst::ImplItem(item) => &item.ident,
114 };
115
116 self.write("Const", Some(&ident.to_string()), korok.into())
117 }
118
119 fn visit_unsupported_item(
120 &mut self,
121 korok: &mut codama_koroks::UnsupportedItemKorok,
122 ) -> CodamaResult<()> {
123 self.write("UnsupportedItem", None, korok.into())
124 }
125
126 fn visit_impl_item(&mut self, korok: &mut codama_koroks::ImplItemKorok) -> CodamaResult<()> {
127 self.write("ImplItem", None, korok.into())
128 }
129
130 fn visit_unsupported_impl_item(
131 &mut self,
132 korok: &mut codama_koroks::UnsupportedImplItemKorok,
133 ) -> CodamaResult<()> {
134 self.write("UnsupportedImplItem", None, korok.into())
135 }
136
137 fn visit_field(&mut self, korok: &mut codama_koroks::FieldKorok) -> CodamaResult<()> {
138 let ident = korok
139 .ast
140 .ident
141 .as_ref()
142 .map_or("None".to_string(), |i| i.to_string());
143
144 self.write("Field", Some(&ident), korok.into())
145 }
146}