tabled/derive/display/
mod.rs

1//! Module contains a list of helpers for work with display.
2
3use core::fmt::Debug;
4
5/// A function which is usefull in conjuntion with
6/// `#[tabled(display)]` and `#[tabled(display)]`.
7///
8/// It can be used with a [`prim@bool`] type.
9/// You must provide 2 argumnts which will be display
10/// for true and false case correspondingly.
11///
12/// # Example
13///
14/// ```
15/// use tabled::{Tabled, Table, assert::assert_table, derive::display};
16///
17/// #[derive(Tabled)]
18/// #[tabled(display(bool, "display::bool", "Got", 0))]
19/// pub struct State {
20///     name: &'static str,
21///     working: bool,
22///     closed: bool,
23/// }
24///
25/// let data = vec![
26///     State { name: "work", working: true, closed: false },
27///     State { name: "stop", working: false, closed: false },
28///     State { name: "closed", working: false, closed: true },
29/// ];
30///
31/// let table = Table::new(data);
32///
33/// assert_table!(
34///     table,
35///     "+--------+---------+--------+"
36///     "| name   | working | closed |"
37///     "+--------+---------+--------+"
38///     "| work   | Got     | 0      |"
39///     "+--------+---------+--------+"
40///     "| stop   | 0       | 0      |"
41///     "+--------+---------+--------+"
42///     "| closed | 0       | Got    |"
43///     "+--------+---------+--------+"
44/// );
45/// ```
46pub fn bool<T, F>(value: &bool, on_true: T, on_false: F) -> String
47where
48    T: ToString,
49    F: ToString,
50{
51    match value {
52        true => on_true.to_string(),
53        false => on_false.to_string(),
54    }
55}
56
57/// A function which is usefull in conjuntion with
58/// `#[tabled(display)]` and `#[tabled(display)]`.
59///
60/// It can be used with any [`Option`] type.
61/// You must provide a second argument which represents a value be printed in case of [`None`].
62///
63/// # Example
64///
65/// ```
66/// use tabled::{Tabled, Table, derive::display};
67/// use tabled::assert::assert_table;
68///
69/// #[derive(Tabled)]
70/// #[tabled(display(Option, "display::option", "Unknown"))]
71/// pub struct ZKP<'a> {
72///     application: &'a str,
73///     state: Option<&'a str>
74/// }
75///
76/// let data = vec![
77///     ZKP { application: "Decentralized Identity", state: Some("Proved") },
78///     ZKP { application: "Voting Systems", state: Some("Investigation") },
79///     ZKP { application: "Privacy-Preserving Transactions", state: None },
80/// ];
81///
82/// let table = Table::new(data);
83///
84/// assert_table!(
85///     table,
86///     "+---------------------------------+---------------+"
87///     "| application                     | state         |"
88///     "+---------------------------------+---------------+"
89///     "| Decentralized Identity          | Proved        |"
90///     "+---------------------------------+---------------+"
91///     "| Voting Systems                  | Investigation |"
92///     "+---------------------------------+---------------+"
93///     "| Privacy-Preserving Transactions | Unknown       |"
94///     "+---------------------------------+---------------+"
95/// );
96/// ```
97pub fn option<T>(value: &Option<T>, default: &str) -> String
98where
99    T: ToString,
100{
101    match value {
102        Some(val) => val.to_string(),
103        None => default.to_string(),
104    }
105}
106
107/// A function which is usefull in conjuntion with
108/// `#[tabled(display)]` and `#[tabled(display)]`.
109///
110/// It can be used with any type which implements a [`Debug`].
111/// So rather then [`std::fmt::Display`] usage we will be using a debug implementation.
112///
113/// ```
114/// use tabled::{Tabled, Table, derive::display};
115/// use tabled::assert::assert_table;
116///
117/// #[derive(Tabled)]
118/// #[tabled(display(Option, "display::debug"))]
119/// pub struct ZKP<'a> {
120///     application: &'a str,
121///     state: Option<&'a str>
122/// }
123///
124/// let data = vec![
125///     ZKP { application: "Decentralized Identity", state: Some("Proved") },
126///     ZKP { application: "Voting Systems", state: Some("Investigation") },
127///     ZKP { application: "Privacy-Preserving Transactions", state: None },
128/// ];
129///
130/// let table = Table::new(data);
131///
132/// assert_table!(
133///     table,
134///     r#"+---------------------------------+-----------------------+"#
135///     r#"| application                     | state                 |"#
136///     r#"+---------------------------------+-----------------------+"#
137///     r#"| Decentralized Identity          | Some("Proved")        |"#
138///     r#"+---------------------------------+-----------------------+"#
139///     r#"| Voting Systems                  | Some("Investigation") |"#
140///     r#"+---------------------------------+-----------------------+"#
141///     r#"| Privacy-Preserving Transactions | None                  |"#
142///     r#"+---------------------------------+-----------------------+"#
143/// );
144/// ```
145pub fn debug<T>(value: &T) -> String
146where
147    T: Debug,
148{
149    format!("{:?}", value)
150}
151
152/// A function which is usefull in conjuntion with
153/// `#[tabled(display)]` and `#[tabled(display)]`.
154///
155/// It just returns an empty string.
156///
157/// ```
158/// use tabled::{Tabled, Table, derive::display};
159/// use tabled::assert::assert_table;
160///
161/// #[derive(Tabled)]
162/// pub struct ZKP<'a> {
163///     application: &'a str,
164///     #[tabled(display = "display::empty")]
165///     state: Option<&'a str>
166/// }
167///
168/// let data = vec![
169///     ZKP { application: "Decentralized Identity", state: Some("Proved") },
170///     ZKP { application: "Voting Systems", state: Some("Investigation") },
171///     ZKP { application: "Privacy-Preserving Transactions", state: None },
172/// ];
173///
174/// let table = Table::new(data);
175///
176/// assert_table!(
177///     table,
178///     r#"+---------------------------------+-------+"#
179///     r#"| application                     | state |"#
180///     r#"+---------------------------------+-------+"#
181///     r#"| Decentralized Identity          |       |"#
182///     r#"+---------------------------------+-------+"#
183///     r#"| Voting Systems                  |       |"#
184///     r#"+---------------------------------+-------+"#
185///     r#"| Privacy-Preserving Transactions |       |"#
186///     r#"+---------------------------------+-------+"#
187/// );
188/// ```
189pub fn empty<T>(_value: &T) -> String {
190    String::new()
191}
192
193/// A function which truncates value to the given width.
194///
195/// # Example
196///
197/// ```
198/// use tabled::{Tabled, Table, derive::display};
199/// use tabled::assert::assert_table;
200///
201/// #[derive(Tabled)]
202/// pub struct ZKP<'a> {
203///     #[tabled(display("display::truncate", 5))]
204///     application: &'a str,
205///     state: &'a str,
206/// }
207///
208/// let data = vec![
209///     ZKP { application: "Decentralized Identity", state: "Proved" },
210///     ZKP { application: "Voting Systems", state: "Investigation" },
211///     ZKP { application: "Privacy-Preserving Transactions", state: "" },
212/// ];
213///
214/// let table = Table::new(data);
215///
216/// assert_table!(
217///     table,
218///     "+-------------+---------------+"
219///     "| application | state         |"
220///     "+-------------+---------------+"
221///     "| Decen       | Proved        |"
222///     "+-------------+---------------+"
223///     "| Votin       | Investigation |"
224///     "+-------------+---------------+"
225///     "| Priva       |               |"
226///     "+-------------+---------------+"
227/// );
228/// ```
229#[cfg(feature = "std")]
230pub fn truncate<T>(value: &T, limit: usize) -> String
231where
232    T: ToString,
233{
234    let text = value.to_string();
235    let text = crate::settings::width::Truncate::truncate(&text, limit);
236    text.into_owned()
237}
238
239/// A function which wraps value to the given width.
240///
241/// # Example
242///
243/// ```
244/// use tabled::{Tabled, Table, derive::display};
245/// use tabled::assert::assert_table;
246///
247/// #[derive(Tabled)]
248/// pub struct ZKP<'a> {
249///     application: &'a str,
250///     #[tabled(display("display::wrap", 5))]
251///     state: &'a str,
252/// }
253///
254/// let data = vec![
255///     ZKP { application: "Decentralized Identity", state: "Proved" },
256///     ZKP { application: "Voting Systems", state: "Investigation" },
257///     ZKP { application: "Privacy-Preserving Transactions", state: "" },
258/// ];
259///
260/// let table = Table::new(data);
261///
262/// assert_table!(
263///     table,
264///     "+---------------------------------+-------+"
265///     "| application                     | state |"
266///     "+---------------------------------+-------+"
267///     "| Decentralized Identity          | Prove |"
268///     "|                                 | d     |"
269///     "+---------------------------------+-------+"
270///     "| Voting Systems                  | Inves |"
271///     "|                                 | tigat |"
272///     "|                                 | ion   |"
273///     "+---------------------------------+-------+"
274///     "| Privacy-Preserving Transactions |       |"
275///     "+---------------------------------+-------+"
276/// );
277/// ```
278#[cfg(feature = "std")]
279pub fn wrap<T>(value: &T, limit: usize) -> String
280where
281    T: ToString,
282{
283    let text = value.to_string();
284    crate::settings::width::Wrap::wrap(&text, limit, false)
285}