Struct Pipeline

Source
pub struct Pipeline<'a> {
    pub headers: Headers,
    /* private fields */
}
Expand description

The main thing

Fields§

§headers: Headers

Implementations§

Source§

impl<'a> Pipeline<'a>

Source

pub fn from_reader<R: Read + 'a>(reader: Reader<R>) -> Result<Self, PlError>

Source

pub fn from_path<P: AsRef<Path>>(file_path: P) -> Result<Self, PlError>

Create a pipeline from a CSV or TSV file.

Source

pub fn from_rows<I: IntoIterator<Item = Row>>( records: I, ) -> Result<Self, PlError>
where <I as IntoIterator>::IntoIter: 'a,

Source

pub fn from_pipelines<I>(pipelines: I) -> Self
where I: IntoIterator<Item = Pipeline<'a>>, <I as IntoIterator>::IntoIter: 'a,

Merge multiple source pipelines into one. The source pipelines must have identical headers, otherwise the pipelie will return a MismatchedHeaders error returned.

§Example
use csv_pipeline::Pipeline;

let csv = Pipeline::from_pipelines(vec![
  Pipeline::from_path("test/AB.csv").unwrap(),
  Pipeline::from_path("test/AB.csv").unwrap(),
])
  .collect_into_string()
  .unwrap();

assert_eq!(csv, "A,B\n1,2\n1,2\n");
Source

pub fn add_col<F>(self, name: &str, get_value: F) -> Self
where F: FnMut(&Headers, &Row) -> Result<String, Error> + 'a,

Adds a column with values computed from the closure for each row.

§Example
use csv_pipeline::Pipeline;

let csv = Pipeline::from_path("test/AB.csv")
  .unwrap()
  .add_col("C", |headers, row| {
    Ok("3".to_string())
  })
  .collect_into_string()
  .unwrap();

assert_eq!(csv, "A,B,C\n1,2,3\n");
Source

pub fn map<F>(self, get_row: F) -> Self
where F: FnMut(&Headers, Row) -> Result<Row, Error> + 'a,

Maps each row.

§Example
use csv_pipeline::Pipeline;

let csv = Pipeline::from_path("test/AB.csv")
  .unwrap()
  .map(|headers, row| {
    Ok(row.into_iter().map(|field| field.to_string() + "0").collect())
  })
  .collect_into_string()
  .unwrap();

assert_eq!(csv, "A,B\n10,20\n"
);
Source

pub fn map_col<F>(self, col: &str, get_value: F) -> Self
where F: FnMut(&str) -> Result<String, Error> + 'a,

Maps each field of a column.

§Example
use csv_pipeline::Pipeline;

let csv = Pipeline::from_path("test/Countries.csv")
  .unwrap()
  .map_col("Country", |field| Ok(field.to_uppercase()))
  .collect_into_string()
  .unwrap();

assert_eq!(
  csv,
  "ID,Country\n\
    1,NORWAY\n\
    2,TUVALU\n"
);
Source

pub fn filter<F>(self, get_row: F) -> Self
where F: FnMut(&Headers, &Row) -> bool + 'a,

Filter rows using the provided closure.

§Example
use csv_pipeline::Pipeline;

let csv = Pipeline::from_path("test/Countries.csv")
  .unwrap()
  .filter(|headers, row| {
    let country = headers.get_field(&row, "Country").unwrap();
    country == "Tuvalu"
  })
  .collect_into_string()
  .unwrap();

assert_eq!(
  csv,
  "ID,Country\n\
    2,Tuvalu\n"
);
Source

pub fn filter_col<F>(self, name: &str, get_row: F) -> Self
where F: FnMut(&str) -> bool + 'a,

Filter rows based on the field of the specified column, using the provided closure.

§Example
use csv_pipeline::Pipeline;

let csv = Pipeline::from_path("test/Countries.csv")
  .unwrap()
  .filter_col("Country", |country| country == "Tuvalu")
  .collect_into_string()
  .unwrap();

assert_eq!(
  csv,
  "ID,Country\n\
    2,Tuvalu\n"
);
Source

pub fn select(self, columns: Vec<&str>) -> Self

Pick which columns to output, in the specified order. Panics if duplicate colums are specified.

§Example
use csv_pipeline::Pipeline;

let csv = Pipeline::from_path("test/AB.csv")
  .unwrap()
  .select(vec!["B"])
  .collect_into_string()
  .unwrap();

assert_eq!(csv, "B\n2\n");
Source

pub fn rename_col(self, from: &str, to: &str) -> Self

Panics if a new name already exists

§Example
use csv_pipeline::Pipeline;

let csv = Pipeline::from_path("test/AB.csv")
  .unwrap()
  .rename_col("A", "X")
  .collect_into_string()
  .unwrap();

assert_eq!(csv, "X,B\n1,2\n");
Source

pub fn rename_cols<R>(self, get_name: R) -> Self
where R: FnMut(usize, &str) -> &str,

Panics if a new name already exists

§Example
use csv_pipeline::Pipeline;

let csv = Pipeline::from_path("test/AB.csv")
  .unwrap()
  .rename_cols(|i, name| {
    match name {
      "A" => "X",
      name => name,
    }
  })
  .collect_into_string()
  .unwrap();

assert_eq!(csv, "X,B\n1,2\n");
Source

pub fn transform_into<T>(self, get_transformers: T) -> Self
where T: FnMut() -> Vec<Box<dyn Transform>> + 'a,

Group and reduce rows into the provided format. Panics if the transform results in duplicate column names.

§Example
use csv_pipeline::Pipeline;

let csv = Pipeline::from_path("test/AB.csv")
  .unwrap()
  .rename_cols(|i, name| {
    match name {
      "A" => "X",
      name => name,
    }
  })
  .collect_into_string()
  .unwrap();

assert_eq!(csv, "X,B\n1,2\n");
Source

pub fn validate<F>(self, f: F) -> Self
where F: FnMut(&Headers, &Row) -> Result<(), Error> + 'a,

Do your own validation on each row.

Source

pub fn validate_col<F>(self, name: &str, f: F) -> Self
where F: FnMut(&str) -> Result<(), Error> + 'a,

Do your own validation on the fields in a column.

Source

pub fn flush(self, target: impl Target + 'a) -> Self

Write to the specified Target.

§Example
use csv_pipeline::{Pipeline, Target};

let mut csv = String::new();
Pipeline::from_path("test/AB.csv")
  .unwrap()
  .flush(Target::string(&mut csv))
  .run()
  .unwrap();

assert_eq!(csv, "A,B\n1,2\n");
Source

pub fn build(self) -> PipelineIter<'a>

Turn the pipeline into an iterator. You can also do this using pipeline.into_iter().

Source

pub fn run(self) -> Result<(), PlError>

Shorthand for .build().run().

Source

pub fn collect_into_rows(self) -> Result<Vec<Row>, PlError>

Source

pub fn collect_into_string(self) -> Result<String, PlError>

Trait Implementations§

Source§

impl<'a> IntoIterator for Pipeline<'a>

Source§

type Item = Result<StringRecord, PlError>

The type of the elements being iterated over.
Source§

type IntoIter = PipelineIter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Pipeline<'a>

§

impl<'a> !RefUnwindSafe for Pipeline<'a>

§

impl<'a> !Send for Pipeline<'a>

§

impl<'a> !Sync for Pipeline<'a>

§

impl<'a> Unpin for Pipeline<'a>

§

impl<'a> !UnwindSafe for Pipeline<'a>

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, 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.