luff/printer/
mod.rs

1//! File printing implementations
2
3mod colors;
4mod markdown;
5mod tree;
6
7use crate::{cli::OutputFormat, error::Result, walker::WalkerEntry};
8use std::path::PathBuf;
9
10// Re-exports
11pub use colors::Colors;
12pub use markdown::MarkdownPrinter;
13pub use tree::{TreePrinter, format_tree};
14
15/// Type-safe wrapper for pattern filtering behavior
16///
17/// This newtype eliminates boolean blindness and makes the intent explicit
18/// at call sites. Instead of passing `true` or `false`, callers use
19/// `SkipPatterns::ENABLED` or `SkipPatterns::DISABLED`.
20///
21/// # Examples
22///
23/// ```
24/// use luff::printer::SkipPatterns;
25///
26/// // Clear intent at call site
27/// let patterns = SkipPatterns::ENABLED;
28/// assert!(patterns.should_skip());
29///
30/// let patterns = SkipPatterns::DISABLED;
31/// assert!(!patterns.should_skip());
32/// ```
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub struct SkipPatterns(bool);
35
36impl SkipPatterns {
37    /// Enable pattern-based filtering
38    ///
39    /// Files matching ignore patterns (binary extensions, etc.) will be
40    /// filtered out before processing.
41    pub const ENABLED: Self = Self(true);
42
43    /// Disable pattern-based filtering
44    ///
45    /// All files will be processed regardless of extension or pattern
46    /// matches. This is typically used when files are explicitly specified
47    /// via the `-f` flag.
48    pub const DISABLED: Self = Self(false);
49
50    /// Check if pattern filtering should be applied
51    ///
52    /// Returns `true` if files should be filtered based on ignore patterns,
53    /// `false` if all files should be processed.
54    #[must_use]
55    pub const fn should_skip(self) -> bool {
56        self.0
57    }
58}
59
60/// Options for configuring printer output
61#[derive(Debug, Clone)]
62pub struct PrinterOptions {
63    /// The output format to use
64    pub format: OutputFormat,
65    /// Root directory for calculating relative paths
66    pub root: PathBuf,
67    /// Whether to apply pattern-based filtering
68    pub skip_patterns: SkipPatterns,
69}
70
71/// Print a file entry using the appropriate printer
72///
73/// # Errors
74///
75/// Returns an error if:
76/// - File cannot be read
77/// - File is not valid UTF-8 (for text formats)
78/// - Output cannot be written to stdout
79pub fn print_file(entry: &WalkerEntry, options: &PrinterOptions) -> Result<()> {
80    match options.format {
81        OutputFormat::Markdown => {
82            MarkdownPrinter::print(entry, &options.root, options.skip_patterns)
83        }
84        OutputFormat::Tree => TreePrinter::print(entry, &options.root),
85    }
86}