naga/common/wgsl/
diagnostics.rs

1//! WGSL diagnostic filters and severities.
2
3use core::fmt::{self, Display, Formatter};
4
5use crate::diagnostic_filter::{
6    FilterableTriggeringRule, Severity, StandardFilterableTriggeringRule,
7};
8
9impl Severity {
10    const ERROR: &'static str = "error";
11    const WARNING: &'static str = "warning";
12    const INFO: &'static str = "info";
13    const OFF: &'static str = "off";
14
15    /// Convert from a sentinel word in WGSL into its associated [`Severity`], if possible.
16    pub fn from_wgsl_ident(s: &str) -> Option<Self> {
17        Some(match s {
18            Self::ERROR => Self::Error,
19            Self::WARNING => Self::Warning,
20            Self::INFO => Self::Info,
21            Self::OFF => Self::Off,
22            _ => return None,
23        })
24    }
25}
26
27#[expect(
28    missing_debug_implementations,
29    reason = "use this type with `Display`, not `Debug`"
30)]
31pub struct DisplayFilterableTriggeringRule<'a>(&'a FilterableTriggeringRule);
32
33impl Display for DisplayFilterableTriggeringRule<'_> {
34    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
35        let &Self(inner) = self;
36        match *inner {
37            FilterableTriggeringRule::Standard(rule) => write!(f, "{}", rule.to_wgsl_ident()),
38            FilterableTriggeringRule::Unknown(ref rule) => write!(f, "{rule}"),
39            FilterableTriggeringRule::User(ref rules) => {
40                let &[ref seg1, ref seg2] = rules.as_ref();
41                write!(f, "{seg1}.{seg2}")
42            }
43        }
44    }
45}
46
47impl FilterableTriggeringRule {
48    /// [`Display`] this rule's identifiers in WGSL.
49    pub const fn display_wgsl_ident(&self) -> impl Display + '_ {
50        DisplayFilterableTriggeringRule(self)
51    }
52}
53
54impl StandardFilterableTriggeringRule {
55    const DERIVATIVE_UNIFORMITY: &'static str = "derivative_uniformity";
56
57    /// Convert from a sentinel word in WGSL into its associated
58    /// [`StandardFilterableTriggeringRule`], if possible.
59    pub fn from_wgsl_ident(s: &str) -> Option<Self> {
60        Some(match s {
61            Self::DERIVATIVE_UNIFORMITY => Self::DerivativeUniformity,
62            _ => return None,
63        })
64    }
65
66    /// Maps this [`StandardFilterableTriggeringRule`] into the sentinel word associated with it in
67    /// WGSL.
68    pub const fn to_wgsl_ident(self) -> &'static str {
69        match self {
70            Self::DerivativeUniformity => Self::DERIVATIVE_UNIFORMITY,
71        }
72    }
73}