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.
pub fn from_rows<I: IntoIterator<Item = Row>>(
records: I,
) -> Result<Self, PlError>where
<I as IntoIterator>::IntoIter: 'a,
Sourcepub fn from_pipelines<I>(pipelines: I) -> Self
pub fn from_pipelines<I>(pipelines: I) -> 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) -> Self
pub fn add_col<F>(self, name: &str, get_value: F) -> Self
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) -> Self
pub fn map<F>(self, get_row: F) -> Self
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) -> Self
pub fn map_col<F>(self, col: &str, get_value: F) -> Self
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) -> Self
pub fn filter<F>(self, get_row: F) -> Self
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) -> Self
pub fn filter_col<F>(self, name: &str, get_row: F) -> Self
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) -> Self
pub fn rename_cols<R>(self, get_name: R) -> Self
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) -> Self
pub fn transform_into<T>(self, get_transformers: T) -> Self
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_col<F>(self, name: &str, f: F) -> Self
pub fn validate_col<F>(self, name: &str, f: F) -> Self
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()
.