const Amount = @import("Amount.zig"); const std = @import("std"); const Posting = @This(); const Comparison = @import("Comparison.zig"); const Error = Comparison.Error; account: []const u8, /// Null means the amount is left for hledger to infer, which M1-JRN-005 /// requires of the destination posting. Presence is part of structural /// equality (M1-HIST-007), so an omitted amount never equals an explicit /// one. amount: ?Amount, /// The decoded payload: no indentation, no delimiter, otherwise exact /// (M1-JRN-009). Null means no comment was configured or it rendered /// empty. comment: ?[]const u8, pub const ParseResult = union(enum) { account_problem: []const u8, success: Posting, }; pub fn parse(account: []const u8, amount: ?Amount, comment: ?[]const u8) ParseResult { if (Posting.accountProblem(account)) |p| { return .{ .account_problem = p }; } return .{ .success = .{ .account = account, .amount = amount, .comment = comment } }; } pub fn compare(self: Posting, other: Posting, at: Comparison) Error!void { if (!std.mem.eql(u8, self.account, other.account)) { return at.textDiffers("account", self.account, other.account); } // M1-HIST-007: presence, not only value. An inferred amount is not an // explicit one that happens to hold the same number. if ((self.amount == null) != (other.amount == null)) { return at.differs("one posting states an amount and the other leaves it inferred", .{}); } if (self.amount) |approved_amount| { const candidate_amount = other.amount.?; if (approved_amount.currency != candidate_amount.currency) { return at.differs( "currency differs: approved {f}, candidate {f}", .{ approved_amount.currency, candidate_amount.currency }, ); } // M1-HIST-008: by mathematical value, so scale and sign spelling do // not matter. if (approved_amount.quantity.order(candidate_amount.quantity) != .eq) { return at.differs( "amount differs: approved {f}, candidate {f}", .{ approved_amount.quantity, candidate_amount.quantity }, ); } } if ((self.comment == null) != (other.comment == null)) { return at.differs("one posting carries a comment and the other does not", .{}); } if (self.comment) |approved_comment| { // M1-HIST-009: exact bytes, so trailing whitespace counts. if (!std.mem.eql(u8, approved_comment, other.comment.?)) { return at.textDiffers("comment", approved_comment, other.comment.?); } } } /// Posting lines are indented; hledger needs the leading whitespace. const indent = " "; /// hledger needs at least two spaces between an account and what follows it. const gap = " "; /// The journal line for this posting, without its newline: the only rendering /// a posting has, so it is the one `{f}` gives. pub fn format(self: Posting, writer: *std.Io.Writer) std.Io.Writer.Error!void { try writer.print("{s}{s}", .{ indent, self.account }); // M1-JRN-004 on the source posting; M1-JRN-005 leaves the destination's // amount out so hledger infers it. if (self.amount) |amount| { try writer.print("{s}{f} {f}", .{ gap, amount.quantity, amount.currency }); } // M1-TPL-010: an empty payload emits no comment syntax at all, so that an // absent comment and one that rendered empty produce the same output and // therefore compare equal. if (self.comment) |payload| { if (payload.len > 0) { try writer.print("{s}; {s}", .{ gap, payload }); } } } /// M1-JRN-006, with DESIGN_M1.md D4's reject set. Returns why the name is /// unusable, or null when it is fine. pub fn accountProblem(name: []const u8) ?[]const u8 { if (name.len == 0) return "must not be empty"; // hledger reads these as virtual postings, which M1-JRN-006 excludes. if (name[0] == '(' or name[0] == '[') return "virtual postings are not supported"; if (isBlank(name[0])) return "must not begin with whitespace"; if (isBlank(name[name.len - 1])) return "must not end with whitespace"; for (name, 0..) |c, position| { if (c == ';') return "must not contain ';', which opens a comment"; if (c < 0x20) return "must not contain a control character"; if (c == ' ' and position + 1 < name.len and name[position + 1] == ' ') { return "must not contain two consecutive spaces, which start the amount"; } } return null; } fn isBlank(c: u8) bool { return c == ' ' or c == '\t'; } // -- tests ------------------------------------------------------------------- const expect = std.testing.expect; test "M1-JRN-006: an account must be non-empty and ordinary" { try expect(accountProblem("assets:bank:checking") == null); try expect(accountProblem("a") == null); // Empty. try expect(accountProblem("") != null); // Virtual postings. try expect(accountProblem("(assets:bank)") != null); try expect(accountProblem("[assets:bank]") != null); // A semicolon would open a comment. try expect(accountProblem("assets;bank") != null); // Two spaces would start the amount field. try expect(accountProblem("assets bank") != null); // Leading or trailing whitespace is not part of a name. try expect(accountProblem(" assets") != null); try expect(accountProblem("assets ") != null); try expect(accountProblem("assets\tbank") != null); // Line breaks and controls. try expect(accountProblem("assets\nbank") != null); try expect(accountProblem("assets\x01bank") != null); }