Skip to main content

codelens_core/output/
format.rs

1//! Output format trait definition.
2
3use std::io::Write;
4
5use crate::analyzer::stats::AnalysisResult;
6use crate::config::SortBy;
7use crate::error::Result;
8use crate::insight::estimation::{EstimationComparison, EstimationReport};
9use crate::insight::health::HealthReport;
10use crate::insight::hotspot::HotspotReport;
11use crate::insight::trend::TrendReport;
12
13/// Unified report type for all output formatters.
14#[derive(Debug, Clone)]
15pub enum Report {
16    Analysis(AnalysisResult),
17    Health(HealthReport),
18    Hotspot(HotspotReport),
19    Trend(TrendReport),
20    Estimation(EstimationReport),
21    EstimationComparison(EstimationComparison),
22}
23
24/// Trait for output formatters.
25pub trait OutputFormat: Send + Sync {
26    /// Get the format name.
27    fn name(&self) -> &'static str;
28
29    /// Get the file extension.
30    fn extension(&self) -> &'static str;
31
32    /// Write the report to the writer.
33    fn write(&self, report: &Report, options: &OutputOptions, writer: &mut dyn Write)
34        -> Result<()>;
35}
36
37/// Output options.
38#[derive(Debug, Clone)]
39pub struct OutputOptions {
40    /// Show only summary.
41    pub summary_only: bool,
42    /// Sort order.
43    pub sort_by: SortBy,
44    /// Limit to top N results.
45    pub top_n: Option<usize>,
46    /// Colorize output (for console).
47    pub colorize: bool,
48    /// Show git information.
49    pub show_git_info: bool,
50}
51
52impl Default for OutputOptions {
53    fn default() -> Self {
54        Self {
55            summary_only: false,
56            sort_by: SortBy::Lines,
57            top_n: None,
58            colorize: true,
59            show_git_info: false,
60        }
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_output_options_default() {
70        let options = OutputOptions::default();
71        assert!(!options.summary_only);
72        assert!(matches!(options.sort_by, SortBy::Lines));
73        assert!(options.top_n.is_none());
74        assert!(options.colorize);
75        assert!(!options.show_git_info);
76    }
77
78    #[test]
79    fn test_output_options_custom() {
80        let options = OutputOptions {
81            summary_only: true,
82            sort_by: SortBy::Code,
83            top_n: Some(10),
84            colorize: false,
85            show_git_info: true,
86        };
87        assert!(options.summary_only);
88        assert!(matches!(options.sort_by, SortBy::Code));
89        assert_eq!(options.top_n, Some(10));
90        assert!(!options.colorize);
91        assert!(options.show_git_info);
92    }
93}