use super::{base_caps, concepts_of, finding_from_concept};
use crate::domain::concepts::ConceptKind;
use crate::domain::model::LanguageModel;
use crate::domain::rule::{Rule, RuleMeta};
use crate::domain::types::{Confidence, Finding, Severity};
use std::sync::OnceLock;
#[derive(Default)]
pub struct ErrorContextLossRule;
impl Rule for ErrorContextLossRule {
fn meta(&self) -> &RuleMeta {
static META: OnceLock<RuleMeta> = OnceLock::new();
META.get_or_init(|| RuleMeta {
id: "error-context-loss".into(),
name: "Error context loss".into(),
description: "Original error context is discarded when mapping/wrapping.".into(),
default_severity: Severity::Medium,
required_capabilities: base_caps(),
autofix_safe: false,
})
}
fn evaluate(&self, model: &LanguageModel) -> Vec<Finding> {
concepts_of(model, ConceptKind::ContextLoss)
.chain(
concepts_of(model, ConceptKind::Propagation)
.filter(|(_, c)| c.drops_context),
)
.map(|(lang, c)| {
finding_from_concept(
&self.meta().id,
lang,
c,
Severity::Medium,
Confidence::Medium,
"Error context is dropped during mapping or conversion.",
"Debugging loses the root cause; only a generic error remains.",
"Chain sources (anyhow/thiserror source, cause(), wrap with context).",
)
})
.collect()
}
}