//! Writes the hledger compatibility corpus to a directory (M1-JRN-008). //! //! Built only by `zig build compat-corpus`, never installed beside the //! application: M1-PKG-007 makes hledger a test dependency, and nothing in a //! run may depend on it or on this. const std = @import("std"); const csv2journal = @import("csv2journal"); /// Everything `main` can propagate: argument and arena allocation, writing the /// usage text, and the directory and file work of writing or verifying. const Error = error{ OutOfMemory, Unexpected, WriteFailed } || std.Io.Dir.CreateDirPathError || std.Io.Dir.OpenError || std.Io.Dir.WriteFileError || std.Io.Dir.ReadFileAllocError; pub fn main(init: std.process.Init) Error!u8 { const arena = init.arena.allocator(); const io = init.io; const argv = try init.minimal.args.toSlice(arena); const verifying = argv.len == 3 and std.mem.eql(u8, argv[1], "--verify"); if (argv.len != 2 and !verifying) { try emit(io, std.Io.File.stderr(), "usage: compat-corpus [--verify] \n"); return 2; } const path = if (verifying) argv[2] else argv[1]; const base = std.Io.Dir.cwd(); if (!verifying) try base.createDirPath(io, path); var out = try base.openDir(io, path, .{}); defer out.close(io); return if (verifying) verify(arena, io, out) else write(arena, io, out); } fn write(arena: std.mem.Allocator, io: std.Io, out: std.Io.Dir) !u8 { for (csv2journal.compat.cases) |case| { const text = try arena.print("{f}", .{case.journal()}); const name = try arena.print("{s}.journal", .{case.name}); try out.writeFile(io, .{ .sub_path = name, .data = text }); } return 0; } /// Reads hledger's own re-print of each case and checks it still says what we /// meant. Parsing is hledger's half of M1-JRN-007; this is ours. fn verify(arena: std.mem.Allocator, io: std.Io, out: std.Io.Dir) !u8 { var failures: usize = 0; for (csv2journal.compat.cases) |case| { if (!case.hledger_roundtrip) continue; const name = try arena.print("{s}.hledger", .{case.name}); const text = try out.readFileAlloc(io, name, arena, .unlimited); var diags = csv2journal.diag.List.init(arena); const reprinted = csv2journal.Journal.parse.parse(arena, name, text, &diags) catch { try report(arena, io, case.name, "hledger's output does not parse", &diags); failures += 1; continue; }; reprinted.compare( .{ .transactions = case.transactions }, .{ .diags = &diags, .location = .{ .csv = case.name, .journal = name } }, ) catch { try report(arena, io, case.name, "hledger changed the journal", &diags); failures += 1; }; } return if (failures == 0) 0 else 1; } fn report( arena: std.mem.Allocator, io: std.Io, name: []const u8, what: []const u8, diags: *csv2journal.diag.List, ) !void { try emit(io, std.Io.File.stderr(), try arena.print( "{s}: {s}\n", .{ name, what }, )); for (diags.items.items) |d| { try emit(io, std.Io.File.stderr(), try arena.print(" {s}\n", .{d.message})); } } fn emit(io: std.Io, file: std.Io.File, text: []const u8) !void { var buffer: [256]u8 = undefined; var file_writer = file.writer(io, &buffer); try file_writer.interface.writeAll(text); try file_writer.interface.flush(); }