1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
//! An easy to use library for pretty print tables of Rust `struct`s and `enum`s.
//!
//! The library is based on a [Tabled] trait which is used to actually build tables.
//! It also provides an variate of dynamic settings for customization of a [Table].
//!
//! [Table] can be build from vast majority of Rust's standart types.
//!
//! ## Usage
//!
//! If you want to build a table for your custom type.
//! A starting point is to a anotate your type with `#[derive(Tabled)]`.
//!
//! Then you can create `Table::new` to create a table;
//!
//! ```rust
//! use tabled::{Tabled, Table};
//!
//! #[derive(Tabled)]
//! struct Language {
//! name: &'static str,
//! designed_by: &'static str,
//! invented_year: usize,
//! }
//!
//! let languages = vec![
//! Language{
//! name: "C",
//! designed_by: "Dennis Ritchie",
//! invented_year: 1972
//! },
//! Language{
//! name: "Rust",
//! designed_by: "Graydon Hoare",
//! invented_year: 2010
//! },
//! Language{
//! name: "Go",
//! designed_by: "Rob Pike",
//! invented_year: 2009
//! },
//! ];
//!
//! let table = Table::new(languages).to_string();
//!
//! let expected = "+------+----------------+---------------+\n\
//! | name | designed_by | invented_year |\n\
//! +------+----------------+---------------+\n\
//! | C | Dennis Ritchie | 1972 |\n\
//! +------+----------------+---------------+\n\
//! | Rust | Graydon Hoare | 2010 |\n\
//! +------+----------------+---------------+\n\
//! | Go | Rob Pike | 2009 |\n\
//! +------+----------------+---------------+\n";
//!
//! assert_eq!(table, expected);
//! ```
//!
//! You can also create a table by using [TableIteratorExt].
//!
//! ```rust,no_run
//! # let languages = [""];
//! use tabled::TableIteratorExt;
//! let table = languages.table();
//! ```
//!
//! Not all types can derive [Tabled] trait though.
//! The example below can't be compiled.
//!
//! ```rust,compile_fail
//! # use tabled::Tabled;
//! #[derive(Tabled)]
//! struct SomeType {
//! field1: SomeOtherType,
//! }
//!
//! struct SomeOtherType;
//! ```
//!
//! We must know what we're up to print as a field. Because of this
//! each field must implement [std::fmt::Display].
//!
//! ### Default implementations
//!
//! As I've already mentioned most of the default types implements the trait out of the box.
//!
//! This allows you to run the following code.
//!
//! ```rust
//! use tabled::{Tabled, Table};
//! let table = Table::new(&[1, 2, 3]);
//! # let expected = "+-----+\n\
//! # | i32 |\n\
//! # +-----+\n\
//! # | 1 |\n\
//! # +-----+\n\
//! # | 2 |\n\
//! # +-----+\n\
//! # | 3 |\n\
//! # +-----+\n";
//! # assert_eq!(table.to_string(), expected);
//! ```
//!
//! ### Combination of types via tuples
//!
//! Personally I consider this a feature which drives the library to shine.
//! You can combine any types that implements [Tabled] trait into one table.
//!
//! You can also see in this example a `#[header("name")]` usage which configures a header
//! of a table which will be printed.
//! You could change it dynamically as well.
//!
//! ```rust
//! use tabled::{Tabled, Table, Style};
//!
//! #[derive(Tabled)]
//! enum Domain {
//! Security,
//! Embeded,
//! Frontend,
//! Unknown,
//! }
//!
//! #[derive(Tabled)]
//! struct Developer(#[header("name")] &'static str);
//!
//! let data = vec![
//! (Developer("Terri Kshlerin"), Domain::Embeded),
//! (Developer("Catalina Dicki"), Domain::Security),
//! (Developer("Jennie Schmeler"), Domain::Frontend),
//! (Developer("Maxim Zhiburt"), Domain::Unknown),
//! ];
//!
//! let table = Table::new(data).with(Style::PSQL).to_string();
//!
//! assert_eq!(
//! table,
//! concat!(
//! " name | Security | Embeded | Frontend | Unknown \n",
//! "-----------------+----------+---------+----------+---------\n",
//! " Terri Kshlerin | | + | | \n",
//! " Catalina Dicki | + | | | \n",
//! " Jennie Schmeler | | | + | \n",
//! " Maxim Zhiburt | | | | + \n"
//! )
//! );
//! ```
//!
//! ## Settings
//!
//! You can find more examples of settings and attributes in
//! [README.md](https://github.com/zhiburt/tabled/blob/master/README.md)
//!
use std::fmt;
mod alignment;
mod concat;
mod disable;
mod formating;
mod highlight;
mod indent;
mod object;
mod panel;
mod rotate;
mod table;
mod width;
pub mod builder;
pub mod display;
pub mod style;
pub use crate::{
alignment::*, concat::*, disable::*, formating::*, highlight::*, indent::*, object::*,
panel::*, rotate::*, style::Style, table::*, width::*,
};
pub use tabled_derive::Tabled;
// todo: change return type to impl Iterator<Cow<str
/// Tabled a trait responsible for providing a header fields and a row fields.
///
/// It's urgent that `header` len is equal to `fields` len.
///
/// ```text
/// Self::headers().len() == self.fields().len()
/// ```
pub trait Tabled {
/// A length of fields and headers,
/// which must be the same.
const LENGTH: usize;
/// Fields method must return a list of cells.
///
/// The cells will be placed in the same row, preserving the order.
fn fields(&self) -> Vec<String>;
/// Headers must return a list of column names.
fn headers() -> Vec<String>;
}
impl<T> Tabled for &T
where
T: Tabled,
{
const LENGTH: usize = T::LENGTH;
fn fields(&self) -> Vec<String> {
T::fields(self)
}
fn headers() -> Vec<String> {
T::headers()
}
}
macro_rules! tuple_table {
( $($name:ident)+ ) => {
impl<$($name: Tabled),+> Tabled for ($($name,)+){
const LENGTH: usize = $($name::LENGTH+)+ 0;
fn fields(&self) -> Vec<String> {
#![allow(non_snake_case)]
let ($($name,)+) = self;
let mut fields = Vec::new();
$(fields.append(&mut $name.fields());)+
fields
}
fn headers() -> Vec<String> {
let mut fields = Vec::new();
$(fields.append(&mut $name::headers());)+
fields
}
}
};
}
tuple_table! { A }
tuple_table! { A B }
tuple_table! { A B C }
tuple_table! { A B C D }
tuple_table! { A B C D E }
tuple_table! { A B C D E F }
macro_rules! default_table {
( $t:ty ) => {
impl Tabled for $t {
const LENGTH: usize = 1;
fn fields(&self) -> Vec<String> {
vec![format!("{}", self)]
}
fn headers() -> Vec<String> {
vec![stringify!($t).to_string()]
}
}
};
}
default_table!(&str);
default_table!(String);
default_table!(char);
default_table!(bool);
default_table!(isize);
default_table!(usize);
default_table!(u8);
default_table!(u16);
default_table!(u32);
default_table!(u64);
default_table!(u128);
default_table!(i8);
default_table!(i16);
default_table!(i32);
default_table!(i64);
default_table!(i128);
default_table!(f32);
default_table!(f64);
impl<T: fmt::Display, const N: usize> Tabled for [T; N] {
const LENGTH: usize = N;
fn fields(&self) -> Vec<String> {
self.iter().map(|e| e.to_string()).collect()
}
fn headers() -> Vec<String> {
(0..N).map(|i| format!("{}", i)).collect()
}
}
