diff --git a/src/lib.rs b/src/lib.rs index 3138a08..fd843ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ mod pipeline_iterators; pub mod target; pub use headers::Headers; -pub use pipeline::{Pipeline, PipelineBuilder}; +pub use pipeline::{Pipeline, PipelineIter}; pub type Row = csv::StringRecord; pub type RowResult = Result; @@ -30,7 +30,7 @@ impl From for Error { #[test] fn test_pipeline() { let mut csv_str = String::new(); - let mut pipeline = PipelineBuilder::from_path("test/Countries.csv") + let mut pipeline = Pipeline::from_path("test/Countries.csv") .add_col("Language", |headers, row| { match headers.get_field(row, "Country") { Some("Norway") => Ok("Norwegian".to_string()), diff --git a/src/pipeline.rs b/src/pipeline.rs index 5611984..b2a3551 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -7,16 +7,16 @@ use std::fs::File; use std::io; use std::path::Path; -pub struct PipelineBuilder<'a> { +pub struct Pipeline<'a> { pub headers: Headers, iterator: Box + 'a>, } -impl<'a> PipelineBuilder<'a> { +impl<'a> Pipeline<'a> { pub fn from_reader(mut reader: Reader) -> Self { let headers_row = reader.headers().unwrap().clone(); let row_iterator = RowIter::from_records(reader.into_records()); - PipelineBuilder { + Pipeline { headers: Headers::from(headers_row), iterator: Box::new(row_iterator), } @@ -42,9 +42,9 @@ impl<'a> PipelineBuilder<'a> { /// ## Example /// /// ``` - /// use csv_pipeline::PipelineBuilder; + /// use csv_pipeline::Pipeline; /// - /// PipelineBuilder::from_path("test/Countries.csv") + /// Pipeline::from_path("test/Countries.csv") /// .add_col("Language", |headers, row| { /// Ok("".to_string()) /// }); @@ -62,14 +62,14 @@ impl<'a> PipelineBuilder<'a> { self } - /// Maps each row + /// Maps each row. /// /// ## Example /// /// ``` - /// use csv_pipeline::PipelineBuilder; + /// use csv_pipeline::Pipeline; /// - /// PipelineBuilder::from_path("test/Countries.csv") + /// Pipeline::from_path("test/Countries.csv") /// .map(|headers, row| { /// Ok(row.into_iter().map(|field| field.to_uppercase()).collect()) /// }); @@ -86,14 +86,14 @@ impl<'a> PipelineBuilder<'a> { self } - /// Maps each field of a column + /// Maps each field of a column. /// /// ## Example /// /// ``` - /// use csv_pipeline::PipelineBuilder; + /// use csv_pipeline::Pipeline; /// - /// PipelineBuilder::from_path("test/Countries.csv") + /// Pipeline::from_path("test/Countries.csv") /// .map_col("Country", |field| { /// Ok(field.to_uppercase()) /// }); @@ -117,20 +117,30 @@ impl<'a> PipelineBuilder<'a> { self } - pub fn build(self) -> Pipeline<'a> { - Pipeline { + /// Turn the pipeline into an iterator. + /// You can also do this using `pipeline.into_iter()`. + pub fn build(self) -> PipelineIter<'a> { + PipelineIter { headers: self.headers, iterator: Box::new(self.iterator), } } } +impl<'a> IntoIterator for Pipeline<'a> { + type Item = RowResult; + type IntoIter = PipelineIter<'a>; + + fn into_iter(self) -> Self::IntoIter { + self.build() + } +} -pub struct Pipeline<'a> { +pub struct PipelineIter<'a> { pub headers: Headers, pub iterator: Box + 'a>, } -impl<'a> Pipeline<'a> { +impl<'a> PipelineIter<'a> { /// Advances the iterator until an error is found. /// /// Returns `None` when the iterator is finished. @@ -143,7 +153,7 @@ impl<'a> Pipeline<'a> { None } } -impl<'a> Iterator for Pipeline<'a> { +impl<'a> Iterator for PipelineIter<'a> { type Item = RowResult; fn next(&mut self) -> Option {