Skip to main content

dry4rust/cli/
report_command.rs

1// Copyright (c) 2026 Matjaz Domen Pecan
2// Copyright 2026 Umberto Gotti <umberto.gotti@umbertogotti.dev>
3// Licensed under the MIT License
4// SPDX-License-Identifier: MIT
5
6use std::io::Write;
7
8use crate::analysis::AnalysisResult;
9use crate::cli::cli_error::CliResult;
10use crate::output::report::Report;
11use crate::output::reporter::Reporter;
12
13/// `report`: the summary followed by every group that survived filtering.
14///
15/// What the document looks like is the reporter's decision, not this one's --
16/// text writes section after section, JSON writes one object with the sections
17/// inside it. A command that made that choice itself could only ever make the
18/// text one.
19pub struct ReportCommand<'a> {
20    result: &'a AnalysisResult,
21    reporter: &'a dyn Reporter,
22}
23
24impl<'a> ReportCommand<'a> {
25    #[must_use]
26    pub const fn new(result: &'a AnalysisResult, reporter: &'a dyn Reporter) -> Self {
27        Self { result, reporter }
28    }
29
30    /// Write the summary and the groups.
31    ///
32    /// # Errors
33    ///
34    /// Returns [`crate::cli::cli_error::CliError::Io`] if the writer fails.
35    pub fn run(&self, writer: &mut impl Write) -> CliResult {
36        self.reporter.report(
37            &Report {
38                stats: &self.result.stats,
39                exact: &self.result.exact_groups,
40                near: &self.result.near_groups,
41                sub_exact: &self.result.sub_exact_groups,
42                sub_near: &self.result.sub_near_groups,
43            },
44            writer,
45        )?;
46        Ok(())
47    }
48}