arborist_cli/output/
json.rs1use std::io::{self, Write};
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 out = stdout.lock();
11 let json = serde_json::to_string_pretty(reports)
12 .map_err(|e| ArboristError::Analysis(e.to_string()))?;
13 writeln!(out, "{json}")?;
14 Ok(())
15}
16
17pub fn write_flat(flat: &[FlatFunction]) -> Result<(), ArboristError> {
18 use serde::Serialize;
19
20 #[derive(Serialize)]
21 struct FlatJson<'a> {
22 name: &'a str,
23 file: &'a str,
24 language: &'a str,
25 line_start: usize,
26 line_end: usize,
27 cognitive: u64,
28 cyclomatic: u64,
29 sloc: u64,
30 }
31
32 let items: Vec<FlatJson<'_>> = flat
33 .iter()
34 .map(|f| FlatJson {
35 name: &f.name,
36 file: &f.file_path,
37 language: &f.language,
38 line_start: f.line_start,
39 line_end: f.line_end,
40 cognitive: f.cognitive,
41 cyclomatic: f.cyclomatic,
42 sloc: f.sloc,
43 })
44 .collect();
45
46 let stdout = io::stdout();
47 let mut out = stdout.lock();
48 let json =
49 serde_json::to_string_pretty(&items).map_err(|e| ArboristError::Analysis(e.to_string()))?;
50 writeln!(out, "{json}")?;
51 Ok(())
52}