Something went wrong. Try again.
Convert CSV banking statements to hledger journals
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184//! The manifest's profile, prepared (M1-PROJ-018).//!//! Everything the engine needs from the manifest, resolved once: CSV dialect,//! field specs and templates. None of it depends on the rules file.//!//! A layout depends on the file's header, so the transaction mapping is//! resolved per file rather than here.
const std = @import("std");const Decimal = @import("../Decimal.zig");const diag = @import("../diag.zig");const fields = @import("../csv/fields.zig");const Manifest = @import("../config/Manifest.zig");const reader = @import("../csv/reader.zig");const row = @import("../row.zig");const schema = @import("../yaml/schema.zig");const template = @import("../template.zig");
pub const Error = error{ EngineFailed, OutOfMemory };
/// A configuration file as it was read: where it came from, and its bytes.pub const Source = struct { path: []const u8, text: []const u8,};
/// The file a template or condition came from, and the field aliases it may/// name. Both are needed to report a diagnostic against it.pub const Scope = struct { aliases: []const []const u8, file: []const u8,};
const Profile = @This();
aliases: []const []const u8,/// How numeric predicates read an amount cell (M1-RULE-016).amount_format: Decimal.Format,csv: reader.Options,destination_comment: ?template.Template,field_specs: []const fields.FieldSpec,header: bool,narration: template.Template,skip_records: usize,source_account: []const u8,source_comment: ?template.Template,/// The layout depends on the header, so the transaction mapping is/// resolved per file rather than once.transaction: schema.Cursor,
pub fn rowOptions(self: Profile) fields.Options { return .{ .reader = self.csv, .skip_records = self.skip_records, .header = self.header, .fields = self.field_specs, };}
/// M1-PROJ-018: anything malformed, incomplete or inconsistent in the/// manifest fails here.pub fn load(arena: std.mem.Allocator, manifest_source: Source, diags: *diag.List) Error!Profile { const manifest_path = manifest_source.path; const loaded = Manifest.load(arena, manifest_path, manifest_source.text, diags) catch return error.EngineFailed;
const profile = loaded.root.child("profile") catch return error.EngineFailed; const csv = profile.child("csv") catch return error.EngineFailed;
const delimiter = try enumOf(csv, "delimiter", &.{ "comma", "semicolon", "tab" }); const line_ending = try enumOf(csv, "lineEnding", &.{ "lf", "crlf" }); const skip = (csv.child("skipRecords") catch return error.EngineFailed) .unsignedInteger() catch return error.EngineFailed; const header = (csv.child("header") catch return error.EngineFailed) .boolean() catch return error.EngineFailed;
const field_specs = try loadFieldSpecs(arena, csv, diags); const aliases = try arena.alloc([]const u8, field_specs.len); for (field_specs, 0..) |spec, index| aliases[index] = spec.alias;
const transaction = profile.child("transaction") catch return error.EngineFailed; const source_account = (transaction.child("sourceAccount") catch return error.EngineFailed) .string() catch return error.EngineFailed;
const amount_format = row.ColumnMapping.loadAmountFormat(transaction) catch return error.EngineFailed;
const narration = try parseTemplate( arena, transaction, "narrationTemplate", .{ .file = manifest_path, .aliases = aliases }, diags, );
var source_comment: ?template.Template = null; var destination_comment: ?template.Template = null; if (transaction.optionalChild("postingCommentTemplates") catch return error.EngineFailed) |comments| { source_comment = try optionalTemplate(arena, comments, "source", .{ .file = manifest_path, .aliases = aliases }, diags); destination_comment = try optionalTemplate(arena, comments, "destination", .{ .file = manifest_path, .aliases = aliases }, diags); }
return .{ .csv = .{ .delimiter = if (std.mem.eql(u8, delimiter, "comma")) .comma else if (std.mem.eql(u8, delimiter, "semicolon")) .semicolon else .tab, .line_ending = if (std.mem.eql(u8, line_ending, "lf")) .lf else .crlf, }, .skip_records = skip, .header = header, .field_specs = field_specs, .aliases = aliases, .transaction = transaction, .source_account = source_account, .amount_format = amount_format, .narration = narration, .source_comment = source_comment, .destination_comment = destination_comment, };}
// -- configuration helpers ---------------------------------------------------
fn enumOf( parent: schema.Cursor, key: []const u8, allowed: []const []const u8,) Error![]const u8 { const cursor = parent.child(key) catch return error.EngineFailed; return cursor.enumValue(allowed) catch error.EngineFailed;}
fn loadFieldSpecs( arena: std.mem.Allocator, csv: schema.Cursor, diags: *diag.List,) Error![]const fields.FieldSpec { _ = diags; const cursor = csv.child("fields") catch return error.EngineFailed; const mapping = cursor.mappingNode() catch return error.EngineFailed;
const specs = try arena.alloc(fields.FieldSpec, mapping.entries.len); for (mapping.entries, 0..) |entry, index| { const field = cursor.child(entry.key) catch return error.EngineFailed; // M1-YAML-012 fixed the shape; this reads whichever selector is there. const selector: fields.Selector = if (field.optionalChild("index") catch return error.EngineFailed) |by_index| .{ .index = by_index.unsignedInteger() catch return error.EngineFailed } else .{ .header = (field.child("header") catch return error.EngineFailed) .string() catch return error.EngineFailed }; specs[index] = .{ .alias = entry.key, .selector = selector }; } return specs;}
fn parseTemplate( arena: std.mem.Allocator, parent: schema.Cursor, key: []const u8, scope: Scope, diags: *diag.List,) Error!template.Template { const file = scope.file; const aliases = scope.aliases; const cursor = parent.child(key) catch return error.EngineFailed; const text = cursor.string() catch return error.EngineFailed; return template.parse(arena, text, aliases, .{ .file = file, .key_path = cursor.path, }, diags) catch error.EngineFailed;}
fn optionalTemplate( arena: std.mem.Allocator, parent: schema.Cursor, key: []const u8, scope: Scope, diags: *diag.List,) Error!?template.Template { if ((parent.optionalChild(key) catch return error.EngineFailed) == null) return null; return try parseTemplate(arena, parent, key, scope, diags);}