pub struct CSVFile {
pub delimiter: char,
pub columns: Vec<String>,
pub rows: Vec<Vec<String>>,
}
Fields§
§delimiter: char
§columns: Vec<String>
§rows: Vec<Vec<String>>
Implementations§
source§impl CSVFile
impl CSVFile
sourcepub fn new(file_name: &String, delimiter: &char) -> Result<Self, Error>
pub fn new(file_name: &String, delimiter: &char) -> Result<Self, Error>
Creates a new CSVFile from a file name and an optional delimiter (a comma by default). It reads the first line of the file to get the columns and the rest of the file to get the data. It may return an error if the file doesn’t exist or if it can’t be read properly.
sourcepub fn build(
columns: &Vec<String>,
rows: &Vec<Vec<String>>,
delimiter: &char
) -> Result<Self, Error>
pub fn build( columns: &Vec<String>, rows: &Vec<Vec<String>>, delimiter: &char ) -> Result<Self, Error>
Creates a new CSVFile from the columns and the rows.
§Example
let columns = vec!["a".to_string(), "b".to_string(), "c".to_string()];
let rows = vec![
vec!["1".to_string(), "2".to_string(), "3".to_string()],
vec!["4".to_string(), "5".to_string(), "6".to_string()],
vec!["7".to_string(), "8".to_string(), "9".to_string()],
];
let file = CSVFile::build(&columns, &rows, &',').unwrap();
assert_eq!(file.columns, columns);
assert_eq!(file.rows, rows);
sourcepub fn count_rows(&self) -> usize
pub fn count_rows(&self) -> usize
Returns the number of rows in the CSV file. It doesn’t count the header.
sourcepub fn has_column(&self, column_name: &String) -> bool
pub fn has_column(&self, column_name: &String) -> bool
Returns true
if the CSV file has the given column.
sourcepub fn has_no_rows(&self) -> bool
pub fn has_no_rows(&self) -> bool
Returns true
if the CSV file has no row.
sourcepub fn has_no_columns(&self) -> bool
pub fn has_no_columns(&self) -> bool
Returns true
if the CSV file has no column.
sourcepub fn empty(&self) -> bool
pub fn empty(&self) -> bool
Returns true
if the CSV file is empty,
meaning it doesn’t have any column and any row.
sourcepub fn set_delimiter(&mut self, new_delimiter: &char)
pub fn set_delimiter(&mut self, new_delimiter: &char)
Sets the delimiter of the CSV file.
sourcepub fn get_column_idx(&self, column_name: &String) -> Option<usize>
pub fn get_column_idx(&self, column_name: &String) -> Option<usize>
Gets the index of a column by its name.
sourcepub fn get_cell(&self, coordinates: &CSVCoords) -> Option<&String>
pub fn get_cell(&self, coordinates: &CSVCoords) -> Option<&String>
Gets a cell at given coordinates.
It returns None
if the coordinates are out of range.
§Example
let columns = vec!["a".to_string(), "b".to_string(), "c".to_string()];
let rows = vec![
vec!["1".to_string(), "2".to_string(), "3".to_string()],
vec!["4".to_string(), "5".to_string(), "6".to_string()],
vec!["7".to_string(), "8".to_string(), "9".to_string()],
];
let file = CSVFile::build(&columns, &rows, &',').unwrap();
assert_eq!(file.get_cell(&CSVCoords { row: 0, column: 0 }), Some(&"1".to_string()));
assert_eq!(file.get_cell(&CSVCoords { row: 1, column: 1 }), Some(&"5".to_string()));
assert_eq!(file.get_cell(&CSVCoords { row: 2, column: 2 }), Some(&"9".to_string()));
sourcepub fn find_text(&self, text: &String) -> Vec<CSVCoords>
pub fn find_text(&self, text: &String) -> Vec<CSVCoords>
Finds text in the CSV file and returns the coordinates of the cells.
sourcepub fn check_validity(&self) -> bool
pub fn check_validity(&self) -> bool
Checks if the CSV file is valid. It checks for duplicates in the columns and if the rows have the right length.
sourcepub fn fill_column(
&mut self,
column_name: &String,
data: &Vec<String>
) -> Result<(), Error>
pub fn fill_column( &mut self, column_name: &String, data: &Vec<String> ) -> Result<(), Error>
Fills a column with the given data. It may return an error if the column doesn’t exist or if the length of the data is different from the number of rows.
sourcepub fn merge(&mut self, other: &CSVFile) -> Result<(), Error>
pub fn merge(&mut self, other: &CSVFile) -> Result<(), Error>
Merges two CSV files together. It may return an error if a duplicated column is found. If the number of rows are different, then the rows are extended with empty strings.
The other CSVFile instance is supposed to be valid.
sourcepub fn add_row(&mut self, data: &Vec<String>) -> Result<(), Error>
pub fn add_row(&mut self, data: &Vec<String>) -> Result<(), Error>
Adds a row to the CSV file. It may return an error if the number of fields in the row is different from the number of columns.
sourcepub fn add_column(&mut self, name: &String) -> Result<(), Error>
pub fn add_column(&mut self, name: &String) -> Result<(), Error>
Adds a column to the CSV file. It may return an error if the column already exists. It appends an empty string to each row.
sourcepub fn insert_column(
&mut self,
name: &String,
column_idx: usize
) -> Result<(), Error>
pub fn insert_column( &mut self, name: &String, column_idx: usize ) -> Result<(), Error>
Inserts a column to the CSV file at a specific index. It may return an error if the column already exists or if the index is out of range. It also inserts an empty string to each row.
sourcepub fn remove_column(&mut self, column_idx: usize) -> Result<(), Error>
pub fn remove_column(&mut self, column_idx: usize) -> Result<(), Error>
Removes a column from the CSV file. It may return an error if the column index is out of range.
sourcepub fn remove_row(&mut self, row_idx: usize) -> Result<(), Error>
pub fn remove_row(&mut self, row_idx: usize) -> Result<(), Error>
Removes a row from the CSV file. It may return an error if the row index is out of range.
sourcepub fn trim_end(&mut self)
pub fn trim_end(&mut self)
Removes all the rows that are composed of empty strings only, starting at the very end and stopping as soon as a non-empty row is found.
If no empty row is found, then nothing happens.
sourcepub fn trim_start(&mut self)
pub fn trim_start(&mut self)
Removes all the rows that are composed of empty strings only, starting at the very beginning and stopping as soon as a non-empty row is found.
If no empty row is found, then nothing happens.
sourcepub fn trim(&mut self)
pub fn trim(&mut self)
Removes all the rows that are composed of empty strings only at the beginning and at the end.
sourcepub fn remove_empty_lines(&mut self)
pub fn remove_empty_lines(&mut self)
Removes all the empty lines from the CSV file.