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