Skip to main content

heddle_cli_render/cli/render/
fsck.rs

1// SPDX-License-Identifier: Apache-2.0
2use anyhow::Result;
3use heddle_core::FsckReport;
4
5use crate::cli::{render::write_stdout, style};
6
7pub fn fsck_json(report: &FsckReport) -> Result<()> {
8    let mut text = serde_json::to_string(report)?;
9    text.push('\n');
10    write_stdout(&text)
11}
12
13pub fn fsck_text(report: &FsckReport) -> Result<()> {
14    let mut text = String::new();
15    if report.valid {
16        let counted = style::count(report.objects_checked, "object");
17        text.push_str(&format!(
18            "{} repository is valid ({counted} checked)\n",
19            style::ok_marker(),
20        ));
21        if report.git_projection_checked {
22            text.push_str(&format!(
23                "  {}\n",
24                style::field("Git projection", "mapping, notes, and checkout checked")
25            ));
26        }
27    } else {
28        text.push_str(&format!(
29            "{} repository has {}\n",
30            style::error_marker(),
31            style::count(report.errors.len(), "integrity error")
32        ));
33        for error in &report.errors {
34            if let Some(obj) = &error.object {
35                text.push_str(&format!(
36                    "  {} {} {}\n",
37                    style::error(&format!("[{}]", error.kind)),
38                    error.message,
39                    style::dim(&format!("({obj})"))
40                ));
41            } else {
42                text.push_str(&format!(
43                    "  {} {}\n",
44                    style::error(&format!("[{}]", error.kind)),
45                    error.message
46                ));
47            }
48        }
49    }
50    if let Some(target) = &report.repair_target {
51        let status = if report.repaired {
52            "repaired"
53        } else {
54            "no changes"
55        };
56        text.push_str(&format!(
57            "  {}\n",
58            style::field("Repair", &format!("{target}: {status}"))
59        ));
60        for repair in &report.repairs {
61            if repair.count > 0 || repair.repaired {
62                text.push_str(&format!(
63                    "    {} {} ({})\n",
64                    repair.name, repair.detail, repair.count
65                ));
66            }
67        }
68    }
69    for warning in &report.warnings {
70        text.push_str(&format!("{} {}\n", style::warn_marker(), warning));
71    }
72    write_stdout(&text)
73}