pub use crate::codes;
pub use crate::stored::legacy09::legacy_diag::{
DiagnosticCode, DiagnosticSeverity, DiagnosticStage, StructuredDiagnostic,
};
pub(crate) fn to_module_diagnostic(
legacy: &StructuredDiagnostic,
target: Option<String>,
) -> powerio_core::Diagnostic {
use powerio_core::{Diagnostic, DiagnosticCode};
let code = DiagnosticCode::new(legacy.code.as_str()).unwrap_or_else(|_| {
DiagnosticCode::new("PARTNER.LEGACY.UNCODED").expect("static code is valid")
});
let severity = match legacy.severity {
DiagnosticSeverity::Error => powerio_core::DiagnosticSeverity::Error,
DiagnosticSeverity::Warning => powerio_core::DiagnosticSeverity::Warning,
_ => powerio_core::DiagnosticSeverity::Note,
};
let mut diagnostic = Diagnostic::new(code, severity, legacy.message.clone());
if let Some(target) = target
&& let Ok(with) = diagnostic.clone().with_target(target)
{
diagnostic = with;
}
diagnostic
}
pub(crate) fn translate_legacy_target(element_path: Option<&str>, kind: &str) -> Option<String> {
let path = element_path?;
let prefix = format!("/model/{kind}");
let rest = path.strip_prefix(&prefix)?;
if rest.is_empty() {
return None;
}
rest.starts_with('/').then(|| rest.to_owned())
}
#[cfg(test)]
mod tests {
#[test]
fn every_hardcoded_diagnostic_code_literal_is_well_formed() {
for code in [
"PARTNER.LEGACY.UNCODED", "READ.TEST.NOTE", "READ.CASE.FILLER", ] {
assert!(
powerio_core::code_is_well_formed(code),
"{code} would panic its DiagnosticCode::new(...).expect(...) site"
);
}
}
}