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

Function extract

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

Source from the content-addressed store, hash-verified

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

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
parseMethod · 0.80
bytesMethod · 0.80
nextMethod · 0.80
push_nodeMethod · 0.80
pushMethod · 0.80
lenMethod · 0.80
into_vecMethod · 0.80
line_startsFunction · 0.70

Tested by

no test coverage detected