Skip to main content

the_code_graph_cli/commands/
find.rs

1use domain::error::Result;
2use domain::model::EdgeKind;
3use domain::ports::GraphStore;
4use domain::use_cases::query::QueryUseCase;
5
6use crate::commands::helpers::open_graph;
7use crate::commands::FindArgs;
8use crate::output::{print, FindResult, OutputFormat};
9
10pub fn run_find(args: &FindArgs, output_format: OutputFormat) -> Result<()> {
11    let (store, _root) = open_graph()?;
12    let uc = QueryUseCase::new(store.clone(), store.clone());
13    let symbols = uc.find(&args.pattern)?;
14
15    let mut results = Vec::new();
16    for symbol in symbols {
17        let callers: Vec<String> = store
18            .get_edges_to(&symbol.qualified_name)?
19            .into_iter()
20            .filter(|e| e.kind == EdgeKind::Calls)
21            .map(|e| e.source)
22            .collect();
23        let callees: Vec<String> = store
24            .get_edges_from(&symbol.qualified_name)?
25            .into_iter()
26            .filter(|e| e.kind == EdgeKind::Calls)
27            .map(|e| e.target)
28            .collect();
29        let tested_by: Vec<String> = store
30            .get_edges_to(&symbol.qualified_name)?
31            .into_iter()
32            .filter(|e| e.kind == EdgeKind::TestedBy)
33            .map(|e| e.source)
34            .collect();
35        results.push(FindResult {
36            symbol,
37            callers,
38            callees,
39            tested_by,
40        });
41    }
42
43    print(&results, output_format);
44    Ok(())
45}