pub struct Table { /* private fields */ }Implementations§
Source§impl Table
impl Table
pub fn new() -> Self
pub fn num_records(&self) -> usize
pub fn num_fields(&self) -> usize
Sourcepub fn push_row(&mut self, new_record: &Vec<&str>) -> Result<(), TableError>
pub fn push_row(&mut self, new_record: &Vec<&str>) -> Result<(), TableError>
§Description
Pushes a new record to the end of the Table object.
§Arguments
new_record: &Vec<&str>- An immutable reference to string slices that represent the new record.
§Errors
Returns an error if the number of fields is invalid.
§Example
use cli_tables::Table;
let mut table = Table::new();
let header = vec!["Id", "Title", "Series", "Author"];
table.push_row(&header).unwrap();
let book = vec!["0", "Sword of Destiny", "The Witcher Series", "Andrzej Sapkowski"];
table.push_row(&book).unwrap();Sourcepub fn push_row_string(
&mut self,
new_record: &Vec<String>,
) -> Result<(), TableError>
pub fn push_row_string( &mut self, new_record: &Vec<String>, ) -> Result<(), TableError>
§Description
Pushes a new record to the end of the Table object.
§Arguments
new_record: &Vec<String>- An immutable reference to Strings that represent the new record.
§Errors
Returns an error if the number of fields is invalid.
§Example
use cli_tables::Table;
let mut table = Table::new();
let header = vec![
"Id".to_string(),
"Title".to_string(),
"Series".to_string(),
"Author".to_string()
];
table.push_row_string(&header).unwrap();
let book = vec![
"0".to_string(),
"Sword of Destiny".to_string(),
"The Witcher Series".to_string(),
"Andrzej Sapkowski".to_string()
];
table.push_row_string(&book).unwrap();Sourcepub fn push_rows(
&mut self,
new_records: &Vec<Vec<&str>>,
) -> Result<(), TableError>
pub fn push_rows( &mut self, new_records: &Vec<Vec<&str>>, ) -> Result<(), TableError>
§Description
The push_rows function takes multiple records, and adds each one
in order to the Table object.
§Arguments
new_records- reference to aVec<Vec<&str>>object that represents multiple records.
§Errors
Returns a TableError if the number of fields is not equal
for each record or if the number of fields is not equal to
the existing number of fields in the Table object.
§Example
use cli_tables::Table;
let mut table = Table::new();
let table_vec = vec![
vec!["Id", "Title", "Series", "Author"],
vec!["0", "Sword of Destiny", "The Witcher Series", "Andrzej Sapkowski"],
vec!["1", "The Last Wish", "The Witcher Series", "Andrzej Sapkowski"]
];
table.push_rows(&table_vec).unwrap();Sourcepub fn get_row(&self, id: usize) -> Result<Vec<String>, TableError>
pub fn get_row(&self, id: usize) -> Result<Vec<String>, TableError>
§Description
Retrieves the desired record of the Table object and returns it.
§Arguments
id: usize- represets the desired record id to be retrieved.
§Errors
Returns a TableError if no record with the given id is found.
§Example
use cli_tables::Table;
let mut table = Table::new();
let header = vec![
"Id",
"Title",
"Series",
"Author"
];
table.push_row(&header).unwrap();
table.get_row(0).unwrap();Sourcepub fn delete_record(&mut self, id: usize) -> Result<(), TableError>
pub fn delete_record(&mut self, id: usize) -> Result<(), TableError>
§Description
Deletes the desired record from the Table object.
§Arguments
id- Ausizevalue that represets the desired record id to be deleted.
§Errors
Returns a TableError if no record with the given id is found.
§Example
use cli_tables::Table;
let mut table = Table::new();
let header = vec![
"Id",
"Title",
"Series",
"Author"
];
table.push_row(&header).unwrap();
table.delete_record(0).unwrap();Sourcepub fn set(&mut self, new_table: &Vec<Vec<&str>>) -> Result<(), TableError>
pub fn set(&mut self, new_table: &Vec<Vec<&str>>) -> Result<(), TableError>
§Description
The set function takes multiple records, and adds each one
in order to the Table object. If the Table object is not
empty, it is reset with the given values.
§Arguments
new_table- reference to aVec<Vec<&str>>object that represents multiple records.
§Errors
Returns a TableError if the number of fields is not equal
for each record.
§Example
use cli_tables::Table;
let mut table = Table::new();
let table_vec = vec![
vec!["Id", "Title", "Series", "Author"],
vec!["0", "Sword of Destiny", "The Witcher Series", "Andrzej Sapkowski"],
vec!["1", "The Last Wish", "The Witcher Series", "Andrzej Sapkowski"]
];
table.set(&table_vec).unwrap();