format_tools/format/output_format.rs
1//! Customizable format of printing table.
2//!
3//! # Example of table format
4//!
5//! ```text
6//! sid | sname | gap
7//! -----+-------+-----
8//! 3 | Alice | 5
9//! 6 | Joe | 1
10//! 10 | Boris | 5
11//! ```
12//!
13//! # Example of list of rows format.
14//!
15//! ```text
16//! -[ RECORD 1 ]
17//! sid | 3
18//! sname | Alice
19//! gap | 5
20//! -[ RECORD 2 ]
21//! sid | 6
22//! sname | Joe
23//! gap | 1
24//! -[ RECORD 3 ]
25//! sid | 10
26//! sname | Boris
27//! gap | 5
28//! ```
29//!
30
31/// Define a private namespace for all its items.
32mod private
33{
34
35 use std::borrow::Cow;
36
37 use crate::*;
38 use print::
39 {
40 InputExtract,
41 Context,
42 };
43 use core::fmt;
44
45 //=
46
47 /// Trait for converting table extracts into string representations.
48 ///
49 /// `TableOutputFormat` defines the method for formatting table data
50 /// and writing it into a specified buffer, providing flexibility in
51 /// output style and format.
52 ///
53 pub trait TableOutputFormat
54 {
55 /// Formats the table extract and writes it into the destination buffer.
56 ///
57 /// # Parameters
58 /// - `x`: The `InputExtract` containing table data to be formatted.
59 /// - `c`: The `Context` holding the buffer and styles for formatting.
60 ///
61 /// # Returns
62 /// A `fmt::Result` indicating success or failure of the write operation.
63 fn extract_write< 'buf, 'data >
64 (
65 &self,
66 x : &InputExtract< 'data >,
67 c : &mut Context< 'buf >,
68 ) -> fmt::Result;
69 }
70
71 impl Default for &'static dyn TableOutputFormat
72 {
73 #[ inline( always ) ]
74 fn default() -> Self
75 {
76 super::table::Table::instance()
77 }
78 }
79
80 /// Print table, which is constructed with vectors and `Cow`s, with the
81 /// specified output formatter.
82 ///
83 /// This function is useful when you do not want to use `AsTable`, or implement `Fields`, and
84 /// other traits, but you just have string slices in vectors.
85 ///
86 /// `rows` should not contain header of the table, it will be automatically added if `has_header`
87 /// is true.
88 pub fn vector_table_write< 'data, 'context >
89 (
90 column_names : Vec< Cow< 'data, str > >,
91 has_header : bool,
92 rows : Vec< Vec< Cow< 'data, str > > >,
93 c : &mut Context< 'context >,
94 ) -> fmt::Result
95 {
96 InputExtract::extract_from_raw_table
97 (
98 column_names,
99 has_header,
100 rows,
101 c.printer.filter_col,
102 c.printer.filter_row,
103 | x |
104 {
105 c.printer.output_format.extract_write( x, c )
106 }
107 )
108 }
109
110}
111
112mod table;
113mod records;
114mod keys;
115
116#[ allow( unused_imports ) ]
117pub use own::*;
118
119/// Own namespace of the module.
120#[ allow( unused_imports ) ]
121pub mod own
122{
123 use super::*;
124 #[ doc( inline ) ]
125 pub use orphan::*;
126
127 #[ doc( inline ) ]
128 pub use
129 {
130 table::Table,
131 records::Records,
132 keys::Keys,
133 };
134
135 #[ doc( inline ) ]
136 pub use private::vector_table_write;
137
138}
139
140/// Orphan namespace of the module.
141#[ allow( unused_imports ) ]
142pub mod orphan
143{
144 use super::*;
145 #[ doc( inline ) ]
146 pub use exposed::*;
147}
148
149/// Exposed namespace of the module.
150#[ allow( unused_imports ) ]
151pub mod exposed
152{
153 use super::*;
154 pub use super::super::output_format;
155
156 #[ doc( inline ) ]
157 pub use private::TableOutputFormat;
158
159}
160
161/// Prelude to use essentials: `use my_module::prelude::*`.
162#[ allow( unused_imports ) ]
163pub mod prelude
164{
165 use super::*;
166}