Skip to main content

arborist_cli/output/
csv_output.rs

1use std::io;
2
3use arborist::FileReport;
4
5use crate::analysis::FlatFunction;
6use crate::error::ArboristError;
7
8pub fn write_reports(reports: &[FileReport]) -> Result<(), ArboristError> {
9    let stdout = io::stdout();
10    let mut wtr = csv::Writer::from_writer(stdout.lock());
11
12    wtr.write_record([
13        "file",
14        "language",
15        "function",
16        "line_start",
17        "line_end",
18        "cognitive",
19        "cyclomatic",
20        "sloc",
21    ])
22    .map_err(|e| ArboristError::Analysis(e.to_string()))?;
23
24    for report in reports {
25        let lang = report.language.to_string();
26        for func in &report.functions {
27            wtr.write_record([
28                &report.path,
29                &lang,
30                &func.name,
31                &func.start_line.to_string(),
32                &func.end_line.to_string(),
33                &func.cognitive.to_string(),
34                &func.cyclomatic.to_string(),
35                &func.sloc.to_string(),
36            ])
37            .map_err(|e| ArboristError::Analysis(e.to_string()))?;
38        }
39    }
40
41    wtr.flush()?;
42    Ok(())
43}
44
45pub fn write_flat(flat: &[FlatFunction]) -> Result<(), ArboristError> {
46    let stdout = io::stdout();
47    let mut wtr = csv::Writer::from_writer(stdout.lock());
48
49    wtr.write_record([
50        "file",
51        "language",
52        "function",
53        "line_start",
54        "line_end",
55        "cognitive",
56        "cyclomatic",
57        "sloc",
58    ])
59    .map_err(|e| ArboristError::Analysis(e.to_string()))?;
60
61    for func in flat {
62        wtr.write_record([
63            &func.file_path,
64            &func.language,
65            &func.name,
66            &func.line_start.to_string(),
67            &func.line_end.to_string(),
68            &func.cognitive.to_string(),
69            &func.cyclomatic.to_string(),
70            &func.sloc.to_string(),
71        ])
72        .map_err(|e| ArboristError::Analysis(e.to_string()))?;
73    }
74
75    wtr.flush()?;
76    Ok(())
77}