Skip to main content

weavatrix_refactor_plan/
validation.rs

1use crate::evidence_validation::{EvidenceProfile, validate_evidence};
2use crate::structure::validate_structure;
3use crate::{
4    PlanError, PlanFingerprint, PlanStats, RefactorPlan, RefactorPlanLimits, fingerprint_validated,
5};
6
7/// A consumer-compatible plan proven structurally safe and internally consistent.
8#[derive(Clone, Debug)]
9pub struct ValidatedConsumerPlan<'plan> {
10    plan: &'plan RefactorPlan,
11    stats: PlanStats,
12    fingerprint: PlanFingerprint,
13}
14
15impl<'plan> ValidatedConsumerPlan<'plan> {
16    #[must_use]
17    pub const fn plan(&self) -> &'plan RefactorPlan {
18        self.plan
19    }
20
21    #[must_use]
22    pub const fn evidence(&self) -> &PlanEvidence {
23        &self.plan.evidence
24    }
25
26    #[must_use]
27    pub const fn fingerprint(&self) -> PlanFingerprint {
28        self.fingerprint
29    }
30
31    #[must_use]
32    pub const fn stats(&self) -> PlanStats {
33        self.stats
34    }
35
36    #[must_use]
37    pub const fn total_edits(&self) -> usize {
38        self.stats.edits
39    }
40
41    #[must_use]
42    pub const fn total_paths(&self) -> usize {
43        self.stats.paths
44    }
45}
46
47/// Strict producer output with explicit, truthful completeness evidence.
48#[derive(Clone, Debug)]
49pub struct ValidatedPlannerPlan<'plan> {
50    consumer: ValidatedConsumerPlan<'plan>,
51}
52
53impl<'plan> ValidatedPlannerPlan<'plan> {
54    #[must_use]
55    pub const fn plan(&self) -> &'plan RefactorPlan {
56        self.consumer.plan()
57    }
58
59    #[must_use]
60    pub const fn evidence(&self) -> &PlanEvidence {
61        self.consumer.evidence()
62    }
63
64    #[must_use]
65    pub const fn fingerprint(&self) -> PlanFingerprint {
66        self.consumer.fingerprint()
67    }
68
69    #[must_use]
70    pub const fn stats(&self) -> PlanStats {
71        self.consumer.stats()
72    }
73}
74
75/// Exact-operation executor input, independent of semantic completeness.
76#[derive(Clone, Debug)]
77pub struct ValidatedExecutorPlan<'plan> {
78    consumer: ValidatedConsumerPlan<'plan>,
79}
80
81impl<'plan> ValidatedExecutorPlan<'plan> {
82    #[must_use]
83    pub const fn plan(&self) -> &'plan RefactorPlan {
84        self.consumer.plan()
85    }
86
87    #[must_use]
88    pub const fn fingerprint(&self) -> PlanFingerprint {
89        self.consumer.fingerprint()
90    }
91
92    #[must_use]
93    pub const fn stats(&self) -> PlanStats {
94        self.consumer.stats()
95    }
96}
97
98/// Compatibility name for the general validated refactor-plan wrapper.
99pub type ValidatedRefactorPlan<'plan> = ValidatedConsumerPlan<'plan>;
100
101/// Validates exact operations and any recognized evidence that is present.
102pub fn validate_consumer_plan(
103    plan: &RefactorPlan,
104    limits: RefactorPlanLimits,
105) -> Result<ValidatedConsumerPlan<'_>, PlanError> {
106    validate_profile(plan, limits, EvidenceProfile::Consumer)
107}
108
109/// Validates a strict planner-produced plan with explicit evidence.
110pub fn validate_planner_plan(
111    plan: &RefactorPlan,
112    limits: RefactorPlanLimits,
113) -> Result<ValidatedPlannerPlan<'_>, PlanError> {
114    validate_profile(plan, limits, EvidenceProfile::Planner)
115        .map(|consumer| ValidatedPlannerPlan { consumer })
116}
117
118/// Validates filesystem-safe exact operations without requiring COMPLETE semantics.
119pub fn validate_executor_plan(
120    plan: &RefactorPlan,
121    limits: RefactorPlanLimits,
122) -> Result<ValidatedExecutorPlan<'_>, PlanError> {
123    validate_profile(plan, limits, EvidenceProfile::Consumer)
124        .map(|consumer| ValidatedExecutorPlan { consumer })
125}
126
127pub(crate) fn validate_for_fingerprint(
128    plan: &RefactorPlan,
129    limits: RefactorPlanLimits,
130) -> Result<PlanStats, PlanError> {
131    let stats = validate_structure(plan, limits)?;
132    validate_evidence(plan, limits, EvidenceProfile::Consumer)?;
133    Ok(stats)
134}
135
136fn validate_profile(
137    plan: &RefactorPlan,
138    limits: RefactorPlanLimits,
139    profile: EvidenceProfile,
140) -> Result<ValidatedConsumerPlan<'_>, PlanError> {
141    let stats = validate_structure(plan, limits)?;
142    validate_evidence(plan, limits, profile)?;
143    let fingerprint = fingerprint_validated(plan)?;
144    Ok(ValidatedConsumerPlan {
145        plan,
146        stats,
147        fingerprint,
148    })
149}
150
151use crate::PlanEvidence;