Struct csv_tools::CSVFile

source ·
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

source

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.

source

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);
source

pub fn write(&self, filename: &String) -> Result<(), Error>

Writes the CSV file to a file.

source

pub fn len(&self) -> usize

Returns the number of columns in the CSV file.

source

pub fn count_rows(&self) -> usize

Returns the number of rows in the CSV file. It doesn’t count the header.

source

pub fn has_column(&self, column_name: &String) -> bool

Returns true if the CSV file has the given column.

source

pub fn has_no_rows(&self) -> bool

Returns true if the CSV file has no row.

source

pub fn has_no_columns(&self) -> bool

Returns true if the CSV file has no column.

source

pub fn empty(&self) -> bool

Returns true if the CSV file is empty, meaning it doesn’t have any column and any row.

source

pub fn set_delimiter(&mut self, new_delimiter: &char)

Sets the delimiter of the CSV file.

source

pub fn get_column_idx(&self, column_name: &String) -> Option<usize>

Gets the index of a column by its name.

source

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()));
source

pub fn find_text(&self, text: &String) -> Vec<CSVCoords>

Finds text in the CSV file and returns the coordinates of the cells.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn trim(&mut self)

Removes all the rows that are composed of empty strings only at the beginning and at the end.

source

pub fn remove_empty_lines(&mut self)

Removes all the empty lines from the CSV file.

Trait Implementations§

source§

impl Debug for CSVFile

source§

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

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

impl Display for CSVFile

source§

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

Formats the value using the given formatter. Read more

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.