TODO: Can we dynamically get the types, like in pandas? We need to put quotes around strings and not around numbers. https://stackoverflow.com/questions/64369887/how-do-i-read-csv-data-without-knowing-the-structure-at-compile-time
(text: &str)
| 1068 | // quotes around strings and not around numbers. |
| 1069 | // https://stackoverflow.com/questions/64369887/how-do-i-read-csv-data-without-knowing-the-structure-at-compile-time |
| 1070 | pub fn parse_csv(text: &str) -> Result<RelationLiteral, String> { |
| 1071 | let text = text.trim(); |
| 1072 | let mut rdr = csv::Reader::from_reader(text.as_bytes()); |
| 1073 | |
| 1074 | fn parse_header(row: &csv::StringRecord) -> Vec<String> { |
| 1075 | row.into_iter().map(|x| x.to_string()).collect() |
| 1076 | } |
| 1077 | |
| 1078 | fn parse_row(row: csv::StringRecord) -> Vec<Literal> { |
| 1079 | row.into_iter() |
| 1080 | .map(|x| Literal::String(x.to_string())) |
| 1081 | .collect() |
| 1082 | } |
| 1083 | |
| 1084 | Ok(RelationLiteral { |
| 1085 | columns: parse_header(rdr.headers().map_err(|e| e.to_string())?), |
| 1086 | rows: rdr |
| 1087 | .records() |
| 1088 | .map(|row_result| row_result.map(parse_row)) |
| 1089 | .try_collect() |
| 1090 | .map_err(|e| e.to_string())?, |
| 1091 | }) |
| 1092 | } |
| 1093 | |
| 1094 | type JsonFormat1Row = HashMap<String, serde_json::Value>; |
| 1095 |
no test coverage detected