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

Function extract

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 14

grammar_forFunction · 0.85
file_node_idFunction · 0.85
node_kind_indexFunction · 0.85
build_metaFunction · 0.85
putMethod · 0.80
push_nodeMethod · 0.80
lenMethod · 0.80
into_vecMethod · 0.80
line_startsFunction · 0.70
unwrapMethod · 0.60
setMethod · 0.45
visit_nodeMethod · 0.45

Tested by

no test coverage detected