[READ-ONLY] Mirror of https://github.com/probablykasper/csv-pipeline.
0

Configure Feed

Select the types of activity you want to include in your feed.

Pipeline + PipelineIter

Kasper (Jan 9, 2023, 9:05 AM +0100) 6e96a0ea 405d828b

+28 -18
+2 -2
src/lib.rs
··· 4 4 pub mod target; 5 5 6 6 pub use headers::Headers; 7 - pub use pipeline::{Pipeline, PipelineBuilder}; 7 + pub use pipeline::{Pipeline, PipelineIter}; 8 8 pub type Row = csv::StringRecord; 9 9 pub type RowResult = Result<Row, Error>; 10 10 ··· 30 30 #[test] 31 31 fn test_pipeline() { 32 32 let mut csv_str = String::new(); 33 - let mut pipeline = PipelineBuilder::from_path("test/Countries.csv") 33 + let mut pipeline = Pipeline::from_path("test/Countries.csv") 34 34 .add_col("Language", |headers, row| { 35 35 match headers.get_field(row, "Country") { 36 36 Some("Norway") => Ok("Norwegian".to_string()),
+26 -16
src/pipeline.rs
··· 7 7 use std::io; 8 8 use std::path::Path; 9 9 10 - pub struct PipelineBuilder<'a> { 10 + pub struct Pipeline<'a> { 11 11 pub headers: Headers, 12 12 iterator: Box<dyn Iterator<Item = RowResult> + 'a>, 13 13 } 14 14 15 - impl<'a> PipelineBuilder<'a> { 15 + impl<'a> Pipeline<'a> { 16 16 pub fn from_reader(mut reader: Reader<File>) -> Self { 17 17 let headers_row = reader.headers().unwrap().clone(); 18 18 let row_iterator = RowIter::from_records(reader.into_records()); 19 - PipelineBuilder { 19 + Pipeline { 20 20 headers: Headers::from(headers_row), 21 21 iterator: Box::new(row_iterator), 22 22 } ··· 42 42 /// ## Example 43 43 /// 44 44 /// ``` 45 - /// use csv_pipeline::PipelineBuilder; 45 + /// use csv_pipeline::Pipeline; 46 46 /// 47 - /// PipelineBuilder::from_path("test/Countries.csv") 47 + /// Pipeline::from_path("test/Countries.csv") 48 48 /// .add_col("Language", |headers, row| { 49 49 /// Ok("".to_string()) 50 50 /// }); ··· 62 62 self 63 63 } 64 64 65 - /// Maps each row 65 + /// Maps each row. 66 66 /// 67 67 /// ## Example 68 68 /// 69 69 /// ``` 70 - /// use csv_pipeline::PipelineBuilder; 70 + /// use csv_pipeline::Pipeline; 71 71 /// 72 - /// PipelineBuilder::from_path("test/Countries.csv") 72 + /// Pipeline::from_path("test/Countries.csv") 73 73 /// .map(|headers, row| { 74 74 /// Ok(row.into_iter().map(|field| field.to_uppercase()).collect()) 75 75 /// }); ··· 86 86 self 87 87 } 88 88 89 - /// Maps each field of a column 89 + /// Maps each field of a column. 90 90 /// 91 91 /// ## Example 92 92 /// 93 93 /// ``` 94 - /// use csv_pipeline::PipelineBuilder; 94 + /// use csv_pipeline::Pipeline; 95 95 /// 96 - /// PipelineBuilder::from_path("test/Countries.csv") 96 + /// Pipeline::from_path("test/Countries.csv") 97 97 /// .map_col("Country", |field| { 98 98 /// Ok(field.to_uppercase()) 99 99 /// }); ··· 117 117 self 118 118 } 119 119 120 - pub fn build(self) -> Pipeline<'a> { 121 - Pipeline { 120 + /// Turn the pipeline into an iterator. 121 + /// You can also do this using `pipeline.into_iter()`. 122 + pub fn build(self) -> PipelineIter<'a> { 123 + PipelineIter { 122 124 headers: self.headers, 123 125 iterator: Box::new(self.iterator), 124 126 } 125 127 } 126 128 } 129 + impl<'a> IntoIterator for Pipeline<'a> { 130 + type Item = RowResult; 131 + type IntoIter = PipelineIter<'a>; 127 132 128 - pub struct Pipeline<'a> { 133 + fn into_iter(self) -> Self::IntoIter { 134 + self.build() 135 + } 136 + } 137 + 138 + pub struct PipelineIter<'a> { 129 139 pub headers: Headers, 130 140 pub iterator: Box<dyn Iterator<Item = RowResult> + 'a>, 131 141 } 132 142 133 - impl<'a> Pipeline<'a> { 143 + impl<'a> PipelineIter<'a> { 134 144 /// Advances the iterator until an error is found. 135 145 /// 136 146 /// Returns `None` when the iterator is finished. ··· 143 153 None 144 154 } 145 155 } 146 - impl<'a> Iterator for Pipeline<'a> { 156 + impl<'a> Iterator for PipelineIter<'a> { 147 157 type Item = RowResult; 148 158 149 159 fn next(&mut self) -> Option<Self::Item> {