Extract code graph nodes and edges from a Rust source file. `file_path` is used for qualified names and node IDs (not for I/O). `source` is the Rust source code to parse.
(file_path: &str, source: &str)
| 77 | /// `file_path` is used for qualified names and node IDs (not for I/O). |
| 78 | /// `source` is the Rust source code to parse. |
| 79 | pub fn extract(file_path: &str, source: &str) -> ExtractionResult { |
| 80 | let start = Instant::now(); |
| 81 | let mut state = ExtractionState::new(file_path, source); |
| 82 | |
| 83 | let tree = match Self::parse_source(source) { |
| 84 | Ok(tree) => tree, |
| 85 | Err(msg) => { |
| 86 | state.errors.push(msg); |
| 87 | return Self::build_result(state, start); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | // Create the File root node. |
| 92 | let file_node = Node { |
| 93 | id: generate_node_id(file_path, &NodeKind::File, file_path, 0), |
| 94 | kind: NodeKind::File, |
| 95 | name: file_path.to_string(), |
| 96 | qualified_name: file_path.to_string(), |
| 97 | file_path: file_path.to_string(), |
| 98 | start_line: 0, |
| 99 | attrs_start_line: 0, |
| 100 | end_line: source.lines().count().saturating_sub(1) as u32, |
| 101 | start_column: 0, |
| 102 | end_column: 0, |
| 103 | signature: None, |
| 104 | docstring: None, |
| 105 | visibility: Visibility::Pub, |
| 106 | is_async: false, |
| 107 | branches: 0, |
| 108 | loops: 0, |
| 109 | returns: 0, |
| 110 | max_nesting: 0, |
| 111 | unsafe_blocks: 0, |
| 112 | unchecked_calls: 0, |
| 113 | assertions: 0, |
| 114 | updated_at: state.timestamp, |
| 115 | parent_id: None, |
| 116 | }; |
| 117 | let file_node_id = file_node.id.clone(); |
| 118 | state.nodes.push(file_node); |
| 119 | state.node_stack.push((file_path.to_string(), file_node_id)); |
| 120 | |
| 121 | // Walk the AST. |
| 122 | let root = tree.root_node(); |
| 123 | Self::visit_children(&mut state, root); |
| 124 | |
| 125 | state.node_stack.pop(); |
| 126 | |
| 127 | Self::build_result(state, start) |
| 128 | } |
| 129 | |
| 130 | /// Parse source code into a tree-sitter AST. |
| 131 | fn parse_source(source: &str) -> Result<Tree, String> { |