use crate::error::Span;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecoveryStrategy {
InsertText {
position: usize,
text: String,
},
RemoveText {
span: Span,
},
ReplaceText {
span: Span,
replacement: String,
},
AddClosing {
position: usize,
delimiter: char,
},
FixQuoting {
span: Span,
quote_char: char,
},
RepairNumber {
span: Span,
corrected_value: String,
},
AddComma {
position: usize,
},
RemoveTrailingComma {
position: usize,
},
QuoteKey {
span: Span,
},
FixComment {
span: Span,
},
ManualIntervention {
suggestions: Vec<String>,
},
}
impl RecoveryStrategy {
pub fn description(&self) -> String {
match self {
RecoveryStrategy::InsertText { text, .. } => {
format!("Insert \"{text}\"")
}
RecoveryStrategy::RemoveText { span } => {
format!("Remove {} characters", span.len())
}
RecoveryStrategy::ReplaceText { replacement, .. } => {
format!("Replace with \"{replacement}\"")
}
RecoveryStrategy::AddClosing { delimiter, .. } => {
format!("Add closing '{delimiter}'")
}
RecoveryStrategy::FixQuoting { quote_char, .. } => {
format!("Fix quoting with '{quote_char}'")
}
RecoveryStrategy::RepairNumber {
corrected_value, ..
} => {
format!("Repair number to \"{corrected_value}\"")
}
RecoveryStrategy::AddComma { .. } => "Add missing comma".to_string(),
RecoveryStrategy::RemoveTrailingComma { .. } => "Remove trailing comma".to_string(),
RecoveryStrategy::QuoteKey { .. } => "Add quotes around key".to_string(),
RecoveryStrategy::FixComment { .. } => "Fix comment syntax".to_string(),
RecoveryStrategy::ManualIntervention { suggestions } => {
format!("Manual intervention required: {}", suggestions.join("; "))
}
}
}
pub fn confidence(&self) -> f32 {
match self {
RecoveryStrategy::InsertText { .. } => 0.8,
RecoveryStrategy::RemoveText { .. } => 0.7,
RecoveryStrategy::ReplaceText { .. } => 0.8,
RecoveryStrategy::AddClosing { .. } => 0.9,
RecoveryStrategy::FixQuoting { .. } => 0.8,
RecoveryStrategy::RepairNumber { .. } => 0.7,
RecoveryStrategy::AddComma { .. } => 0.8,
RecoveryStrategy::RemoveTrailingComma { .. } => 0.9,
RecoveryStrategy::QuoteKey { .. } => 0.8,
RecoveryStrategy::FixComment { .. } => 0.6,
RecoveryStrategy::ManualIntervention { .. } => 0.3,
}
}
}