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, Error>
sourcepub fn from_path<P: AsRef<Path>>(file_path: P) -> Result<Self, Error>
pub fn from_path<P: AsRef<Path>>(file_path: P) -> Result<Self, Error>
Create a pipeline from a CSV or TSV file.
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;
Pipeline::from_path("test/AB.csv")
.unwrap()
.add_col("C", |headers, row| {
Ok("1".to_string())
});
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 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. 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");
pub fn validate<F>(self, get_row: F) -> Selfwhere
F: FnMut(&Headers, &Row) -> Result<(), Error> + 'a,
pub fn validate_col<F>(self, name: &str, get_row: F) -> Selfwhere
F: FnMut(&str) -> Result<(), Error> + 'a,
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()
.