context_creator/formatters/
mod.rs

1//! Output formatters for different file formats
2
3use crate::cli::OutputFormat;
4use crate::core::cache::FileCache;
5use crate::core::context_builder::ContextOptions;
6use crate::core::walker::FileInfo;
7use anyhow::Result;
8use std::sync::Arc;
9
10pub mod markdown;
11pub mod paths;
12pub mod plain;
13pub mod xml;
14
15/// Data passed to formatters for rendering
16pub struct DigestData<'a> {
17    pub files: &'a [FileInfo],
18    pub options: &'a ContextOptions,
19    pub cache: &'a Arc<FileCache>,
20    pub base_directory: &'a str,
21}
22
23/// Trait for digest formatters
24pub trait DigestFormatter {
25    /// Render the document header
26    fn render_header(&mut self, data: &DigestData) -> Result<()>;
27
28    /// Render statistics section
29    fn render_statistics(&mut self, data: &DigestData) -> Result<()>;
30
31    /// Render file tree structure
32    fn render_file_tree(&mut self, data: &DigestData) -> Result<()>;
33
34    /// Render table of contents
35    fn render_toc(&mut self, data: &DigestData) -> Result<()>;
36
37    /// Render details for a single file
38    fn render_file_details(&mut self, file: &FileInfo, data: &DigestData) -> Result<()>;
39
40    /// Finalize and return the formatted output
41    fn finalize(self: Box<Self>) -> String;
42
43    /// Get the format name (for testing)
44    fn format_name(&self) -> &'static str;
45}
46
47/// Create a formatter based on the output format
48pub fn create_formatter(format: OutputFormat) -> Box<dyn DigestFormatter> {
49    match format {
50        OutputFormat::Markdown => Box::new(markdown::MarkdownFormatter::new()),
51        OutputFormat::Xml => Box::new(xml::XmlFormatter::new()),
52        OutputFormat::Plain => Box::new(plain::PlainFormatter::new()),
53        OutputFormat::Paths => Box::new(paths::PathsFormatter::new()),
54    }
55}