Skip to main content

the_code_graph_cli/commands/
flows.rs

1use domain::error::Result;
2use domain::model::{FlowConfig, FlowStats};
3use domain::use_cases::flow::FlowUseCase;
4
5use crate::commands::helpers::open_graph;
6use crate::commands::FlowsArgs;
7use crate::config::load_config;
8use crate::output::{print, OutputFormat};
9
10pub fn run_flows(args: &FlowsArgs, output_format: OutputFormat) -> Result<()> {
11    let (store, root) = open_graph()?;
12    let config = load_config(&root)?;
13
14    let mut flow_config = FlowConfig {
15        max_depth: args.depth,
16        ..FlowConfig::default()
17    };
18    if let Some(fc) = &config.flows {
19        if let Some(extra) = &fc.extra_entry_points {
20            flow_config.extra_entry_points = extra.clone();
21        }
22        if let Some(excluded) = &fc.excluded_entry_points {
23            flow_config.excluded_entry_points = excluded.clone();
24        }
25    }
26
27    let uc = FlowUseCase::new(store);
28
29    if args.rank {
30        let mut scores = uc.criticality()?;
31        scores.truncate(args.limit);
32        print(&scores, output_format);
33    } else if let Some(ref symbol) = args.symbol {
34        let flows = uc.flows_through(symbol, &flow_config)?;
35        let analysis = domain::model::FlowAnalysis {
36            entry_points: vec![],
37            criticality: vec![],
38            stats: FlowStats {
39                total_entry_points: 0,
40                total_flows: flows.len(),
41                max_depth: flows.iter().map(|f| f.depth).max().unwrap_or(0),
42                avg_depth: if flows.is_empty() {
43                    0.0
44                } else {
45                    flows.iter().map(|f| f.depth as f64).sum::<f64>() / flows.len() as f64
46                },
47            },
48            flows,
49        };
50        print(&analysis, output_format);
51    } else {
52        let mut analysis = uc.analyze(&flow_config)?;
53        analysis.flows.truncate(args.limit);
54        print(&analysis, output_format);
55    }
56    Ok(())
57}