Skip to main content

wgsl_validator/
errors.rs

1use std::fmt;
2use naga::{WithSpan, front::wgsl::ParseError, valid::ValidationError};
3
4#[derive(Debug)]
5pub enum WgslError {
6    ValidationErr {
7        src: String,
8        error: WithSpan<ValidationError>,
9        emitted: String,
10    },
11    ParserErr {
12        error: String,
13        line: usize,
14        pos: usize,
15    },
16}
17
18impl WgslError {
19    pub fn from_parse_err(err: ParseError, src: &str) -> Self {
20        let error = err.emit_to_string(src);
21        let loc = err.location(src);
22        if let Some(loc) = loc {
23            Self::ParserErr {
24                error,
25                line: loc.line_number as usize,
26                pos: loc.line_position as usize,
27            }
28        } else {
29            Self::ParserErr {
30                error,
31                line: 0,
32                pos: 0,
33            }
34        }
35    }
36}
37
38impl fmt::Display for WgslError {
39    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40        match self {
41            WgslError::ParserErr { error, .. } => write!(f, "{}", error),
42            WgslError::ValidationErr { emitted, .. } => write!(f, "{}", emitted),
43        }
44    }
45}