Skip to main content

vyre_driver/diagnostics/
legacy.rs

1use crate::error::Error;
2
3use super::{Diagnostic, OpLocation};
4
5/// Split a legacy error message on the first `". Fix: "` delimiter.
6pub(super) fn split_fix(full: String) -> (String, Option<String>) {
7    if let Some((head, tail)) = full.split_once(". Fix: ") {
8        let head = head.to_owned();
9        let tail = tail.trim_end_matches('.').to_owned();
10        (head, Some(tail))
11    } else {
12        (full, None)
13    }
14}
15
16/// Map a legacy `Error` variant to a diagnostic code + op location.
17fn classify(err: &Error) -> (&'static str, Option<OpLocation>) {
18    match err {
19        Error::InlineCycle { op_id } => ("E-INLINE-CYCLE", Some(OpLocation::op(op_id.clone()))),
20        Error::InlineUnknownOp { op_id } => {
21            ("E-INLINE-UNKNOWN-OP", Some(OpLocation::op(op_id.clone())))
22        }
23        Error::InlineNonInlinable { op_id } => (
24            "E-INLINE-NON-INLINABLE",
25            Some(OpLocation::op(op_id.clone())),
26        ),
27        Error::InlineArgCountMismatch { op_id, .. } => {
28            ("E-INLINE-ARG-COUNT", Some(OpLocation::op(op_id.clone())))
29        }
30        Error::InlineNoOutput { op_id } => {
31            ("E-INLINE-NO-OUTPUT", Some(OpLocation::op(op_id.clone())))
32        }
33        Error::InlineOutputCountMismatch { op_id, .. } => {
34            ("E-INLINE-OUTPUT-COUNT", Some(OpLocation::op(op_id.clone())))
35        }
36        Error::WireFormatValidation { .. } => ("E-WIRE-VALIDATION", None),
37        Error::Lowering { .. } => ("E-LOWERING", None),
38        Error::Interp { .. } => ("E-INTERP", None),
39        Error::Gpu { .. } => ("E-GPU", None),
40        Error::DecodeConfig { .. } => ("E-DECODE-CONFIG", None),
41        Error::Decode { .. } => ("E-DECODE", None),
42        Error::Decompress { .. } => ("E-DECOMPRESS", None),
43        Error::Dfa { .. } => ("E-DFA", None),
44        Error::Dataflow { .. } => ("E-DATAFLOW", None),
45        Error::Prefix { .. } => ("E-PREFIX", None),
46        Error::Csr { .. } => ("E-CSR", None),
47        Error::Serialization { .. } => ("E-SERIALIZATION", None),
48        Error::RuleEval { .. } => ("E-RULE-EVAL", None),
49        Error::VersionMismatch { .. } => ("E-WIRE-VERSION", None),
50        Error::UnknownDialect { .. } => ("E-WIRE-UNKNOWN-DIALECT", None),
51        Error::UnknownOp { dialect, op } => (
52            "E-WIRE-UNKNOWN-OP",
53            Some(OpLocation::op(format!("{dialect}.{op}"))),
54        ),
55        _ => ("E-UNKNOWN", None),
56    }
57}
58
59impl From<&Error> for Diagnostic {
60    fn from(err: &Error) -> Self {
61        let (code, location) = classify(err);
62        let (message, fix) = split_fix(err.to_string());
63        let mut diag = Diagnostic::error(code, message);
64        if let Some(fix) = fix {
65            diag = diag.with_fix(fix);
66        }
67        if let Some(loc) = location {
68            diag = diag.with_location(loc);
69        }
70        diag
71    }
72}
73
74impl From<Error> for Diagnostic {
75    fn from(err: Error) -> Self {
76        Diagnostic::from(&err)
77    }
78}