use crate::evidence_validation::{EvidenceProfile, validate_evidence};
use crate::structure::validate_structure;
use crate::{
PlanError, PlanFingerprint, PlanStats, RefactorPlan, RefactorPlanLimits, fingerprint_validated,
};
#[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
}
}
#[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()
}
}
#[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()
}
}
pub type ValidatedRefactorPlan<'plan> = ValidatedConsumerPlan<'plan>;
pub fn validate_consumer_plan(
plan: &RefactorPlan,
limits: RefactorPlanLimits,
) -> Result<ValidatedConsumerPlan<'_>, PlanError> {
validate_profile(plan, limits, EvidenceProfile::Consumer)
}
pub fn validate_planner_plan(
plan: &RefactorPlan,
limits: RefactorPlanLimits,
) -> Result<ValidatedPlannerPlan<'_>, PlanError> {
validate_profile(plan, limits, EvidenceProfile::Planner)
.map(|consumer| ValidatedPlannerPlan { consumer })
}
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;