Something went wrong. Try again.
Convert CSV banking statements to hledger journals
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126//! One walk of an approved journal against a candidate (M1-HIST-*).//!//! The pair being compared and the list to report into travel together the//! whole way down -- journal, transaction, posting -- so they are one value//! rather than two parameters. Descending narrows the location and keeps the//! list: `inTransaction` and `inPosting` are that step.
const std = @import("std");const diag = @import("../diag.zig");
const Comparison = @This();
diags: *diag.List,/// The pair, narrowed to whatever part of it is being compared.location: diag.HistoryAt,
pub const Error = error{ Differs, OutOfMemory };
/// The same comparison, descended into one transaction of the pair.pub fn inTransaction(self: Comparison, index: usize) Comparison { return .{ .diags = self.diags, .location = self.location.inTransaction(index) };}
/// The same comparison, descended into one posting of the transaction.pub fn inPosting(self: Comparison, index: usize) Comparison { return .{ .diags = self.diags, .location = self.location.inPosting(index) };}
/// M1-HIST-010: records the difference against the current location and/// refuses. Every refusal in a comparison goes through here, so none of them/// can forget to say where it was.pub fn differs(self: Comparison, comptime fmt: []const u8, args: anytype) Error { self.diags.addHistory(self.location, fmt, args) catch return error.OutOfMemory; return error.Differs;}
/// A difference between two strings that M1-HIST-009 compares byte for byte.////// Printed plainly, two strings differing only in whitespace look identical,/// and the diagnostic reads like a fault in the comparison rather than a/// difference between the journals. So when that is what happened, say so and/// show the whitespace.////// This does not change what counts as equal. DESIGN_M1.md §11.3 records the/// open question of whether surrounding whitespace should be part of a/// narration at all; until that is settled the comparison stays exact, and/// this makes its refusals legible.pub fn textDiffers( self: Comparison, what: []const u8, approved: []const u8, candidate: []const u8,) Error { if (!differsOnlyInWhitespace(approved, candidate)) { return self.differs( "{s} differs: approved '{s}', candidate '{s}'", .{ what, approved, candidate }, ); }
return self.differs( "{s} differs only in whitespace: approved '{f}', candidate '{f}'" ++ " (" ++ space_mark ++ " space, " ++ tab_mark ++ " tab)", .{ what, Visible{ .text = approved }, Visible{ .text = candidate } }, );}
fn isBlank(byte: u8) bool { return byte == ' ' or byte == '\t' or byte == '\r' or byte == '\n';}
/// True when removing every whitespace byte from both makes them equal. That/// covers a difference at either end and one in the middle.fn differsOnlyInWhitespace(approved: []const u8, candidate: []const u8) bool { var approved_index: usize = 0; var candidate_index: usize = 0; while (true) { while (approved_index < approved.len and isBlank(approved[approved_index])) approved_index += 1; while (candidate_index < candidate.len and isBlank(candidate[candidate_index])) candidate_index += 1; if (approved_index == approved.len or candidate_index == candidate.len) break; if (approved[approved_index] != candidate[candidate_index]) return false; approved_index += 1; candidate_index += 1; } return approved_index == approved.len and candidate_index == candidate.len;}
const space_mark = "\u{00B7}"; // MIDDLE DOTconst tab_mark = "\u{2192}"; // RIGHTWARDS ARROW
/// The second rendering of a string: the one that shows what is otherwise/// invisible. STYLE.md §4 -- a named formatter, because the canonical/// rendering of a string is the string.const Visible = struct { text: []const u8,
pub fn format(self: Visible, writer: *std.Io.Writer) std.Io.Writer.Error!void { for (self.text) |byte| { switch (byte) { ' ' => try writer.writeAll(space_mark), '\t' => try writer.writeAll(tab_mark), '\r' => try writer.writeAll("\\r"), '\n' => try writer.writeAll("\\n"), else => try writer.writeByte(byte), } } }};
// -- tests -------------------------------------------------------------------
const expectEqual = std.testing.expectEqual;
test "M1-SAFE-004: differs reports exhaustion rather than the difference" { var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 }); var diags = diag.List.init(failing.allocator()); defer diags.deinit();
const comparison: Comparison = .{ .diags = &diags, .location = .{ .csv = "2026.csv", .journal = "2026.csv.journal" }, }; const outcome: Error = comparison.inTransaction(0).differs("differs", .{}); try expectEqual(Error.OutOfMemory, outcome);}