//! Diagnostics (M1-DIAG-001 .. M1-DIAG-004). //! //! Every module reports failures through this accumulator instead of returning //! on the first error. That is what M1-DIAG-004 asks for: a bad row, or a //! historical pair that fails to parse, must not hide independent errors that //! are still determinable from the same operation snapshot. //! //! Memory. `List` is given an arena. Formatted messages are allocated from it //! and never freed individually. Borrowed slices -- file paths, key paths -- //! must outlive the list; in practice they point into the operation snapshot, //! which is read once and immutable (M1-PROJ-015). const std = @import("std"); pub const Diagnostic = @import("Diagnostic.zig"); const LineCol = Diagnostic.LineCol; /// Where a historical difference was found (M1-DIAG-003). pub const HistoryAt = struct { /// CSV path relative to `inputPath`. csv: []const u8, /// The approved journal beside it. Together these identify the pair. journal: []const u8, posting: ?usize = null, /// Zero-based indices into the candidate journal. transaction: ?usize = null, /// The same pair, narrowed to one transaction within it. pub fn inTransaction(self: HistoryAt, index: usize) HistoryAt { var narrowed = self; narrowed.transaction = index; return narrowed; } /// The same location, narrowed to one posting within the transaction. pub fn inPosting(self: HistoryAt, index: usize) HistoryAt { var narrowed = self; narrowed.posting = index; return narrowed; } }; /// Where a configuration problem was found (M1-DIAG-002). pub const ConfigAt = struct { file: []const u8, /// Dotted key path, e.g. "profile.csv.fields.date". key_path: []const u8, line_col: ?LineCol = null, }; /// An accumulator. Order is insertion order and never sorted, so identical /// inputs produce identical diagnostics (M1-SAFE-001). pub const List = struct { arena: std.mem.Allocator, items: std.ArrayList(Diagnostic), pub fn init(arena: std.mem.Allocator) List { return .{ .arena = arena, .items = .empty }; } /// A list allocated in the arena it records into, for the callers that /// pass a `*List` on rather than keeping one on the stack. `init` is for /// those. pub fn create(arena: std.mem.Allocator) std.mem.Allocator.Error!*List { const list = try arena.create(List); list.* = init(arena); return list; } pub fn deinit(self: *List) void { self.items.deinit(self.arena); } pub fn isEmpty(self: List) bool { return self.items.items.len == 0; } pub fn count(self: List) usize { return self.items.items.len; } /// Appends verbatim. `d.message` is borrowed, not copied. pub fn add(self: *List, d: Diagnostic) std.mem.Allocator.Error!void { try self.items.append(self.arena, d); } pub fn addFmt(self: *List, comptime fmt: []const u8, args: anytype, opts: struct { file: ?[]const u8 = null, journal: ?[]const u8 = null, key_path: ?[]const u8 = null, line_col: ?LineCol = null, physical_row: ?usize = null, posting: ?usize = null, transaction: ?usize = null, }) std.mem.Allocator.Error!void { try self.add(.{ .message = try self.arena.print(fmt, args), .file = opts.file, .physical_row = opts.physical_row, .key_path = opts.key_path, .line_col = opts.line_col, .journal = opts.journal, .transaction = opts.transaction, .posting = opts.posting, }); } /// M1-DIAG-001. `file` is relative to `inputPath`; `physical_row` is the /// zero-based record ordinal. Both are required, so a row diagnostic /// cannot be built without them. pub fn addRow( self: *List, file: []const u8, physical_row: usize, comptime fmt: []const u8, args: anytype, ) std.mem.Allocator.Error!void { try self.items.append(self.arena, .{ .message = try self.arena.print(fmt, args), .file = file, .physical_row = physical_row, }); } /// M1-DIAG-002. File and key path are required. pub fn addConfig( self: *List, at: ConfigAt, comptime fmt: []const u8, args: anytype, ) std.mem.Allocator.Error!void { try self.items.append(self.arena, .{ .message = try self.arena.print(fmt, args), .file = at.file, .key_path = at.key_path, .line_col = at.line_col, }); } /// M1-DIAG-003. Both halves of the pair are required. pub fn addHistory( self: *List, at: HistoryAt, comptime fmt: []const u8, args: anytype, ) std.mem.Allocator.Error!void { try self.items.append(self.arena, .{ .message = try self.arena.print(fmt, args), .file = at.csv, .journal = at.journal, .transaction = at.transaction, .posting = at.posting, }); } }; // -- tests ------------------------------------------------------------------- test "M1-DIAG-001: a row diagnostic carries source path and physical row" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var list = List.init(arena.allocator()); try list.addRow("bank/2026.csv", 12, "amount cell is not a decimal: {s}", .{"N/A"}); const first_diag = list.items.items[0]; try std.testing.expectEqualStrings("bank/2026.csv", first_diag.file.?); try std.testing.expectEqual(@as(usize, 12), first_diag.physical_row.?); try std.testing.expectEqualStrings("amount cell is not a decimal: N/A", first_diag.message); } test "M1-DIAG-001: physical row zero is carried, not mistaken for absent" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var list = List.init(arena.allocator()); try list.addRow("a.csv", 0, "blank record", .{}); try std.testing.expectEqual(@as(usize, 0), list.items.items[0].physical_row.?); } test "M1-DIAG-002: a configuration diagnostic carries file and key path" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var list = List.init(arena.allocator()); try list.addConfig( .{ .file = "project.yaml", .key_path = "profile.csv.fields.date" }, "unknown key {s}", .{"headr"}, ); const first_diag = list.items.items[0]; try std.testing.expectEqualStrings("project.yaml", first_diag.file.?); try std.testing.expectEqualStrings("profile.csv.fields.date", first_diag.key_path.?); try std.testing.expect(first_diag.line_col == null); } test "M1-DIAG-002: a syntax diagnostic can carry libyaml's line and column" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var list = List.init(arena.allocator()); try list.addConfig( .{ .file = "rules.yaml", .key_path = "rules", .line_col = .{ .line = 7, .column = 3 }, }, "flow mappings are not supported", .{}, ); const first_diag = list.items.items[0]; try std.testing.expectEqual(@as(usize, 7), first_diag.line_col.?.line); try std.testing.expectEqual(@as(usize, 3), first_diag.line_col.?.column); } test "M1-DIAG-003: a historical diagnostic carries the pair and the differing data" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var list = List.init(arena.allocator()); try list.addHistory( .{ .csv = "bank/2025.csv", .journal = "bank/2025.csv.journal", .transaction = 3, .posting = 1, }, "account differs: approved {s}, candidate {s}", .{ "expenses:food", "expenses:groceries" }, ); const first_diag = list.items.items[0]; try std.testing.expectEqualStrings("bank/2025.csv", first_diag.file.?); try std.testing.expectEqualStrings("bank/2025.csv.journal", first_diag.journal.?); try std.testing.expectEqual(@as(usize, 3), first_diag.transaction.?); try std.testing.expectEqual(@as(usize, 1), first_diag.posting.?); } test "M1-DIAG-004: an error in one row does not mask errors in another" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var list = List.init(arena.allocator()); try list.addRow("bank/2026.csv", 4, "matches no rule", .{}); try list.addRow("bank/2026.csv", 9, "matches 2 rules", .{}); try list.addConfig(.{ .file = "rules.yaml", .key_path = "rules.food" }, "bad regex", .{}); try std.testing.expectEqual(@as(usize, 3), list.count()); try std.testing.expectEqual(@as(usize, 4), list.items.items[0].physical_row.?); try std.testing.expectEqual(@as(usize, 9), list.items.items[1].physical_row.?); try std.testing.expectEqualStrings("rules.yaml", list.items.items[2].file.?); } test "M1-DIAG-004: a historical pair failing does not stop later pairs reporting" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var list = List.init(arena.allocator()); try list.addHistory( .{ .csv = "a.csv", .journal = "a.csv.journal" }, "unsupported journal syntax", .{}, ); try list.addHistory( .{ .csv = "b.csv", .journal = "b.csv.journal", .transaction = 0 }, "narration differs", .{}, ); try std.testing.expectEqual(@as(usize, 2), list.count()); try std.testing.expectEqualStrings("a.csv", list.items.items[0].file.?); try std.testing.expectEqualStrings("b.csv", list.items.items[1].file.?); } test "M1-SAFE-001: identical input produces identical diagnostics in order" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var first = List.init(arena.allocator()); var second = List.init(arena.allocator()); for ([_]*List{ &first, &second }) |list| { try list.addRow("bank/2026.csv", 2, "matches no rule", .{}); try list.addConfig(.{ .file = "rules.yaml", .key_path = "rules.x" }, "bad regex", .{}); } try std.testing.expectEqual(first.count(), second.count()); for (first.items.items, second.items.items) |a, b| { try std.testing.expectEqualStrings(a.message, b.message); try std.testing.expectEqualDeep(a.file, b.file); try std.testing.expectEqualDeep(a.physical_row, b.physical_row); try std.testing.expectEqualDeep(a.key_path, b.key_path); } } test "an empty list reports itself empty" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); var list = List.init(arena.allocator()); try std.testing.expect(list.isEmpty()); try list.addRow("a.csv", 0, "x", .{}); try std.testing.expect(!list.isEmpty()); }