tree_fmt/formatters/
format_trait.rs

1//! Unified format trait for all formatters
2
3use crate::TableView;
4
5/// Error type for formatting operations
6#[ derive( Debug ) ]
7pub enum FormatError
8{
9  /// Serialization error (requires `serde_support` feature)
10  #[ cfg( feature = "serde_support" ) ]
11  Serialization( String ),
12  /// Invalid or malformed data
13  InvalidData( String ),
14  /// Unsupported operation for this formatter
15  UnsupportedOperation( String ),
16}
17
18impl std::fmt::Display for FormatError
19{
20  fn fmt( &self, f : &mut std::fmt::Formatter< '_ > ) -> std::fmt::Result
21  {
22    match self
23    {
24      #[ cfg( feature = "serde_support" ) ]
25      Self::Serialization( msg ) => write!( f, "Serialization error: {msg}" ),
26      Self::InvalidData( msg ) => write!( f, "Invalid data: {msg}" ),
27      Self::UnsupportedOperation( msg ) => write!( f, "Unsupported operation: {msg}" ),
28    }
29  }
30}
31
32impl std::error::Error for FormatError {}
33
34/// Unified formatting interface for all output formats
35///
36/// All formatters (table, json, yaml, text, etc.) implement this trait
37/// to provide a consistent interface for formatting `TableView` data.
38///
39/// # Examples
40///
41/// ```
42/// # #[cfg(feature = "format_table")]
43/// # {
44/// use tree_fmt::{ RowBuilder, TableFormatter, TableConfig, Format };
45///
46/// let view = RowBuilder::new( vec![ "Name".into(), "Age".into() ] )
47///   .add_row( vec![ "Alice".into(), "30".into() ] )
48///   .build_view();
49///
50/// let formatter = TableFormatter::with_config( TableConfig::plain() );
51/// let output = Format::format( &formatter, &view ).unwrap();
52/// assert!( output.contains( "Name" ) );
53/// # }
54/// ```
55pub trait Format
56{
57  /// Format the table view to a string
58  ///
59  /// # Errors
60  ///
61  /// Returns `FormatError` if formatting fails due to invalid data,
62  /// serialization errors, or unsupported operations.
63  fn format( &self, data : &TableView ) -> Result< String, FormatError >;
64}