| 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 table = Table::new(data) |
| 90 | //! .with(Style::modern()) |
| 91 | //! .to_string(); |
| 92 | //! |
| 93 | //! println!("{}" , table); |
| 94 | //! |
| 95 | //! assert_eq!( |
| 96 | //! table, |
| 97 | //! concat!( |
| 98 | //! "┌───────────────────┬────────────────────┬───────────┬───────┬─────────┐ \n" , |
| 99 | //! "│ started_timestamp │ finihsed_timestamp │ timestamp │ Ready │ Unknown │ \n" , |
| 100 | //! "├───────────────────┼────────────────────┼───────────┼───────┼─────────┤ \n" , |
| 101 | //! "│ │ │ │ │ + │ \n" , |
| 102 | //! "├───────────────────┼────────────────────┼───────────┼───────┼─────────┤ \n" , |
| 103 | //! "│ 123 │ 234 │ │ │ │ \n" , |
| 104 | //! "└───────────────────┴────────────────────┴───────────┴───────┴─────────┘" , |
| 105 | //! ), |
| 106 | //! ) |
| 107 | //! ``` |
| 108 | //! |
| 109 | //! [`Table`]: crate::Table |
| 110 | |
| 111 | mod index_builder; |
| 112 | mod table_builder; |
| 113 | |
| 114 | pub use index_builder::IndexBuilder; |
| 115 | pub use table_builder::Builder; |
| 116 | |