Skip to main content

zyn_core/mark/
level.rs

1//! Diagnostic severity levels.
2
3/// Diagnostic severity level, ordered from least to most severe.
4#[non_exhaustive]
5#[repr(u8)]
6#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
7pub enum Level {
8    /// No diagnostic (default). Not emitted.
9    #[default]
10    None,
11    /// Informational note.
12    Note,
13    /// Helpful suggestion.
14    Help,
15    /// Compiler warning.
16    Warning,
17    /// Hard compile error.
18    Error,
19}
20
21impl Level {
22    /// Returns the numeric value of this level.
23    pub fn to_u8(&self) -> u8 {
24        *self as u8
25    }
26
27    /// Returns a lowercase string representation (`"error"`, `"warning"`, etc.).
28    pub fn as_str(&self) -> &'static str {
29        match self {
30            Self::None => "none",
31            Self::Error => "error",
32            Self::Warning => "warning",
33            Self::Note => "note",
34            Self::Help => "help",
35        }
36    }
37}
38
39impl std::fmt::Display for Level {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        write!(f, "{}", self.as_str())
42    }
43}
44
45#[cfg(feature = "diagnostics")]
46impl From<proc_macro2_diagnostics::Level> for Level {
47    fn from(value: proc_macro2_diagnostics::Level) -> Self {
48        match value {
49            proc_macro2_diagnostics::Level::Error => Self::Error,
50            proc_macro2_diagnostics::Level::Warning => Self::Warning,
51            proc_macro2_diagnostics::Level::Note => Self::Note,
52            proc_macro2_diagnostics::Level::Help => Self::Help,
53            _ => Self::None,
54        }
55    }
56}
57
58#[cfg(feature = "diagnostics")]
59impl From<Level> for proc_macro2_diagnostics::Level {
60    fn from(value: Level) -> Self {
61        match value {
62            Level::Error => Self::Error,
63            Level::Warning => Self::Warning,
64            Level::Note => Self::Note,
65            Level::Help => Self::Help,
66            Level::None => panic!("unsupported diagnostic level"),
67        }
68    }
69}