naga/common/
wgsl.rs

1//! Code shared between the WGSL front and back ends.
2
3use std::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
27struct DisplayFilterableTriggeringRule<'a>(&'a FilterableTriggeringRule);
28
29impl Display for DisplayFilterableTriggeringRule<'_> {
30    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
31        let &Self(inner) = self;
32        match *inner {
33            FilterableTriggeringRule::Standard(rule) => write!(f, "{}", rule.to_wgsl_ident()),
34            FilterableTriggeringRule::Unknown(ref rule) => write!(f, "{rule}"),
35            FilterableTriggeringRule::User(ref rules) => {
36                let &[ref seg1, ref seg2] = rules.as_ref();
37                write!(f, "{seg1}.{seg2}")
38            }
39        }
40    }
41}
42
43impl FilterableTriggeringRule {
44    /// [`Display`] this rule's identifiers in WGSL.
45    pub const fn display_wgsl_ident(&self) -> impl Display + '_ {
46        DisplayFilterableTriggeringRule(self)
47    }
48}
49
50impl StandardFilterableTriggeringRule {
51    const DERIVATIVE_UNIFORMITY: &'static str = "derivative_uniformity";
52
53    /// Convert from a sentinel word in WGSL into its associated
54    /// [`StandardFilterableTriggeringRule`], if possible.
55    pub fn from_wgsl_ident(s: &str) -> Option<Self> {
56        Some(match s {
57            Self::DERIVATIVE_UNIFORMITY => Self::DerivativeUniformity,
58            _ => return None,
59        })
60    }
61
62    /// Maps this [`StandardFilterableTriggeringRule`] into the sentinel word associated with it in
63    /// WGSL.
64    pub const fn to_wgsl_ident(self) -> &'static str {
65        match self {
66            Self::DerivativeUniformity => Self::DERIVATIVE_UNIFORMITY,
67        }
68    }
69}