// SPDX-License-Identifier: GPL-3.0-or-later package engine import ( "context" "time" "git.labunix.xyz/krino/internal/plan" ) // DirPlan is one directory's plan: the match Result it was built from (for // the counts and warnings a front end renders), the chains plan.Build // produced from its matched files, and Elapsed (D12), which - unlike // Result.Elapsed, stopped inside Match before plan.Build ever runs - spans // both Match and Build, so a front end reporting how long planning took is // not undercounting it. It lives here, not in internal/plan, because // internal/plan must not import internal/engine (engine already imports // plan). type DirPlan struct { Dir *Dir Chains []plan.Chain Result *Result Elapsed time.Duration } // Plan matches d, then turns every matched file into a chain per spec // §7.1. Unmatched files never reach plan.Build: they have no rules, hence // no steps, and Result.Unmatched already says they were not matched. // Chains keep Match's Rel order, since Matched is already sorted that way // and Build's output lines up with its input position for position. claims // is the run-scoped claim set (A3): the caller creates one *plan.Claims per // run and passes the same instance to every directory's Plan call, so two // directories claiming one destination resolve the collision instead of // both silently landing on it. func (e *Engine) Plan(ctx context.Context, d *Dir, claims *plan.Claims) (*DirPlan, error) { started := time.Now() r, err := e.Match(ctx, d) if err != nil { return nil, err } inputs := make([]plan.Input, len(r.Matched)) for i, fm := range r.Matched { inputs[i] = plan.Input{File: fm.File, Rules: planRules(fm.Rules), NoDelete: fm.NoDelete} } chains := plan.Build(d.Root, inputs, e.Now(), plan.OS{}, claims) return &DirPlan{Dir: d, Chains: chains, Result: r, Elapsed: time.Since(started)}, nil } // planRules converts a file's matching rules into what plan.Build takes. // Explain builds the chain of one file the same way (GUI design §1.3), so // both go through this. func planRules(matched []RuleMatch) []plan.RuleMatch { rules := make([]plan.RuleMatch, len(matched)) for i, rm := range matched { rules[i] = plan.RuleMatch{ Name: rm.Rule.Name, Actions: rm.Rule.Conf.Actions, Settings: rm.Rule.Settings, Captures: rm.Captures, Reasons: rm.Reasons, } } return rules }