Helper: parse Rust source, find the first `function_item` node, and return its complexity.
(source: &str)
| 2 | |
| 3 | /// Helper: parse Rust source, find the first `function_item` node, and return its complexity. |
| 4 | fn rust_fn_complexity(source: &str) -> tracedecay::extraction::complexity::ComplexityMetrics { |
| 5 | let mut parser = tree_sitter::Parser::new(); |
| 6 | parser |
| 7 | .set_language( |
| 8 | &tracedecay::extraction::ts_provider::language("rust") |
| 9 | .expect("failed to load Rust grammar"), |
| 10 | ) |
| 11 | .expect("failed to load Rust grammar"); |
| 12 | let tree = parser.parse(source, None).expect("parse failed"); |
| 13 | let root = tree.root_node(); |
| 14 | let fn_node = find_first_kind(root, "function_item").expect("no function_item found in source"); |
| 15 | count_complexity(fn_node, &RUST_COMPLEXITY, source.as_bytes()) |
| 16 | } |
| 17 | |
| 18 | /// Recursively find the first node of the given kind. |
| 19 | fn find_first_kind<'a>(node: tree_sitter::Node<'a>, kind: &str) -> Option<tree_sitter::Node<'a>> { |
no test coverage detected