Struct tabled::builder::Builder

source ·
pub struct Builder { /* private fields */ }
Available on crate feature std only.
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.push_record(["index", "measure", "value"]);
builder.push_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 Builder

source

pub fn new() -> Self

Creates a Builder instance.

use tabled::builder::Builder;

let builder = Builder::new();
source

pub fn with_capacity(count_records: usize, count_columns: usize) -> Self

Creates a Builder instance with a given row capacity.

use tabled::builder::Builder;

let mut builder = Builder::with_capacity(2, 3);
builder.push_record((0..3).map(|i| i.to_string()));
builder.push_record(["i", "surname", "lastname"]);
source

pub fn from_vec(data: Vec<Vec<CellInfo<String>>>) -> Self

Creates a Builder instance.

Safety

It’s marked unsafe to emphasize that you shall make sure that all rows bound to have the same length.

use tabled::builder::Builder;

let data = vec![];
let builder = Builder::from_vec(data);
source

pub fn set_empty<T>(&mut self, text: T)
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_empty("undefined");
builder.push_record((0..3).map(|i| i.to_string()));
builder.push_record(["i"]);
source

pub fn build(self) -> Table

Build creates a Table instance.

use tabled::builder::Builder;

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

pub fn index(self) -> IndexBuilder

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 push_record<R>(&mut self, record: R)
where R: IntoIterator, R::Item: Into<String>,

Adds a row to a Table.

use tabled::builder::Builder;

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

pub fn insert_record<R>(&mut self, index: usize, record: R)
where R: IntoIterator, R::Item: Into<String>,

Insert a row into a specific position.

Panics

Panics if index > count_rows.

source

pub fn clean(&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 remove_record(&mut self, index: usize)

Removes a row with a specific position.

Index expected to be in range. Builder::count_records() < x >= 0

Panics

Panics if row_index > count_rows.

source

pub fn remove_column(&mut self, index: usize)

Removes a column with a specific position.

Index expected to be in range. Builder::count_columns() < x >= 0

Panics

Panics if index > count_columns.

source

pub fn push_column<I>(&mut self, column: I)
where I: IntoIterator, I::Item: Into<String>,

Push a column.

source

pub fn insert_column<I>(&mut self, index: usize, column: I)
where I: IntoIterator, I::Item: Into<String>,

Insert a column with a specific position.

In case a column is bigger then the total amount of rows it will be truncated.

Panics

Panics if index > count_columns.

source

pub fn clear(&mut self)

Remove all records.

source

pub fn count_columns(&self) -> usize

Returns an amount of columns which would be present in a built table.

source

pub fn count_records(&self) -> usize

Returns an amount of rows which would be present in a built table.

Notice that it does not include header if present; It returns only amount of records.

Trait Implementations§

source§

impl Clone for Builder

source§

fn clone(&self) -> Builder

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 Debug for Builder

source§

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

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

impl Default for Builder

source§

fn default() -> Builder

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

impl<D> Extend<D> for Builder
where D: Into<String>,

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 From<Builder> for IndexBuilder

source§

fn from(builder: Builder) -> Self

Converts to this type from the input type.
source§

impl From<Builder> for Table

source§

fn from(builder: Builder) -> Self

Converts to this type from the input type.
source§

impl From<Builder> for Vec<Vec<CellInfo<String>>>

source§

fn from(builder: Builder) -> Self

Converts to this type from the input type.
source§

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

source§

fn from(builder: Builder) -> Self

Converts to this type from the input type.
source§

impl From<IndexBuilder> for Builder

source§

fn from(b: IndexBuilder) -> Self

Converts to this type from the input type.
source§

impl From<Table> for Builder

source§

fn from(val: Table) -> Self

Converts to this type from the input type.
source§

impl From<Vec<Vec<CellInfo<String>>>> for Builder

source§

fn from(data: Vec<Vec<CellInfo<String>>>) -> Self

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl<R> FromIterator<R> for Builder
where R: IntoIterator, R::Item: Into<String>,

source§

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

Creates a value from an iterator. Read more

Auto Trait Implementations§

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> 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,

§

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>,

§

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>,

§

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.