Parse CSV input file containing masked numbers using csv library
(file_path: &str)
| 25 | |
| 26 | // Parse CSV input file containing masked numbers using csv library |
| 27 | pub async fn parse_csv_input(file_path: &str) -> Result<Vec<CsvRecord>, Error> { |
| 28 | // Check if file exists |
| 29 | if !Path::new(file_path).exists() { |
| 30 | return Err(anyhow!("CSV file not found: {}", file_path)); |
| 31 | } |
| 32 | |
| 33 | // Open and read the file |
| 34 | let file_content = tokio::fs::read_to_string(file_path).await?; |
| 35 | |
| 36 | // Use the csv crate to parse the file |
| 37 | let mut reader = csv::ReaderBuilder::new() |
| 38 | .trim(csv::Trim::All) |
| 39 | .flexible(true) |
| 40 | .from_reader(Cursor::new(file_content)); |
| 41 | |
| 42 | // Parse records |
| 43 | let mut records = Vec::new(); |
| 44 | |
| 45 | // Deserialize each record |
| 46 | for (idx, result) in reader.deserialize::<CsvRecord>().enumerate() { |
| 47 | match result { |
| 48 | Ok(record) => { |
| 49 | records.push(record); |
| 50 | }, |
| 51 | Err(e) => { |
| 52 | return Err(anyhow!("Error parsing CSV record at line {}: {}", idx + 2, e)); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if records.is_empty() { |
| 58 | return Err(anyhow!("No valid records found in CSV file")); |
| 59 | } |
| 60 | |
| 61 | Ok(records) |
| 62 | } |
| 63 | |
| 64 | // Initialize CSV output file with header |
| 65 | pub async fn initialize_csv_output(file_path: &str) -> Result<(), Error> { |