1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
extern crate puruda_macro; extern crate csv; use puruda_macro::*; use csv::{ReaderBuilder, WriterBuilder, Trim}; use std::error::Error; use std::ops::{Index, IndexMut}; use std::str::FromStr; use std::string::ToString; // ============================================================================= // MultiCol // ============================================================================= // MultiCol definition by `proc_macro` // // # Example implements // ```no-run // #[derive(Debug, Clone)] // pub struct Col2<T1, T2> where T1: Column, T2: Column { // pub col_1: T1, // pub col_2: T2, // } // ``` multi_col_def!(); // ============================================================================= // Implements for MultiCol // ============================================================================= // Multi-Column implements // // # Example of implements // ```no-run // impl<T1, T2> Col2<T1, T2> where T1: Column + Default, T2: Column + Default { // pub fn new() -> Self { // Self { // col_1: T1::default(), // col_2: T2::default(), // } // } // // pub fn from_cols(c1: T1, c2: T2) -> Self { // Self { // col_1: c1, // col_2: c2, // } // } // // pub fn c1(&self) -> &T1 { // &self.col_1 // } // // pub fn c1_mut(&mut self) -> &mut T1 { // &mut self.col_1 // } // // pub fn c2(&self) -> &T2 { // &self.col_2 // } // // pub fn c2_mut(&mut self) -> &mut T2 { // &mut self.col_2 // } // } // ``` multi_col_impl!(); // ============================================================================= // Netcdf support for MultiCol // ============================================================================= //#[cfg(feature = "netcdf")] //pub trait NetCDF: Sized { // fn write_nc(&self, file_path: &str) -> Result<(), Box<dyn Error>>; // fn read_nc(file_path: &str) -> Result<Self, Box<dyn Error>>; // fn read_nc_by_header(file_path: &str, header: Vec<&str>) -> Result<Self, Box<dyn Error>>; //} // //#[cfg(feature = "netcdf")] //impl<T, S> NetCDF for Col2<T, S> //where // T: Column + Default, // S: Column + Default, // T::DType: netcdf::Numeric, // S::DType: netcdf::Numeric, //{ // fn write_nc(&self, file_path: &str) -> Result<(), Box<dyn Error>> { // let mut f = netcdf::create(file_path)?; // // let dim_name = "col_1".to_string(); // let dim = self.col_1.row(); // f.add_dimension(&dim_name, dim)?; // let var = &mut f.add_variable::<T::DType>(self.header[0], &[&dim_name])?; // var.put_values(&self.c1().to_vec()[..], None, None)?; // // let dim_name = "col_2".to_string(); // let dim = self.col_2.row(); // f.add_dimension(&dim_name, dim)?; // let var = &mut f.add_variable::<T::DType>(self.header[0], &[&dim_name])?; // var.put_values(&self.c2().to_vec()[..], None, None)?; // // Ok(()) // } // fn read_nc(file_path: &str) -> Result<Self, Box<dyn Error>> { // unimplemented!() // } // fn read_nc_by_header(file_path: &str, header: Vec<&str>) -> Result<Self, Box<dyn Error>> { // unimplemented!() // } //} // ============================================================================= // CSV Implementation // ============================================================================= #[cfg(feature = "csv")] pub trait CSV: Sized { fn write_csv(&self, file_path: &str, delimiter: char) -> Result<(), Box<dyn Error>>; fn read_csv(file_path: &str, delimiter: char) -> Result<Self, Box<dyn Error>>; } //#[cfg(feature = "csv")] //impl<T, S> CSV for Col2<T, S> //where // T: Column + Default, // S: Column + Default, // T::DType: ToString + FromStr, // S::DType: ToString + FromStr, // <T::DType as FromStr>::Err: std::fmt::Debug + Error, // <S::DType as FromStr>::Err: std::fmt::Debug + Error, // Vec<T::DType>: Into<T>, // Vec<S::DType>: Into<S>, //{ // fn write_csv(&self, file_path: &str, delimiter: char) -> Result<(), Box<dyn Error>> { // let mut wtr = WriterBuilder::new() // .delimiter(delimiter as u8) // .from_path(file_path)?; // let c1 = self.c1(); // let c2 = self.c2(); // let r: usize = c1.row(); // let c: usize = 2; // Col2 // // wtr.write_record(self.header())?; // // for i in 0..r { // let mut record: Vec<String> = vec!["".to_string(); c]; // record[0] = c1.idx(i).to_string(); // record[1] = c2.idx(i).to_string(); // wtr.write_record(record)?; // } // wtr.flush()?; // // Ok(()) // } // // fn read_csv(file_path: &str, delimiter: char) -> Result<Self, Box<dyn Error>> { // let mut rdr = ReaderBuilder::new() // .has_headers(true) // .delimiter(delimiter as u8) // .from_path(file_path)?; // // let mut c1: Vec<T::DType> = vec![]; // let mut c2: Vec<S::DType> = vec![]; // // for rec in rdr.records() { // let rec = rec?; // c1.push(rec[0].parse().unwrap()); // c2.push(rec[1].parse().unwrap()); // } // // let mut col = Col2::from_cols(c1.into(), c2.into()); // col.set_header(vec!["c1", "c2"]); // // Ok(col) // } //} multi_col_csv_impl!(); // ============================================================================= // Column Main Declaration // ============================================================================= pub trait Column { type DType; fn row(&self) -> usize; fn idx(&self, n: usize) -> &Self::DType; fn idx_mut(&mut self, n: usize) -> &mut Self::DType; fn to_vec(&self) -> &Vec<Self::DType>; } impl<T> Index<usize> for dyn Column<DType = T> { type Output = T; fn index(&self, index: usize) -> &Self::Output { self.idx(index) } } impl<T> IndexMut<usize> for dyn Column<DType = T> { fn index_mut(&mut self, index: usize) -> &mut Self::Output { self.idx_mut(index) } } // impl<T, I> Index<I> for dyn Column<DType=T> where I: SliceIndex<[T]> { // type Output = I::Output; // fn index(&self, index: I) -> &Self::Output { // &self.idx_slice(index) // } // } // ============================================================================= // Column Implement for Various type // ============================================================================= col_vec_impl!(bool); col_vec_impl!(u32); col_vec_impl!(u64); col_vec_impl!(usize); col_vec_impl!(i32); col_vec_impl!(i64); col_vec_impl!(isize); col_vec_impl!(f32); col_vec_impl!(f64); col_vec_impl!(String); impl<'a> Column for Vec<&'a str> { type DType = &'a str; fn row(&self) -> usize { self.len() } fn idx(&self, n: usize) -> &Self::DType { &self[n] } fn idx_mut(&mut self, n: usize) -> &mut Self::DType { &mut self[n] } fn to_vec(&self) -> &Vec<Self::DType> { &self } }