[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.

Add rename_col method

Kasper (Jan 11, 2023, 12:27 AM +0100) 5dfa7ceb 421046d3

+42 -2
+16
src/headers.rs
··· 15 15 } 16 16 } 17 17 18 + /// Returns `false` if `from` is non-existant or if the new name already exists 19 + pub fn rename(&mut self, from: &str, to: &str) -> Result<(), Error> { 20 + if self.contains(to) { 21 + return Err(Error::DuplicateColumn(to.to_string())); 22 + } 23 + let index = match self.indexes.remove(from) { 24 + Some(index) => index, 25 + None => return Err(Error::MissingColumn(from.to_string())), 26 + }; 27 + self.indexes.insert(to.to_string(), index); 28 + let mut row_vec: Vec<_> = self.row.into_iter().collect(); 29 + row_vec[index] = to; 30 + self.row = row_vec.into_iter().collect(); 31 + Ok(()) 32 + } 33 + 18 34 /// Returns false if the field already exists 19 35 pub fn push_field(&mut self, name: &str) -> bool { 20 36 if self.indexes.contains_key(name) {
+3 -2
src/lib.rs
··· 38 38 _ => Ok("Unknown"), 39 39 } 40 40 }) 41 - .map_col("Country", |id_str| Ok(id_str.to_uppercase())) 41 + .rename_col("Country", "COUNTRY") 42 + .map_col("COUNTRY", |id_str| Ok(id_str.to_uppercase())) 42 43 .collect_into_string() 43 44 .unwrap(); 44 45 45 46 assert_eq!( 46 47 csv_str, 47 - "ID,Country,Language\n\ 48 + "ID,COUNTRY,Language\n\ 48 49 1,NORWAY,Norwegian\n\ 49 50 2,TUVALU,Unknown\n" 50 51 );
+23
src/pipeline.rs
··· 158 158 /// 159 159 /// let csv = Pipeline::from_path("test/AB.csv") 160 160 /// .unwrap() 161 + /// .rename_col("A", "X") 162 + /// .collect_into_string() 163 + /// .unwrap(); 164 + /// 165 + /// assert_eq!(csv, "X,B\n1,2\n"); 166 + /// ``` 167 + pub fn rename_col(mut self, from: &str, to: &str) -> Self { 168 + match self.headers.rename(from, to) { 169 + Ok(()) => (), 170 + Err(e) => panic!("{:?}", e), 171 + }; 172 + self 173 + } 174 + 175 + /// Panics if a new name already exists 176 + /// 177 + /// ## Example 178 + /// 179 + /// ``` 180 + /// use csv_pipeline::{Pipeline, StringTarget}; 181 + /// 182 + /// let csv = Pipeline::from_path("test/AB.csv") 183 + /// .unwrap() 161 184 /// .rename_cols(|i, name| { 162 185 /// match name { 163 186 /// "A" => "X",