Struct csv_pipeline::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>
impl<'a> Pipeline<'a>
pub fn from_reader<R: Read + 'a>(reader: Reader<R>) -> Result<Self, PlError>
sourcepub fn from_path<P: AsRef<Path>>(file_path: P) -> Result<Self, PlError>
pub fn from_path<P: AsRef<Path>>(file_path: P) -> Result<Self, PlError>
Create a pipeline from a CSV or TSV file.
sourcepub fn from_pipelines(pipelines: Vec<Pipeline<'a>>) -> Self
pub fn from_pipelines(pipelines: Vec<Pipeline<'a>>) -> Self
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");
sourcepub fn add_col<F>(self, name: &str, get_value: F) -> Selfwhere
F: FnMut(&Headers, &Row) -> Result<String, Error> + 'a,
pub fn add_col<F>(self, name: &str, get_value: F) -> Selfwhere 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");
sourcepub fn map<F>(self, get_row: F) -> Selfwhere
F: FnMut(&Headers, Row) -> Result<Row, Error> + 'a,
pub fn map<F>(self, get_row: F) -> Selfwhere 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"
);
sourcepub fn map_col<F>(self, col: &str, get_value: F) -> Selfwhere
F: FnMut(&str) -> Result<String, Error> + 'a,
pub fn map_col<F>(self, col: &str, get_value: F) -> Selfwhere 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"
);
sourcepub fn filter<F>(self, get_row: F) -> Selfwhere
F: FnMut(&Headers, &Row) -> bool + 'a,
pub fn filter<F>(self, get_row: F) -> Selfwhere 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"
);
sourcepub fn filter_col<F>(self, name: &str, get_row: F) -> Selfwhere
F: FnMut(&str) -> bool + 'a,
pub fn filter_col<F>(self, name: &str, get_row: F) -> Selfwhere 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"
);
sourcepub fn select(self, columns: Vec<&str>) -> Self
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");
sourcepub fn rename_col(self, from: &str, to: &str) -> Self
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");
sourcepub fn rename_cols<R>(self, get_name: R) -> Selfwhere
R: FnMut(usize, &str) -> &str,
pub fn rename_cols<R>(self, get_name: R) -> Selfwhere 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");
sourcepub fn transform_into<T>(self, get_transformers: T) -> Selfwhere
T: FnMut() -> Vec<Box<dyn Transform>> + 'a,
pub fn transform_into<T>(self, get_transformers: T) -> Selfwhere 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");
sourcepub fn validate<F>(self, f: F) -> Selfwhere
F: FnMut(&Headers, &Row) -> Result<(), Error> + 'a,
pub fn validate<F>(self, f: F) -> Selfwhere F: FnMut(&Headers, &Row) -> Result<(), Error> + 'a,
Do your own validation on each row.
sourcepub fn validate_col<F>(self, name: &str, f: F) -> Selfwhere
F: FnMut(&str) -> Result<(), Error> + 'a,
pub fn validate_col<F>(self, name: &str, f: F) -> Selfwhere F: FnMut(&str) -> Result<(), Error> + 'a,
Do your own validation on the fields in a column.
sourcepub fn build(self) -> PipelineIter<'a> ⓘ
pub fn build(self) -> PipelineIter<'a> ⓘ
Turn the pipeline into an iterator.
You can also do this using pipeline.into_iter()
.