MCPcopy Create free account
hub / github.com/ddd/gpb / parse_csv_input

Function parse_csv_input

src/csv/parser.rs:27–62  ·  view source on GitHub ↗

Parse CSV input file containing masked numbers using csv library

(file_path: &str)

Source from the content-addressed store, hash-verified

25
26// Parse CSV input file containing masked numbers using csv library
27pub 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
65pub async fn initialize_csv_output(file_path: &str) -> Result<(), Error> {

Callers 1

process_csv_modeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected