Skip to content

Commit 27eb115

Browse files
committed
fix delimiters
1 parent c74f0e3 commit 27eb115

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/csvparser.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,20 +458,28 @@ pub fn parse_revolut_transactions(csvtoparse: &str) -> Result<RevolutTransaction
458458

459459
let mut ta = TransactionAccumulator::default();
460460

461+
let original_delimiter: u8 = if csvtoparse.ends_with(".tsv") {
462+
b'\t'
463+
} else {
464+
b','
465+
};
461466
const DELIMITER: u8 = b';';
467+
const DELIMITER_AS_STR: &str = ";";
462468

463-
//let mut rdr = csv::Reader::from_path(csvtoparse).map_err(|_| "Error: opening CSV")?;
464469
let mut rdr = csv::ReaderBuilder::new()
465470
.flexible(true)
471+
.delimiter(original_delimiter)
466472
.from_path(csvtoparse)
467473
.map_err(|_| "Error: opening CSV")?;
474+
468475
let result = rdr
469476
.headers()
470477
.map_err(|e| format!("Error: scanning CSV header: {e}"))?;
471478
if result.iter().any(|field| field == "Completed Date") {
472479
log::info!("Detected Savings account statement: {csvtoparse}");
473480
let df = CsvReader::from_path(csvtoparse)
474481
.map_err(|_| "Error: opening CSV")?
482+
.with_separator(original_delimiter)
475483
.has_header(true)
476484
.finish()
477485
.map_err(|e| format!("Error reading CSV: {e}"))?;
@@ -492,6 +500,7 @@ pub fn parse_revolut_transactions(csvtoparse: &str) -> Result<RevolutTransaction
492500
log::info!("Detected Investment account statement: {csvtoparse}");
493501
let df = CsvReader::from_path(csvtoparse)
494502
.map_err(|_| "Error: opening CSV")?
503+
.with_separator(original_delimiter)
495504
.has_header(true)
496505
.finish()
497506
.map_err(|e| format!("Error reading CSV: {e}"))?;
@@ -508,7 +517,10 @@ pub fn parse_revolut_transactions(csvtoparse: &str) -> Result<RevolutTransaction
508517
let mut switch = false;
509518
for result in rdr.records() {
510519
let record = result.map_err(|e| format!("Error reading CSV: {e}"))?;
511-
let line = record.into_iter().collect::<Vec<&str>>().join(",");
520+
let line = record
521+
.into_iter()
522+
.collect::<Vec<&str>>()
523+
.join(DELIMITER_AS_STR);
512524
if line.starts_with("Other income & fees") {
513525
switch = true;
514526
} else {
@@ -525,10 +537,12 @@ pub fn parse_revolut_transactions(csvtoparse: &str) -> Result<RevolutTransaction
525537
log::info!("Content of second to be DataFrame: {content2}");
526538

527539
let sales = CsvReader::new(std::io::Cursor::new(content1.as_bytes()))
540+
.with_separator(DELIMITER)
528541
.finish()
529542
.map_err(|e| format!("Error reading CSV: {e}"))?;
530543

531544
let others = CsvReader::new(std::io::Cursor::new(content2.as_bytes()))
545+
.with_separator(DELIMITER)
532546
.truncate_ragged_lines(true)
533547
.finish()
534548
.map_err(|e| format!("Error reading CSV: {e}"))?;
@@ -562,11 +576,11 @@ pub fn parse_revolut_transactions(csvtoparse: &str) -> Result<RevolutTransaction
562576
let mut state = ParsingState::None;
563577

564578
for result in rdr.records() {
565-
let record = result.map_err(|e| format!("Error reading CSV: {e}"))?;
566-
let line = record.into_iter().collect::<Vec<&str>>().join(
567-
std::str::from_utf8(&[DELIMITER])
568-
.map_err(|_| "ERROR: Unable to convert delimiter to string".to_string())?,
569-
);
579+
let record = result.map_err(|e| format!("Error reading CSV record: {e}"))?;
580+
let line = record
581+
.into_iter()
582+
.collect::<Vec<&str>>()
583+
.join(DELIMITER_AS_STR);
570584
if line.starts_with("Transactions for") {
571585
process_tax_consolidated_data(&state, DELIMITER, &mut ta)?;
572586

@@ -929,7 +943,7 @@ mod tests {
929943

930944
// There should be some crypto transactions parsed
931945
assert!(
932-
parsed.crypto_transactions.len() > 0,
946+
!parsed.crypto_transactions.is_empty(),
933947
"No crypto transactions parsed"
934948
);
935949

@@ -953,7 +967,7 @@ mod tests {
953967
total_cost
954968
);
955969
assert!(
956-
(total_gross - 7.95).abs() < 1e-6,
970+
(total_gross - 7.95).abs() < eps,
957971
"expected total crypto gross ~7.95, got {}",
958972
total_gross
959973
);

0 commit comments

Comments
 (0)