From a954d8969062d026fb6a81132ffe8860ca0dcdb6 Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Mon, 30 Sep 2024 14:20:58 -0400 Subject: [PATCH] feat: `impl FromStr for naga::wgsl::error::Severity` --- naga/src/diagnostic_filter.rs | 10 ++++++++++ naga/src/front/wgsl/parse/mod.rs | 13 +++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/naga/src/diagnostic_filter.rs b/naga/src/diagnostic_filter.rs index c305fb65c0..68b34a468e 100644 --- a/naga/src/diagnostic_filter.rs +++ b/naga/src/diagnostic_filter.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use crate::{Handle, Span}; #[cfg(feature = "arbitrary")] @@ -32,6 +34,14 @@ impl Severity { } } +impl FromStr for Severity { + type Err = (); + + fn from_str(s: &str) -> Result { + Self::from_ident(s).ok_or(()) + } +} + // TODO: docs #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] #[cfg_attr(feature = "serialize", derive(Serialize))] diff --git a/naga/src/front/wgsl/parse/mod.rs b/naga/src/front/wgsl/parse/mod.rs index 61f9e5123e..379666a6b5 100644 --- a/naga/src/front/wgsl/parse/mod.rs +++ b/naga/src/front/wgsl/parse/mod.rs @@ -1,5 +1,5 @@ use crate::diagnostic_filter::{ - DiagnosticFilter, DiagnosticFilterMap, DiagnosticFilterNode, DiagnosticTriggeringRule, Severity, + DiagnosticFilter, DiagnosticFilterMap, DiagnosticFilterNode, DiagnosticTriggeringRule, }; use crate::front::wgsl::error::{Error, ExpectedToken}; use crate::front::wgsl::parse::directive::DirectiveKind; @@ -2580,11 +2580,12 @@ impl Parser { lexer.expect(Token::Paren('('))?; let (severity_control_name, severity_control_name_span) = lexer.next_ident_with_span()?; - let new_severity = Severity::from_ident(severity_control_name).ok_or( - Error::DiagnosticInvalidSeverity { - severity_control_name_span, - }, - )?; + let new_severity = + severity_control_name + .parse() + .map_err(|()| Error::DiagnosticInvalidSeverity { + severity_control_name_span, + })?; lexer.expect(Token::Separator(','))?;