tabled::builder

Struct Builder

Source
pub struct Builder<'a> { /* private fields */ }
Expand description

Builder creates a Table from dynamic data set.

It useful when the amount of columns or rows is not known statically.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder.set_columns(["index", "measure", "value"]);
builder.add_record(["0", "weight", "0.443"]);

let table = builder.build();

println!("{}", table);

It may be useful to use FromIterator for building.

use tabled::builder::Builder;
use std::iter::FromIterator;

let data = vec![
    ["column1", "column2"],
    ["data1", "data2"],
    ["data3", "data4"],
];

let table = Builder::from_iter(data).build();

println!("{}", table);

Implementations§

Source§

impl<'a> Builder<'a>

Source

pub fn new() -> Self

Creates a Builder instance.

Source

pub fn hint_column_size(&mut self, size: usize)

Set a column size.

If it make it lower then it was originally it is considered NOP.

Source

pub fn set_columns<H, T>(&mut self, columns: H) -> &mut Self
where H: IntoIterator<Item = T>, T: Into<Cow<'a, str>>,

Sets a Table header.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder
    .set_columns((0..3).map(|i| i.to_string()))
    .add_record(["i", "surname", "lastname"]);
Source

pub fn remove_columns(&mut self) -> &mut Self

Sets off a Table header.

If not set its a nop.

use tabled::Table;

let data = [("Hello", 1u8, false), ("World", 21u8, true)];

let table = Table::builder(data).build().to_string();

assert_eq!(
    table,
    "+-------+----+-------+\n\
     | &str  | u8 | bool  |\n\
     +-------+----+-------+\n\
     | Hello | 1  | false |\n\
     +-------+----+-------+\n\
     | World | 21 | true  |\n\
     +-------+----+-------+"
);


let mut builder = Table::builder(data);
builder.remove_columns();
let table = builder.build().to_string();

assert_eq!(
    table,
    "+-------+----+-------+\n\
     | Hello | 1  | false |\n\
     +-------+----+-------+\n\
     | World | 21 | true  |\n\
     +-------+----+-------+"
);
Source

pub fn add_record<R, T>(&mut self, row: R) -> &mut Self
where R: IntoIterator<Item = T>, T: Into<Cow<'a, str>>,

Adds a row to a Table.

If Self::set_columns is not set the first row will be considered a header.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder.add_record((0..3).map(|i| i.to_string()));
builder.add_record(["i", "surname", "lastname"]);
Source

pub fn set_default_text<T>(&mut self, text: T) -> &mut Self
where T: Into<String>,

Sets a content of cells which are created in case rows has different length.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder.set_default_text("undefined");
builder.set_columns((0..3).map(|i| i.to_string()));
builder.add_record(["i"]);
Source

pub fn build(self) -> Table<VecRecords<CellInfo<'a>>>

Build creates a Table instance.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder.set_columns(["i", "column1", "column2"]);
builder.add_record(["0", "value1", "value2"]);
Source

pub fn index(self) -> IndexBuilder<'a>

Add an index to the Table.

Default index is a range 0-N where N is amount of records.

§Example
use tabled::Table;

let table = Table::builder(&["Hello", "World", "!"]).index().build();

assert_eq!(
    table.to_string(),
    "+---+-------+\n\
     |   | &str  |\n\
     +---+-------+\n\
     | 0 | Hello |\n\
     +---+-------+\n\
     | 1 | World |\n\
     +---+-------+\n\
     | 2 | !     |\n\
     +---+-------+"
)
Source

pub fn clean(&mut self) -> &mut Self

Clean removes empty columns and rows.

§Example
use tabled::Table;

let mut builder = Table::builder(&["Hello", "World", ""]);
builder.clean();

let table = builder.build();

assert_eq!(
    table.to_string(),
    "+-------+\n\
     | &str  |\n\
     +-------+\n\
     | Hello |\n\
     +-------+\n\
     | World |\n\
     +-------+"
)
Source

pub fn custom<R>(records: R) -> CustomRecords<R>

Creates a Builder from a built Records

Trait Implementations§

Source§

impl<'a> Clone for Builder<'a>

Source§

fn clone(&self) -> Builder<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Builder<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for Builder<'a>

Source§

fn default() -> Builder<'a>

Returns the “default value” for a type. Read more
Source§

impl<'a, D> Extend<D> for Builder<'a>
where D: Into<Cow<'a, str>>,

Source§

fn extend<T: IntoIterator<Item = D>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a> From<IndexBuilder<'a>> for Builder<'a>

Source§

fn from(index_builder: IndexBuilder<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Vec<Vec<CellInfo<'a>>>> for Builder<'a>

Source§

fn from(records: Vec<Vec<CellInfo<'a>>>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Vec<String>>> for Builder<'_>

Source§

fn from(strings: Vec<Vec<String>>) -> Self

Converts to this type from the input type.
Source§

impl<'a, R, V> FromIterator<R> for Builder<'a>
where R: IntoIterator<Item = V>, V: Into<Cow<'a, str>>,

Source§

fn from_iter<T: IntoIterator<Item = R>>(iter: T) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Builder<'a>

§

impl<'a> RefUnwindSafe for Builder<'a>

§

impl<'a> Send for Builder<'a>

§

impl<'a> Sync for Builder<'a>

§

impl<'a> Unpin for Builder<'a>

§

impl<'a> UnwindSafe for Builder<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.