Skip to main content

dry4rust/cli/
output_format.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::path::Path;
7
8use crate::output::json::JsonReporter;
9use crate::output::reporter::Reporter;
10use crate::output::text::TextReporter;
11
12/// Output format for CLI reports.
13#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, clap::ValueEnum)]
14pub enum OutputFormat {
15    #[default]
16    Text,
17    Json,
18}
19
20impl OutputFormat {
21    /// Build the reporter that writes this format.
22    ///
23    /// `root` is the base every reported path is shown relative to; `None`
24    /// reports absolute paths.
25    #[must_use]
26    pub fn reporter(self, root: Option<&Path>) -> Box<dyn Reporter> {
27        match self {
28            Self::Text => Box::new(TextReporter::new(root.map(Path::to_path_buf))),
29            Self::Json => Box::new(JsonReporter::new(root.map(Path::to_path_buf))),
30        }
31    }
32}