weavatrix-refactor-plan 0.1.0

Evidence metadata, validation profiles, and canonical fingerprints for Weavatrix refactor plans
Documentation
use crate::evidence_validation::{EvidenceProfile, validate_evidence};
use crate::structure::validate_structure;
use crate::{
    PlanError, PlanFingerprint, PlanStats, RefactorPlan, RefactorPlanLimits, fingerprint_validated,
};

/// A consumer-compatible plan proven structurally safe and internally consistent.
#[derive(Clone, Debug)]
pub struct ValidatedConsumerPlan<'plan> {
    plan: &'plan RefactorPlan,
    stats: PlanStats,
    fingerprint: PlanFingerprint,
}

impl<'plan> ValidatedConsumerPlan<'plan> {
    #[must_use]
    pub const fn plan(&self) -> &'plan RefactorPlan {
        self.plan
    }

    #[must_use]
    pub const fn evidence(&self) -> &PlanEvidence {
        &self.plan.evidence
    }

    #[must_use]
    pub const fn fingerprint(&self) -> PlanFingerprint {
        self.fingerprint
    }

    #[must_use]
    pub const fn stats(&self) -> PlanStats {
        self.stats
    }

    #[must_use]
    pub const fn total_edits(&self) -> usize {
        self.stats.edits
    }

    #[must_use]
    pub const fn total_paths(&self) -> usize {
        self.stats.paths
    }
}

/// Strict producer output with explicit, truthful completeness evidence.
#[derive(Clone, Debug)]
pub struct ValidatedPlannerPlan<'plan> {
    consumer: ValidatedConsumerPlan<'plan>,
}

impl<'plan> ValidatedPlannerPlan<'plan> {
    #[must_use]
    pub const fn plan(&self) -> &'plan RefactorPlan {
        self.consumer.plan()
    }

    #[must_use]
    pub const fn evidence(&self) -> &PlanEvidence {
        self.consumer.evidence()
    }

    #[must_use]
    pub const fn fingerprint(&self) -> PlanFingerprint {
        self.consumer.fingerprint()
    }

    #[must_use]
    pub const fn stats(&self) -> PlanStats {
        self.consumer.stats()
    }
}

/// Exact-operation executor input, independent of semantic completeness.
#[derive(Clone, Debug)]
pub struct ValidatedExecutorPlan<'plan> {
    consumer: ValidatedConsumerPlan<'plan>,
}

impl<'plan> ValidatedExecutorPlan<'plan> {
    #[must_use]
    pub const fn plan(&self) -> &'plan RefactorPlan {
        self.consumer.plan()
    }

    #[must_use]
    pub const fn fingerprint(&self) -> PlanFingerprint {
        self.consumer.fingerprint()
    }

    #[must_use]
    pub const fn stats(&self) -> PlanStats {
        self.consumer.stats()
    }
}

/// Compatibility name for the general validated refactor-plan wrapper.
pub type ValidatedRefactorPlan<'plan> = ValidatedConsumerPlan<'plan>;

/// Validates exact operations and any recognized evidence that is present.
pub fn validate_consumer_plan(
    plan: &RefactorPlan,
    limits: RefactorPlanLimits,
) -> Result<ValidatedConsumerPlan<'_>, PlanError> {
    validate_profile(plan, limits, EvidenceProfile::Consumer)
}

/// Validates a strict planner-produced plan with explicit evidence.
pub fn validate_planner_plan(
    plan: &RefactorPlan,
    limits: RefactorPlanLimits,
) -> Result<ValidatedPlannerPlan<'_>, PlanError> {
    validate_profile(plan, limits, EvidenceProfile::Planner)
        .map(|consumer| ValidatedPlannerPlan { consumer })
}

/// Validates filesystem-safe exact operations without requiring COMPLETE semantics.
pub fn validate_executor_plan(
    plan: &RefactorPlan,
    limits: RefactorPlanLimits,
) -> Result<ValidatedExecutorPlan<'_>, PlanError> {
    validate_profile(plan, limits, EvidenceProfile::Consumer)
        .map(|consumer| ValidatedExecutorPlan { consumer })
}

pub(crate) fn validate_for_fingerprint(
    plan: &RefactorPlan,
    limits: RefactorPlanLimits,
) -> Result<PlanStats, PlanError> {
    let stats = validate_structure(plan, limits)?;
    validate_evidence(plan, limits, EvidenceProfile::Consumer)?;
    Ok(stats)
}

fn validate_profile(
    plan: &RefactorPlan,
    limits: RefactorPlanLimits,
    profile: EvidenceProfile,
) -> Result<ValidatedConsumerPlan<'_>, PlanError> {
    let stats = validate_structure(plan, limits)?;
    validate_evidence(plan, limits, profile)?;
    let fingerprint = fingerprint_validated(plan)?;
    Ok(ValidatedConsumerPlan {
        plan,
        stats,
        fingerprint,
    })
}

use crate::PlanEvidence;