Something went wrong. Try again.
Convert CSV banking statements to hledger journals
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215//! Command line (M1-CLI-001 .. M1-CLI-008).//!//! Three forms, one required argument, and one option that belongs to//! `interactive` alone. M1-CLI-007 forbids any option that would bypass the//! exactly-one-rule invariant, historical validation, the saved-rules//! requirement or the no-overwrite rule -- so the parser accepts no flag//! beyond --listen, and a test names the ones that must never appear.//!//! `convert` performs everything `check` does before it writes anything//! (M1-CLI-005), which here is literal: the same code runs, and only the last//! step differs.
const std = @import("std");const diag = @import("diag.zig");const discover = @import("discover.zig");const engine = @import("engine.zig");const Target = @import("Target.zig");const Manifest = @import("config/Manifest.zig");const Snapshot = @import("Snapshot.zig");const server = @import("ui/server.zig");const Endpoint = server.Endpoint;const Session = @import("ui/Session.zig");const Writer = std.Io.Writer;
pub const Invocation = @import("cli/Invocation.zig");
pub const usage = \\usage: \\ csv2journal check MANIFEST \\ csv2journal convert MANIFEST \\ csv2journal interactive MANIFEST [--listen HOST:PORT] \\;
pub fn execute( arena: std.mem.Allocator, io: std.Io, writer: *Writer, base: std.Io.Dir, invocation: Invocation,) std.Io.Writer.Error!bool { var diags = diag.List.init(arena); var progress: Progress = .{};
const execution: Execution = .{ .arena = arena, .io = io, .base = base, .diags = &diags, .progress = &progress, }; const failure: bool = if (execution.runCommand(invocation)) false else |_| true; const report: Report = .{ .invocation = invocation, .progress = progress, .diags = &diags }; try writer.print("{f}", .{report}); return failure;}
/// What the run managed to determine, for the report. Populated as far as the/// command gets, so M1-CLI-004's "all known row-level results" survive a/// failure.const Progress = struct { current_path: []const u8 = "", history_checked: bool = false, history_pairs: usize = 0, rows: []const engine.RowResult = &.{}, wrote_target: ?[]const u8 = null,};
/// What one command run is given: where it reads and writes, and where its/// diagnostics and progress accumulate.const Execution = struct { arena: std.mem.Allocator, base: std.Io.Dir, diags: *diag.List, io: std.Io, progress: *Progress,
/// Every command loads the manifest; what it does next is the command. fn runCommand(self: Execution, invocation: Invocation) !void { const manifest = try Manifest.loadFromFileVerified( self.arena, self.io, self.base, invocation.manifest_path, self.diags, );
return switch (invocation.command) { .interactive => |endpoint| self.serveInteractively(manifest, endpoint), // M1-CLI-003: check is convert as far as the writing, and no further, // so the two share a body that stops where they differ. .check, .convert => self.runPipeline(invocation, manifest), }; }
fn serveInteractively(self: Execution, manifest: Manifest, endpoint: Endpoint) !void { const session = try Session.open(self.arena, self.io, self.base, &manifest, self.diags);
// M1-UI-001: binding failure is a startup failure, never a move to some // other port. server.serve(self.arena, self.io, endpoint, session) catch { const url = try self.arena.print("{f}", .{endpoint.url()}); try self.addFailure(url, "cannot be listened on"); return error.CannotListen; }; }
fn runPipeline(self: Execution, invocation: Invocation, manifest: Manifest) !void { const arena = self.arena; const io = self.io; const diags = self.diags;
var root = self.base.openDir(io, manifest.input_path, .{ .iterate = true }) catch |err| { try self.addFailure(manifest.input_path, "cannot be opened"); return err; }; defer root.close(io);
const found = try discover.discover(arena, io, root, diags); self.progress.current_path = found.current; self.progress.history_pairs = found.history.len;
const snap = try Snapshot.read(arena, io, .{ .base = self.base, .manifest_path = invocation.manifest_path, .rules_path = manifest.rules_file, .root = root, .found = found, }, diags);
const candidate = try engine.prepare( arena, .{ .path = invocation.manifest_path, .text = snap.manifest }, .{ .path = manifest.rules_file, .text = snap.rules }, diags, );
// M1-CLI-004: the status of every row, whether or not the run can proceed. self.progress.rows = (try engine.classify(arena, candidate, snap.current_path, snap.current_csv, diags)).rows;
for (self.progress.rows) |result| { if (result.status != .one) return error.OneRowHasUnequalOneMatches; }
// M1-HIST-001: every pair, every time. Checked even when the current // import already failed, so one run reports both (M1-DIAG-004). try engine.validateHistory(arena, candidate, snap.history, diags); self.progress.history_checked = true;
// M1-CLI-003: check stops here, having written nothing. if (invocation.command == .check) { return; }
const generated = try engine.generate(arena, candidate, snap.current_path, snap.current_csv, diags); const text = try arena.print("{f}", .{generated}); const target = try arena.print("{s}.journal", .{snap.current_path}); const journal: Target = .{ .root = root, .path = target, .contents = text }; try journal.finalize(arena, io, diags);
self.progress.wrote_target = target; }
fn addFailure(self: Execution, path: []const u8, what: []const u8) !void { try self.diags.addFmt("'{s}' {s}", .{ path, what }, .{ .file = path }); }};
// -- reporting ---------------------------------------------------------------
/// What one run has to say, as a value: the row results, the history tally,/// the diagnostics and whether anything was written. STYLE.md 4 -- a second/// rendering that is not a type's canonical one is a named formatter, so this/// nests through `{f}` like anything else.const Report = struct { diags: *diag.List, invocation: Invocation, progress: Progress,
pub fn format(self: Report, writer: *Writer) Writer.Error!void { // M1-CLI-004: the match status of every evaluated row. for (self.progress.rows) |result| { try writer.print("{s}:{d}: ", .{ self.progress.current_path, result.physical_row }); switch (result.status) { .one => { try writer.writeAll(result.matches[0]); try writer.writeAll("\n"); }, .zero => try writer.writeAll("no rule matches\n"), .multiple => { try writer.writeAll("ambiguous:"); for (result.matches) |name| try writer.print(" {s}", .{name}); try writer.writeAll("\n"); }, .unavailable => try writer.writeAll("cannot be classified\n"), } }
if (self.progress.history_checked) { try writer.print("history: {d} pair(s) validated\n", .{self.progress.history_pairs}); }
// M1-CLI-008: wording is not a stable interface, but the context is. for (self.diags.items.items) |d| { try writer.print("{f}", .{d}); }
if (self.progress.wrote_target) |target| { try writer.print("wrote {s}\n", .{target}); } else if (self.invocation.command == .convert and self.diags.isEmpty()) { try writer.writeAll("nothing was written\n"); } }};