| 130 | |
| 131 | impl Tree { |
| 132 | pub fn run(&self, _config: GlobalConfig) -> CliResult { |
| 133 | let bump = Bump::default(); |
| 134 | |
| 135 | for (filename, mut source) in self.input.sources()? { |
| 136 | let mut src = String::new(); |
| 137 | source.read_to_string(&mut src)?; |
| 138 | |
| 139 | let lexer = Lexer::new(&CssAtomSet::ATOMS, &src); |
| 140 | let mut parser = Parser::new(&bump, &src, lexer); |
| 141 | let result = parser.parse_entirely::<StyleSheet>(); |
| 142 | |
| 143 | let Some(stylesheet) = result.output.as_ref() else { |
| 144 | for err in result.errors { |
| 145 | eprintln!("{}", crate::commands::format_diagnostic_error(&err, &src, filename)); |
| 146 | } |
| 147 | return Err(CliError::ParseFailed); |
| 148 | }; |
| 149 | |
| 150 | // Collect all nodes |
| 151 | let mut visitor = CollectionVisitor::new(&src); |
| 152 | let _ = stylesheet.accept(&mut visitor); |
| 153 | |
| 154 | // Print root |
| 155 | if let Some(root) = visitor.nodes.first() { |
| 156 | println!("{}", root.display); |
| 157 | } |
| 158 | |
| 159 | // Print tree (skip the first node which is the root we just printed) |
| 160 | for (i, node) in visitor.nodes.iter().enumerate().skip(1) { |
| 161 | let prefix = self.build_prefix(&visitor, i, node.depth); |
| 162 | println!("{}{}", prefix, node.display); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | Ok(()) |
| 167 | } |
| 168 | |
| 169 | /// Build the tree prefix (vertical bars and connectors) for a node. |
| 170 | fn build_prefix(&self, visitor: &CollectionVisitor, node_idx: usize, node_depth: usize) -> String { |