Skip to main content

runic_diagnostics/
descriptor.rs

1use core::fmt;
2
3/// The semantic severity of a diagnostic occurrence.
4#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
5#[non_exhaustive]
6pub enum Severity {
7    Error,
8    Warning,
9    Info,
10    Hint,
11}
12
13/// Standardized semantic tags
14#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
15#[non_exhaustive]
16pub enum DiagnosticTag {
17    Unnecessary,
18    Deprecated,
19}
20
21/// A static, namespaced diagnostic code.
22#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
23pub struct DiagnosticCode {
24    pub namespace: &'static str,
25    pub code: &'static str,
26}
27
28impl DiagnosticCode {
29    pub const fn as_ref(&self) -> DiagnosticCodeRef<'_> {
30        DiagnosticCodeRef {
31            namespace: self.namespace,
32            code: self.code,
33        }
34    }
35}
36
37impl fmt::Display for DiagnosticCode {
38    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
39        self.as_ref().fmt(formatter)
40    }
41}
42
43/// A borrowed diagnostic code used by runtime-only diagnostics.
44#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
45pub struct DiagnosticCodeRef<'a> {
46    pub namespace: &'a str,
47    pub code: &'a str,
48}
49
50impl fmt::Display for DiagnosticCodeRef<'_> {
51    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(formatter, "{}/{}", self.namespace, self.code)
53    }
54}
55
56/// Availability of a descriptor's long-form explanation.
57#[derive(Clone, Copy, Debug, Eq, PartialEq)]
58pub enum Explanation {
59    NotProvided,
60    NotEmbedded,
61    Embedded(&'static str),
62}
63
64/// The definition site of a static diagnostic descriptor.
65#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
66pub struct DescriptorOrigin {
67    pub module_path: &'static str,
68    pub file: &'static str,
69    pub line: u32,
70}
71
72/// Static identity and documentation for a diagnostic kind.
73#[derive(Debug, Eq, PartialEq)]
74pub struct DiagnosticDescriptor {
75    pub code: DiagnosticCode,
76    pub summary: &'static str,
77    pub default_severity: Severity,
78    pub explanation: Explanation,
79    pub documentation_url: Option<&'static str>,
80    pub tags: &'static [DiagnosticTag],
81    pub origin: DescriptorOrigin,
82}
83
84/// Lightweight semantic metadata passed to a failure policy.
85#[derive(Clone, Copy, Debug)]
86pub struct DiagnosticMetadata<'a> {
87    pub descriptor: Option<&'static DiagnosticDescriptor>,
88    pub code: Option<DiagnosticCodeRef<'a>>,
89    pub severity: Severity,
90    pub tags: &'a [DiagnosticTag],
91}
92
93#[cfg(test)]
94mod tests {
95    use alloc::string::ToString;
96
97    use super::*;
98
99    #[test]
100    fn code_display_and_explanation_states_are_explicit() {
101        let code = DiagnosticCode {
102            namespace: "runic::parser",
103            code: "E0001",
104        };
105        assert_eq!(code.to_string(), "runic::parser/E0001");
106        assert_eq!(code.as_ref().to_string(), "runic::parser/E0001");
107        assert_ne!(Explanation::NotProvided, Explanation::NotEmbedded);
108        assert_ne!(Explanation::NotEmbedded, Explanation::Embedded("sentinel"));
109    }
110
111    #[test]
112    fn all_four_severities_are_copyable_values() {
113        let values = [Severity::Error, Severity::Warning, Severity::Info, Severity::Hint];
114        assert_eq!(values, values);
115    }
116}