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 crate::analysis::AnalysisResult;
7use crate::cli::cli_error::CliResult;
8use crate::cli::command_outcome::CommandOutcome;
9use crate::output::report::Report;
10use crate::output::reporter::Reporter;
11
12/// `report`: the summary followed by every group that survived filtering.
13///
14/// What the document looks like is the reporter's decision, not this one's --
15/// text writes section after section, JSON writes one object with the sections
16/// inside it. A command that made that choice itself could only ever make the
17/// text one.
18pub struct ReportCommand<'a> {
19    result: &'a AnalysisResult,
20    reporter: &'a dyn Reporter,
21}
22
23impl<'a> ReportCommand<'a> {
24    #[must_use]
25    pub const fn new(result: &'a AnalysisResult, reporter: &'a dyn Reporter) -> Self {
26        Self { result, reporter }
27    }
28
29    /// Build the summary and the groups.
30    ///
31    /// # Errors
32    ///
33    /// Returns [`crate::cli::cli_error::CliError::Io`] if the reporter fails.
34    pub fn run(&self) -> CliResult<CommandOutcome> {
35        let text = self.reporter.report(&Report {
36            stats: &self.result.stats,
37            exact: &self.result.exact_groups,
38            near: &self.result.near_groups,
39            sub_exact: &self.result.sub_exact_groups,
40            sub_near: &self.result.sub_near_groups,
41        })?;
42        Ok(CommandOutcome::succeeded(text))
43    }
44}