feat: add From<Box<dyn Diagnostic>> for MietteDiagnostic

Fixes #285
This commit is contained in:
winlogon 2025-10-09 16:09:23 +02:00
parent df7bcfa17d
commit db010f4c80
No known key found for this signature in database
GPG Key ID: 47B0104D6EFD3C5E
1 changed files with 21 additions and 0 deletions

View File

@ -258,6 +258,27 @@ impl MietteDiagnostic {
}
}
impl From<Box<dyn Diagnostic>> for MietteDiagnostic {
fn from(diag: Box<dyn Diagnostic>) -> Self {
let message = diag.to_string();
let code = diag.code().map(|c| c.to_string());
let severity = diag.severity();
let help = diag.help().map(|h| h.to_string());
let url = diag.url().map(|u| u.to_string());
let labels = diag.labels().map(|l| l.collect::<Vec<_>>());
MietteDiagnostic {
message,
code,
severity,
help,
url,
labels,
}
}
}
#[cfg(feature = "serde")]
#[test]
fn test_serialize_miette_diagnostic() {