Something went wrong. Try again.
Convert CSV banking statements to hledger journals
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438//! The read-only state view (M1-UI-005, 006, 007, 011, 012, 013).//!//! Filtering and searching happen here rather than in the browser. M1-UI-006//! fixes their semantics precisely -- a case-sensitive UTF-8 byte substring//! over every cell, with an empty query showing everything the status filter//! allows -- and a rule stated that exactly deserves tests. Doing it in the//! client would put it where this project has no way to test it, and would//! make the client larger for no gain: on loopback the extra round trip costs//! nothing.//!//! DESIGN_M1.md §6 originally placed both in the client; this is a deliberate//! departure, recorded there.
const std = @import("std");const diag = @import("../diag.zig");const Editor = @import("Editor.zig");const engine = @import("../engine.zig");const fields = @import("../csv/fields.zig");const Session = @import("Session.zig");
pub const Error = error{OutOfMemory};
pub const Query = struct { search: []const u8 = "", /// M1-UI-005: a row is shown when its status is one of these, so an /// empty set shows no row at all. statuses: std.EnumSet(engine.RowStatus) = .full,};
/// Everything the interface shows, already computed by the engine.pub const View = struct { /// False when the candidate could not be prepared. M1-PROJ-018 lets /// interactive mode start anyway so the errors can be seen and corrected. configured: bool, diagnostics: []const diag.Diagnostic, /// M1-UI-015: the draft differs from the last saved rules. dirty: bool, /// The column each alias resolved to in the current file, so the /// interface can show which cell a rule's alias names. Empty whenever /// `rows` is: the layout is only known once the file has been read. fields: []const fields.Column, file: []const u8, /// M1-UI-019, M1-UI-020: every check finalization needs that can be /// decided without writing -- valid rules, saved rules, every row matched /// exactly once, history still reproduced. Whether the target path is /// free is not among them: that is decided atomically when writing. finalizable: bool, history_ok: bool, history_pairs: usize, rows: []const engine.RowResult, /// M1-UI-008: the draft itself, so the interface edits fields rather than /// text. rules: []const Editor.Rule, /// M1-UI-016, M1-UI-019: the draft has at least one rule and validates, /// so the save action is available. Row matching and historical equality /// deliberately do not appear here. savable: bool, /// Before filtering, so the interface can say how much is hidden. total_rows: usize,
/// The aliases a condition can name, for completing one as it is typed. pub fn aliases(self: View, arena: std.mem.Allocator) Error![]const []const u8 { const names = try arena.alloc([]const u8, self.fields.len); for (self.fields, names) |column, *name| name.* = column.field.alias; return names; }};
/// M1-UI-006: exact bytes, any cell. An empty query matches everything.pub fn matches(cells: []const []const u8, search: []const u8) bool { if (search.len == 0) return true; for (cells) |cell| { if (std.mem.find(u8, cell, search) != null) return true; } return false;}
pub fn filter( arena: std.mem.Allocator, rows: []const engine.RowResult, query: Query,) Error![]const engine.RowResult { var kept: std.ArrayList(engine.RowResult) = .empty; for (rows) |row| { if (!query.statuses.contains(row.status)) continue; if (!matches(row.cells, query.search)) continue; try kept.append(arena, row); } return kept.items;}
pub fn parseQuery(arena: std.mem.Allocator, target: []const u8) Error!Query { const mark = std.mem.findScalar(u8, target, '?') orelse return .{};
var query: Query = .{}; var pairs = std.mem.splitScalar(u8, target[mark + 1 ..], '&'); while (pairs.next()) |pair| { const equals = std.mem.findScalar(u8, pair, '=') orelse continue; const key = pair[0..equals]; const value = try decode(arena, pair[equals + 1 ..]);
if (std.mem.eql(u8, key, "q")) query.search = value; if (std.mem.eql(u8, key, "status")) query.statuses = statusesOf(value); } return query;}
/// A comma-separated list. An empty one checks nothing, which is how the/// interface asks for no rows; leaving `status` out shows every row instead./// A word naming no status is skipped rather than failing the request.fn statusesOf(text: []const u8) std.EnumSet(engine.RowStatus) { var statuses: std.EnumSet(engine.RowStatus) = .empty; var words = std.mem.splitScalar(u8, text, ','); while (words.next()) |word| { if (std.meta.stringToEnum(engine.RowStatus, word)) |status| statuses.insert(status); } return statuses;}
fn decode(arena: std.mem.Allocator, text: []const u8) Error![]const u8 { var out: std.ArrayList(u8) = .empty; var index: usize = 0; while (index < text.len) { switch (text[index]) { '+' => { try out.append(arena, ' '); index += 1; }, '%' => { const high = if (index + 1 < text.len) hexOf(text[index + 1]) else null; const low = if (index + 2 < text.len) hexOf(text[index + 2]) else null; if (high != null and low != null) { try out.append(arena, high.? * 16 + low.?); index += 3; } else { // Not an escape after all; take it literally. try out.append(arena, '%'); index += 1; } }, else => { try out.append(arena, text[index]); index += 1; }, } } return out.items;}
fn hexOf(c: u8) ?u8 { return switch (c) { '0'...'9' => c - '0', 'a'...'f' => c - 'a' + 10, 'A'...'F' => c - 'A' + 10, else => null, };}
// -- the payload -------------------------------------------------------------
const FieldView = struct { alias: []const u8, column: usize, /// Serialises as the profile writes it: `{"header":"…"}` or `{"index":N}`. selector: fields.Selector,};
const RowView = struct { cells: []const []const u8, row: usize, /// M1-UI-007: every matching rule, not just how many. rules: []const []const u8, status: []const u8,};
/// M1-UI-012 and M1-UI-013: whatever context the diagnostic carries reaches/// the interface, so a historical row names its file, row and pair.const DiagnosticView = struct { file: ?[]const u8, journal: ?[]const u8, key: ?[]const u8, message: []const u8, posting: ?usize, row: ?usize, transaction: ?usize,};
fn diagnosticViews( arena: std.mem.Allocator, diagnostics: []const diag.Diagnostic,) Error![]const DiagnosticView { const views = try arena.alloc(DiagnosticView, diagnostics.len); for (diagnostics, 0..) |d, index| { views[index] = .{ .message = d.message, .file = d.file, .row = d.physical_row, .key = d.key_path, .journal = d.journal, .transaction = d.transaction, .posting = d.posting, }; } return views;}
const Payload = struct { configured: bool, diagnostics: []const DiagnosticView, dirty: bool, fields: []const FieldView, file: []const u8, finalizable: bool, history_ok: bool, history_pairs: usize, rows: []const RowView, rules: []const Editor.Wire, savable: bool, shown_rows: usize, total_rows: usize,};
const PreviewPayload = struct { diagnostics: []const DiagnosticView, /// M1-UI-014: false means this is not what finalizing would write. final: bool, /// Null rather than a fragment: M1-UI-014 would rather show nothing than /// something that reads like a complete journal but is not. journal: ?[]const u8,};
pub fn renderPreview(arena: std.mem.Allocator, preview: Session.Preview) Error![]const u8 { const payload: PreviewPayload = .{ .journal = preview.journal, .final = preview.final, .diagnostics = try diagnosticViews(arena, preview.diagnostics), }; return arena.print("{f}", .{std.json.fmt(payload, .{})});}
pub fn renderJson(arena: std.mem.Allocator, view: View, query: Query) Error![]const u8 { const shown = try filter(arena, view.rows, query);
const rows = try arena.alloc(RowView, shown.len); for (shown, 0..) |row, index| { rows[index] = .{ .row = row.physical_row, .status = @tagName(row.status), .rules = row.matches, .cells = row.cells, }; }
const diagnostics = try diagnosticViews(arena, view.diagnostics);
const resolved = try arena.alloc(FieldView, view.fields.len); for (view.fields, 0..) |column, index| { resolved[index] = .{ .alias = column.field.alias, .column = column.index, .selector = column.field.selector, }; }
const drafted = try arena.alloc(Editor.Wire, view.rules.len); for (view.rules, 0..) |rule, index| { drafted[index] = .{ .name = rule.name, .match = rule.match, .targetAccount = rule.target_account, .sourceAccount = rule.source_account, }; }
const payload: Payload = .{ .file = view.file, .configured = view.configured, .dirty = view.dirty, .savable = view.savable, .finalizable = view.finalizable, .total_rows = view.total_rows, .shown_rows = shown.len, .history_pairs = view.history_pairs, .history_ok = view.history_ok, .rows = rows, .fields = resolved, .rules = drafted, .diagnostics = diagnostics, };
return arena.print("{f}", .{std.json.fmt(payload, .{})});}
// -- tests -------------------------------------------------------------------
const expect = std.testing.expect;const expectEqual = std.testing.expectEqual;const expectEqualStrings = std.testing.expectEqualStrings;
fn rowOf( number: usize, status: engine.RowStatus, rules: []const []const u8, cells: []const []const u8,) engine.RowResult { return .{ .physical_row = number, .status = status, .matches = rules, .cells = cells };}
const sample_rows = [_]engine.RowResult{ rowOf(1, .one, &.{"food"}, &.{ "2026-09-11", "-1.50", "REWE Markt" }), rowOf(2, .zero, &.{}, &.{ "2026-09-12", "-9.99", "Unknown Shop" }), rowOf(3, .multiple, &.{ "food", "big" }, &.{ "2026-09-13", "-800.00", "markt" }), rowOf(4, .unavailable, &.{}, &.{ "2026-09-14", "N/A", "Mystery" }),};
test "M1-UI-005: every status can be filtered for" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator();
try expectEqual(@as(usize, 4), (try filter(alloc, &sample_rows, .{})).len); inline for (.{ .one, .zero, .multiple, .unavailable }) |status| { const shown = try filter(alloc, &sample_rows, .{ .statuses = .initOne(status) }); try expectEqual(@as(usize, 1), shown.len); try expectEqual(@as(engine.RowStatus, status), shown[0].status); }}
test "M1-UI-005: a row is shown when its status is any of those checked" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator();
const shown = try filter(alloc, &sample_rows, .{ .statuses = .initMany(&.{ .zero, .one }) }); try expectEqual(@as(usize, 2), shown.len); try expectEqual(@as(usize, 1), shown[0].physical_row); try expectEqual(@as(usize, 2), shown[1].physical_row);}
test "M1-UI-005: with no status checked, no row is shown" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator();
try expectEqual(@as(usize, 0), (try filter(alloc, &sample_rows, .{ .statuses = .empty })).len);}
test "M1-UI-006: an empty query shows every row the filter allows" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator(); try expectEqual(@as(usize, 4), (try filter(alloc, &sample_rows, .{ .search = "" })).len); try expectEqual( @as(usize, 1), (try filter(alloc, &sample_rows, .{ .statuses = .initOne(.zero), .search = "" })).len, );}
test "M1-UI-006: a row matches when any cell contains the query" { try expect(matches(sample_rows[0].cells, "Markt")); try expect(matches(sample_rows[0].cells, "-1.50")); try expect(matches(sample_rows[0].cells, "2026-09-11")); try expect(!matches(sample_rows[0].cells, "absent"));}
test "M1-UI-006: the search is case-sensitive" { // "markt" appears lowercase only in row 3, capitalised only in row 1. try expect(matches(sample_rows[2].cells, "markt")); try expect(!matches(sample_rows[0].cells, "markt")); try expect(matches(sample_rows[0].cells, "Markt"));}
test "M1-UI-006: the search is a UTF-8 byte substring, not a word match" { const cells: []const []const u8 = &.{ "caf\u{00E9} noir", "x" }; try expect(matches(cells, "af\u{00E9} no")); try expect(matches(cells, "\u{00E9}")); // The decomposed form is different bytes and does not match. try expect(!matches(cells, "e\u{0301}"));}
test "M1-UI-006: filter and search combine" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator();
const found = try filter(alloc, &sample_rows, .{ .statuses = .initOne(.multiple), .search = "markt" }); try expectEqual(@as(usize, 1), found.len); try expectEqual(@as(usize, 3), found[0].physical_row);
// The same search under a filter that excludes that row yields nothing. const none = try filter(alloc, &sample_rows, .{ .statuses = .initOne(.zero), .search = "markt" }); try expectEqual(@as(usize, 0), none.len);}
test "M1-UI-005: the query string selects the filter and the search" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator();
const all = try parseQuery(alloc, "/api/state"); try expect(all.statuses.eql(.full)); try expectEqualStrings("", all.search);
const zero = try parseQuery(alloc, "/api/state?status=zero"); try expect(zero.statuses.eql(.initOne(.zero)));
const searched = try parseQuery(alloc, "/api/state?q=markt&status=multiple"); try expect(searched.statuses.eql(.initOne(.multiple))); try expectEqualStrings("markt", searched.search);}
test "M1-UI-005: the query string names several statuses, separated by commas" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator();
const query = try parseQuery(alloc, "/api/state?status=zero,one,unavailable"); try expect(query.statuses.eql(.initMany(&.{ .zero, .one, .unavailable })));}
test "M1-UI-005: an empty status in the query string checks nothing" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator();
const query = try parseQuery(alloc, "/api/state?status="); try expect(query.statuses.eql(.empty));}
test "M1-UI-005: an unknown status in the query string is ignored" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const alloc = arena.allocator();
const query = try parseQuery(alloc, "/api/state?status=zero,bogus"); try expect(query.statuses.eql(.initOne(.zero)));}