pub struct IndexBuilder { /* private fields */ }
Available on crate feature std only.
Expand description

IndexBuilder helps to add an index to the table.

Index is a column on the left of the table.

It also can be used to transpose the table.

Creates a new IndexBuilder instance.

It creates a default index a range from 0 to N. (N - count rows) It also sets a default columns to the range 0 .. N (N - count columns). nfo<’a>

Example

use tabled::builder::Builder;

let mut builder = Builder::default();
builder.push_record(["i", "col-1", "col-2"]);
builder.push_record(["0", "value-1", "value-2"]);

let table = builder.index().build().to_string();

assert_eq!(
    table,
    "+---+---+---------+---------+\n\
     |   | i | col-1   | col-2   |\n\
     +---+---+---------+---------+\n\
     | 0 | 0 | value-1 | value-2 |\n\
     +---+---+---------+---------+"
)

Example

use tabled::builder::Builder;

let table = Builder::default()
    .index()
    .build();

Implementations§

source§

impl IndexBuilder

source

pub fn hide(self) -> Self

No flag makes builder to not use an index.

It may be useful when only Self::transpose need to be used.

use tabled::builder::Builder;

let mut builder = Builder::default();
builder.push_record(["i", "col-1", "col-2"]);
builder.push_record(["0", "value-1", "value-2"]);
builder.push_record(["2", "value-3", "value-4"]);

let table = builder.index().hide().build().to_string();

assert_eq!(
    table,
    "+---+---------+---------+\n\
     | i | col-1   | col-2   |\n\
     +---+---------+---------+\n\
     | 0 | value-1 | value-2 |\n\
     +---+---------+---------+\n\
     | 2 | value-3 | value-4 |\n\
     +---+---------+---------+"
)
source

pub fn name(self, name: Option<String>) -> Self

Set an index name.

When None the name won’t be used.

Example
use tabled::builder::Builder;

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

let table = builder.index()
    .column(1)
    .name(Some(String::from("index")))
    .build();

assert_eq!(
    table.to_string(),
    "+--------+---+---------+\n\
     |        | i | column2 |\n\
     +--------+---+---------+\n\
     | index  |   |         |\n\
     +--------+---+---------+\n\
     | value1 | 0 | value2  |\n\
     +--------+---+---------+"
)
source

pub fn column(self, column: usize) -> Self

Sets a index to the chosen column.

Also sets a name of the index to the column name.

Example
use tabled::builder::Builder;

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

let table = builder.index().column(1).build();

assert_eq!(
    table.to_string(),
    "+---------+---+---------+\n\
     |         | i | column2 |\n\
     +---------+---+---------+\n\
     | column1 |   |         |\n\
     +---------+---+---------+\n\
     | value1  | 0 | value2  |\n\
     +---------+---+---------+"
)
source

pub fn transpose(self) -> Self

Transpose index and columns.

Example
use tabled::builder::Builder;

let mut builder = Builder::default();
builder.push_record(["i", "column-1", "column-2", "column-3"]);
builder.push_record(["0", "value-1", "value-2", "value-3"]);
builder.push_record(["1", "value-4", "value-5", "value-6"]);
builder.push_record(["2", "value-7", "value-8", "value-9"]);

let table = builder.index().column(1).transpose().build();

assert_eq!(
    table.to_string(),
    "+----------+---------+---------+---------+\n\
     | column-1 | value-1 | value-4 | value-7 |\n\
     +----------+---------+---------+---------+\n\
     | i        | 0       | 1       | 2       |\n\
     +----------+---------+---------+---------+\n\
     | column-2 | value-2 | value-5 | value-8 |\n\
     +----------+---------+---------+---------+\n\
     | column-3 | value-3 | value-6 | value-9 |\n\
     +----------+---------+---------+---------+"
)
source

pub fn build(self) -> Table

Builds a table.

Trait Implementations§

source§

impl Clone for IndexBuilder

source§

fn clone(&self) -> IndexBuilder

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 IndexBuilder

source§

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

Formats the value using the given formatter. 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<IndexBuilder> for Builder

source§

fn from(b: IndexBuilder) -> Self

Converts to this type from the input type.

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.