1use crate::error::Result;
2use crate::git::CliOps;
3use crate::read::summary::{build_summary, SummaryQuery};
4
5pub fn run(path: String, anchor: Option<String>, format: String, compact: bool) -> Result<()> {
6 let repo_dir = std::env::current_dir().map_err(|e| crate::error::ChronicleError::Io {
7 source: e,
8 location: snafu::Location::default(),
9 })?;
10 let git_ops = CliOps::new(repo_dir);
11
12 let query = SummaryQuery { file: path, anchor };
13
14 let result =
15 build_summary(&git_ops, &query).map_err(|e| crate::error::ChronicleError::Git {
16 source: e,
17 location: snafu::Location::default(),
18 })?;
19
20 let json = if compact {
21 let compact_out = serde_json::json!({
22 "units": result.units,
23 });
24 serde_json::to_string_pretty(&compact_out)
25 } else if format == "pretty" {
26 serde_json::to_string_pretty(&result)
27 } else {
28 serde_json::to_string(&result)
29 }
30 .map_err(|e| crate::error::ChronicleError::Json {
31 source: e,
32 location: snafu::Location::default(),
33 })?;
34
35 println!("{json}");
36 Ok(())
37}