1 | //! Builder module provides a [`Builder`] type which helps building |
2 | //! a [`Table`] dynamically. |
3 | //! |
4 | //! It also contains [`IndexBuilder`] which can help to build a table with index. |
5 | //! |
6 | //! # Examples |
7 | //! |
8 | //! Here's an example of [`IndexBuilder`] usage |
9 | //! |
10 | #![cfg_attr (feature = "derive" , doc = "```" )] |
11 | #![cfg_attr (not(feature = "derive" ), doc = "```ignore" )] |
12 | //! use tabled::{Table, Tabled, settings::Style}; |
13 | //! |
14 | //! #[derive(Tabled)] |
15 | //! struct Mission { |
16 | //! name: &'static str, |
17 | //! #[tabled(inline)] |
18 | //! status: Status, |
19 | //! } |
20 | //! |
21 | //! #[derive(Tabled)] |
22 | //! enum Status { |
23 | //! Complete, |
24 | //! Started, |
25 | //! Ready, |
26 | //! Unknown, |
27 | //! } |
28 | //! |
29 | //! let data = [ |
30 | //! Mission { name: "Algebra" , status: Status::Unknown }, |
31 | //! Mission { name: "Apolo" , status: Status::Complete }, |
32 | //! ]; |
33 | //! |
34 | //! let mut builder = Table::builder(&data) |
35 | //! .index() |
36 | //! .column(0) |
37 | //! .name(None) |
38 | //! .transpose(); |
39 | //! |
40 | //! let mut table = builder.build(); |
41 | //! table.with(Style::modern()); |
42 | //! |
43 | //! println!("{}" , table); |
44 | //! |
45 | //! assert_eq!( |
46 | //! table.to_string(), |
47 | //! concat!( |
48 | //! "┌──────────┬─────────┬───────┐ \n" , |
49 | //! "│ │ Algebra │ Apolo │ \n" , |
50 | //! "├──────────┼─────────┼───────┤ \n" , |
51 | //! "│ Complete │ │ + │ \n" , |
52 | //! "├──────────┼─────────┼───────┤ \n" , |
53 | //! "│ Started │ │ │ \n" , |
54 | //! "├──────────┼─────────┼───────┤ \n" , |
55 | //! "│ Ready │ │ │ \n" , |
56 | //! "├──────────┼─────────┼───────┤ \n" , |
57 | //! "│ Unknown │ + │ │ \n" , |
58 | //! "└──────────┴─────────┴───────┘" , |
59 | //! ), |
60 | //! ) |
61 | //! ``` |
62 | //! |
63 | //! Example when we don't want to show empty data of enum where not all variants are used. |
64 | //! |
65 | #![cfg_attr (feature = "derive" , doc = "```" )] |
66 | #![cfg_attr (not(feature = "derive" ), doc = "```ignore" )] |
67 | //! use tabled::{Table, Tabled, settings::Style}; |
68 | //! |
69 | //! #[derive(Tabled)] |
70 | //! enum Status { |
71 | //! #[tabled(inline)] |
72 | //! Complete { |
73 | //! started_timestamp: usize, |
74 | //! finihsed_timestamp: usize, |
75 | //! }, |
76 | //! #[tabled(inline)] |
77 | //! Started { |
78 | //! timestamp: usize, |
79 | //! }, |
80 | //! Ready, |
81 | //! Unknown, |
82 | //! } |
83 | //! |
84 | //! let data = [ |
85 | //! Status::Unknown, |
86 | //! Status::Complete { started_timestamp: 123, finihsed_timestamp: 234 }, |
87 | //! ]; |
88 | //! |
89 | //! let mut builder = Table::builder(&data); |
90 | //! builder.clean(); |
91 | //! |
92 | //! let table = builder.build() |
93 | //! .with(Style::modern()) |
94 | //! .to_string(); |
95 | //! |
96 | //! println!("{}" , table); |
97 | //! |
98 | //! assert_eq!( |
99 | //! table, |
100 | //! concat!( |
101 | //! "┌───────────────────┬────────────────────┬─────────┐ \n" , |
102 | //! "│ started_timestamp │ finihsed_timestamp │ Unknown │ \n" , |
103 | //! "├───────────────────┼────────────────────┼─────────┤ \n" , |
104 | //! "│ │ │ + │ \n" , |
105 | //! "├───────────────────┼────────────────────┼─────────┤ \n" , |
106 | //! "│ 123 │ 234 │ │ \n" , |
107 | //! "└───────────────────┴────────────────────┴─────────┘" , |
108 | //! ), |
109 | //! ) |
110 | //! ``` |
111 | //! |
112 | //! [`Table`]: crate::Table |
113 | |
114 | mod index_builder; |
115 | mod table_builder; |
116 | |
117 | pub use index_builder::IndexBuilder; |
118 | pub use table_builder::Builder; |
119 | |