//! Preparing a candidate from configuration (M1-PROJ-018, M1-RULE-013). //! //! Everything the engine needs, resolved once: the profile, and rules whose //! expressions and regexes are already compiled. //! //! M1-RULE-013 decides the shape. An unusable expression, regex or alias makes //! the whole candidate unable to classify anything, so preparation fails //! rather than handing back something that cannot be used. An unusable //! *action* does not: that rule keeps its condition and loses only its //! ability to generate. const std = @import("std"); const diag = @import("../diag.zig"); const eval = @import("../expr/eval.zig"); const parser = @import("../expr/parser.zig"); const Profile = @import("Profile.zig"); const config_rules = @import("../config/rules.zig"); const Decimal = @import("../Decimal.zig"); pub const Error = Profile.Error; const PreparedRule = struct { /// Null when the action was unusable. The condition still runs /// (M1-RULE-013); only generation is blocked. action: ?config_rules.Action, name: []const u8, program: eval.Program, }; const Candidate = @This(); profile: Profile, rules: []const PreparedRule, pub fn findRule(self: Candidate, name: []const u8) ?PreparedRule { for (self.rules) |rule| { if (std.mem.eql(u8, rule.name, name)) return rule; } return null; } /// M1-PROJ-018: anything malformed, incomplete or inconsistent fails here. /// M1-RULE-013 also lands here: an unusable expression, regex or alias makes /// the whole candidate unable to classify, so preparation fails rather than /// producing a candidate that cannot be used. pub fn prepare( arena: std.mem.Allocator, manifest_source: Profile.Source, rules_source: Profile.Source, diags: *diag.List, ) Error!Candidate { return withRules(arena, try Profile.load(arena, manifest_source, diags), rules_source, diags); } /// The rules half of `prepare`, for a caller that needs the profile even /// when the rules fail (M1-UI-007). pub fn withRules( arena: std.mem.Allocator, profile: Profile, rules_source: Profile.Source, diags: *diag.List, ) Error!Candidate { const set = config_rules.load(arena, rules_source.path, rules_source.text, diags) catch return error.EngineFailed; return .{ .profile = profile, .rules = try prepareRules( arena, set, .{ .file = rules_source.path, .aliases = profile.aliases }, profile.amount_format, diags, ), }; } // -- rule helpers ------------------------------------------------------------ fn prepareRules( arena: std.mem.Allocator, set: config_rules.Ruleset, scope: Profile.Scope, amount_format: Decimal.Format, diags: *diag.List, ) Error![]const PreparedRule { const aliases = scope.aliases; const file = scope.file; const prepared = try arena.alloc(PreparedRule, set.rules.len); for (set.rules, 0..) |rule, index| { const key_path = try arena.print("rules.{s}.match", .{rule.name}); const context: parser.Context = .{ .file = file, .key_path = key_path }; const tree = parser.parse(arena, rule.match, context, diags) catch return error.EngineFailed; // M1-CSV-015: conditions reference declared aliases, never raw // indexes or header text. An unknown one is M1-RULE-013's "unknown // alias" and makes the whole candidate unusable. var used: std.ArrayList([]const u8) = .empty; parser.collectFields(arena, tree, &used) catch return error.EngineFailed; for (used.items) |name| { if (!containsName(aliases, name)) { diags.addConfig( .{ .file = file, .key_path = key_path }, "'{s}' is not a declared field alias", .{name}, ) catch return error.OutOfMemory; return error.EngineFailed; } } prepared[index] = .{ .name = rule.name, .program = eval.prepare(arena, tree, amount_format, context, diags) catch return error.EngineFailed, .action = rule.action, }; } return prepared; } fn containsName(haystack: []const []const u8, needle: []const u8) bool { for (haystack) |candidate| { if (std.mem.eql(u8, candidate, needle)) return true; } return false; } // -- tests ------------------------------------------------------------------- const expectError = std.testing.expectError; test "M1-SAFE-004: prepareRules reports exhaustion rather than the engine failure" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); // The key path and parse tree allocate from a working arena, so the only // allocation that runs out is the one recording the diagnostic. var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 }); var diags = diag.List.init(failing.allocator()); defer diags.deinit(); // No declared aliases, so the condition names an unknown one. const set: config_rules.Ruleset = .{ .rules = &.{.{ .action = .{ .source_account = null, .target_account = "expenses:food" }, .match = "absent == \"x\"", .name = "r", }} }; try expectError( error.OutOfMemory, prepareRules(arena.allocator(), set, .{ .file = "rules.yaml", .aliases = &.{} }, .dot, &diags), ); }