Something went wrong. Try again.
Convert CSV banking statements to hledger journals
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172//! Producing a journal from a prepared candidate (M1-UI-014, M1-UI-021).//!//! Split out of session.zig because it is a different job: the session decides//! whether a journal may be produced, and this decides what it says and where//! it goes. `text` does not write, and `write` is the only thing here that//! does, which is where M1-UI-014's "MUST NOT be written to the target path"//! is kept.//!//! M1-UI-021 asks interactive finalization to behave exactly as `convert`//! does. It does so by being the same two calls in the same order://! `engine.generate` then `Target.finalize`.//!//! `text` and `write` are deliberately not folded back into their two call//! sites in Session.zig. The seam is one function wide -- `Session.output`//! gathers the six values and nothing else crosses -- and keeping it is what//! stops the session from depending on `Target` and `engine.generate`//! directly.
const std = @import("std");const diag = @import("../diag.zig");const engine = @import("../engine.zig");const Target = @import("../Target.zig");
pub const Error = error{ OutputFailed, OutOfMemory };
/// Everything producing a journal needs, gathered by the caller so that/// nothing here reaches back into the session.const Output = @This();
/// Resolves `input_path`; the CSV path is relative to that root.base: std.Io.Dir,candidate: engine.Candidate,csv_path: []const u8,csv_text: []const u8,input_path: []const u8,io: std.Io,
/// The complete journal, or a failure with the reasons in `diags`. Never a/// partial journal: a journal missing some of its transactions reads/// exactly like a complete one (M1-UI-014).pub fn text(self: Output, arena: std.mem.Allocator, diags: *diag.List) Error![]const u8 { const generated = engine.generate( arena, self.candidate, self.csv_path, self.csv_text, diags, ) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, error.EngineFailed => return error.OutputFailed, }; return arena.print("{f}", .{generated});}
/// Writes the journal beside its CSV, atomically and without overwriting/// (M1-UI-021). The input root is opened here rather than held open for/// the life of a session that lasts as long as someone leaves the page/// open.pub fn write(self: Output, arena: std.mem.Allocator, diags: *diag.List) Error!void { const contents = try self.text(arena, diags);
var root = self.base.openDir(self.io, self.input_path, .{}) catch { try diags.addFmt("input root '{s}' can no longer be opened", .{self.input_path}, .{ .file = self.input_path }); return error.OutputFailed; }; defer root.close(self.io);
const target = try arena.print("{s}.journal", .{self.csv_path}); const journal: Target = .{ .root = root, .path = target, .contents = contents }; journal.finalize(arena, self.io, diags) catch return error.OutputFailed;}