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
impl Table
Sourcepub fn new(headers: Vec<String>, rows: Vec<Vec<String>>) -> Self
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()]],
);Sourcepub fn validate(&self) -> Result<()>
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());Sourcepub fn new_validated(
headers: Vec<String>,
rows: Vec<Vec<String>>,
) -> Result<Self>
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:
- The number of columns exceeds 10,000 (
error::Error::InvalidFormat) - Any row has a different column count than the header (
error::Error::InconsistentColumns)
§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());Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn column_count(&self) -> usize
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);Sourcepub fn headers(&self) -> &[String]
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"]);Sourcepub fn rows(&self) -> &[Vec<String>]
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"]);Sourcepub fn into_parts(self) -> (Vec<String>, Vec<Vec<String>>)
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);