Struct csv_pipeline::Pipeline

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

The main thing

Fields§

§headers: Headers

Implementations§

Create a pipeline from a CSV or TSV file.

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

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

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

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

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

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

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

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

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

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

Do your own validation on each row.

Do your own validation on the fields in a column.

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

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

Shorthand for .build().run().

Trait Implementations§

The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.