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