MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extract

Function extract

codegraph-kernel/src/rustlang.rs:127–201  ·  view source on GitHub ↗
(file_path: &str, source: &str)

Source from the content-addressed store, hash-verified

125}
126
127pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
128 let grammar = crate::langs::grammar_for("rust").ok_or("no rust grammar")?;
129 let t0 = std::time::Instant::now();
130 let mut parser = Parser::new();
131 parser
132 .set_language(&grammar)
133 .map_err(|e| format!("set_language(rust) failed: {e}"))?;
134 let tree = parser
135 .parse(source, None)
136 .ok_or_else(|| "parser returned null tree".to_string())?;
137 if tree.root_node().has_error() {
138 return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
139 }
140
141 let mut w = Walker {
142 src: source,
143 file_path,
144 line_starts: util::line_starts(source),
145 arena: Arena::default(),
146 tables: Tables::default(),
147 stack: Vec::new(),
148 nodes_meta: Vec::new(),
149 node_ids: Vec::new(),
150 defined_fn_names: HashSet::new(),
151 imported_names: HashSet::new(),
152 fn_ref_cands: Vec::new(),
153 fs_values: HashMap::new(),
154 fs_value_counts: HashMap::new(),
155 value_scopes: Vec::new(),
156 };
157
158 let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
159 let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
160 let mut flags = BoolFlags::default();
161 flags.set(FLAG_IS_EXPORTED, false);
162 let file_id = w.arena.put(&ids::file_node_id(file_path));
163 let name_ref = w.arena.put(base_name);
164 let qn_ref = w.arena.put(file_path);
165 w.tables.push_node(&NodeRow {
166 kind: node_kind_index("file").unwrap(),
167 visibility: 0,
168 flags,
169 start_line: 1,
170 end_line: line_count,
171 start_column: 0,
172 end_column: 0,
173 name: name_ref,
174 qualified_name: qn_ref,
175 id: file_id,
176 docstring: NONE_STR,
177 signature: NONE_STR,
178 decorators: NONE_STR,
179 type_parameters: NONE_STR,
180 return_type: NONE_STR,
181 extra_json: NONE_STR,
182 });
183 w.nodes_meta.push(NodeMeta { kind: "file", name: base_name.to_string() });
184 w.node_ids.push(ids::file_node_id(file_path));

Callers

nothing calls this directly

Calls 15

grammar_forFunction · 0.85
file_node_idFunction · 0.85
node_kind_indexFunction · 0.85
build_metaFunction · 0.85
bytesMethod · 0.80
nextMethod · 0.80
push_nodeMethod · 0.80
lenMethod · 0.80
into_vecMethod · 0.80
line_startsFunction · 0.70
putMethod · 0.65
unwrapMethod · 0.60

Tested by

no test coverage detected