Table

Struct Table 

Source
pub struct Table { /* private fields */ }
Expand description

Represents a parsed table with headers and data rows.

All rows must have the same number of columns as the header. Use Table::new_validated to create a table with automatic validation.

§Examples

use table_extractor::Table;

let table = Table::new(
    vec!["id".to_string(), "name".to_string()],
    vec![
        vec!["1".to_string(), "Alice".to_string()],
        vec!["2".to_string(), "Bob".to_string()],
    ],
);

assert_eq!(table.column_count(), 2);
assert!(!table.is_empty());

Implementations§

Source§

impl Table

Source

pub fn new(headers: Vec<String>, rows: Vec<Vec<String>>) -> Self

Creates a new table without validation.

For safer construction with automatic validation, use Table::new_validated.

§Examples
use table_extractor::Table;

let table = Table::new(
    vec!["id".to_string(), "name".to_string()],
    vec![vec!["1".to_string(), "Alice".to_string()]],
);
Source

pub fn validate(&self) -> Result<()>

Validates that all rows have the same number of columns as headers.

§Errors

Returns error::Error::InconsistentColumns if any row has a different column count than the header.

§Examples
use table_extractor::Table;

let table = Table::new(
    vec!["id".to_string(), "name".to_string()],
    vec![
        vec!["1".to_string(), "Alice".to_string()],
        vec!["2".to_string(), "Bob".to_string()],
    ],
);

assert!(table.validate().is_ok());

// Table with inconsistent columns
let bad_table = Table::new(
    vec!["id".to_string(), "name".to_string()],
    vec![vec!["1".to_string()]], // Missing column!
);

assert!(bad_table.validate().is_err());
Source

pub fn new_validated( headers: Vec<String>, rows: Vec<Vec<String>>, ) -> Result<Self>

Creates a new table and validates it.

This is the recommended way to create a table as it ensures data integrity by validating column counts and enforcing limits.

§Errors

Returns an error if:

§Examples
use table_extractor::Table;

// Valid table
let table = Table::new_validated(
    vec!["id".to_string(), "name".to_string()],
    vec![
        vec!["1".to_string(), "Alice".to_string()],
        vec!["2".to_string(), "Bob".to_string()],
    ],
);
assert!(table.is_ok());

// Invalid table (inconsistent columns)
let bad_table = Table::new_validated(
    vec!["id".to_string(), "name".to_string()],
    vec![vec!["1".to_string()]], // Missing column!
);
assert!(bad_table.is_err());
Source

pub fn is_empty(&self) -> bool

Returns true if the table contains no data rows.

Note: A table with headers but no data rows is considered empty.

§Examples
use table_extractor::Table;

let empty_table = Table::new(
    vec!["id".to_string(), "name".to_string()],
    vec![],
);
assert!(empty_table.is_empty());

let table_with_data = Table::new(
    vec!["id".to_string(), "name".to_string()],
    vec![vec!["1".to_string(), "Alice".to_string()]],
);
assert!(!table_with_data.is_empty());
Source

pub fn column_count(&self) -> usize

Returns the number of columns in the table.

This is equivalent to the length of the headers vector.

§Examples
use table_extractor::Table;

let table = Table::new(
    vec!["id".to_string(), "name".to_string(), "email".to_string()],
    vec![],
);
assert_eq!(table.column_count(), 3);
Source

pub fn headers(&self) -> &[String]

Returns a reference to the table headers.

§Examples
use table_extractor::Table;

let table = Table::new(
    vec!["id".to_string(), "name".to_string()],
    vec![],
);
assert_eq!(table.headers(), &["id", "name"]);
Source

pub fn rows(&self) -> &[Vec<String>]

Returns a reference to the table rows.

§Examples
use table_extractor::Table;

let table = Table::new(
    vec!["id".to_string(), "name".to_string()],
    vec![
        vec!["1".to_string(), "Alice".to_string()],
        vec!["2".to_string(), "Bob".to_string()],
    ],
);
assert_eq!(table.rows().len(), 2);
assert_eq!(table.rows()[0], vec!["1", "Alice"]);
Source

pub fn into_parts(self) -> (Vec<String>, Vec<Vec<String>>)

Consumes the table and returns the headers and rows.

This is useful when you need ownership of the table’s data.

§Examples
use table_extractor::Table;

let table = Table::new(
    vec!["id".to_string(), "name".to_string()],
    vec![vec!["1".to_string(), "Alice".to_string()]],
);

let (headers, rows) = table.into_parts();
assert_eq!(headers, vec!["id", "name"]);
assert_eq!(rows.len(), 1);

Trait Implementations§

Source§

impl Clone for Table

Source§

fn clone(&self) -> Table

Returns a duplicate 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 Table

Source§

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

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

impl PartialEq for Table

Source§

fn eq(&self, other: &Table) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Table

Auto Trait Implementations§

§

impl Freeze for Table

§

impl RefUnwindSafe for Table

§

impl Send for Table

§

impl Sync for Table

§

impl Unpin for Table

§

impl UnwindSafe for Table

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.