Skip to main content

wgsl_to_wgpu/
error.rs

1use std::path::Path;
2use thiserror::Error;
3
4/// Errors while generating Rust source for a WGSL shader module.
5#[derive(Debug, Error)]
6#[non_exhaustive]
7pub enum CreateModuleError {
8    /// Bind group sets must be consecutive and start from 0.
9    /// See `bind_group_layouts` for
10    /// [PipelineLayoutDescriptor](https://docs.rs/wgpu/latest/wgpu/struct.PipelineLayoutDescriptor.html#).
11    #[error("bind groups are non-consecutive or do not start from 0")]
12    NonConsecutiveBindGroups,
13
14    /// Each binding resource must be associated with exactly one binding index.
15    #[error("duplicate binding found with index `{binding}`")]
16    DuplicateBinding { binding: u32 },
17
18    /// The shader source could not be parsed.
19    #[error("failed to parse: {error}")]
20    ParseError {
21        error: naga::front::wgsl::ParseError,
22    },
23
24    /// The shader source could not be validated.
25    #[error("failed to validate: {error}")]
26    ValidationError {
27        error: naga::WithSpan<naga::valid::ValidationError>,
28    },
29}
30
31impl CreateModuleError {
32    /// Writes a diagnostic error to stderr.
33    pub fn emit_to_stderr(&self, wgsl_source: &str) {
34        match self {
35            CreateModuleError::ParseError { error } => error.emit_to_stderr(wgsl_source),
36            CreateModuleError::ValidationError { error } => error.emit_to_stderr(wgsl_source),
37            other => {
38                eprintln!("{other}")
39            }
40        }
41    }
42
43    /// Writes a diagnostic error to stderr, including a source path.
44    pub fn emit_to_stderr_with_path(&self, wgsl_source: &str, path: impl AsRef<Path>) {
45        let path = path.as_ref();
46        match self {
47            CreateModuleError::ParseError { error } => {
48                error.emit_to_stderr_with_path(wgsl_source, path)
49            }
50            CreateModuleError::ValidationError { error } => {
51                let path = path.to_string_lossy();
52                error.emit_to_stderr_with_path(wgsl_source, &path)
53            }
54            other => {
55                eprintln!("{}: {}", path.to_string_lossy(), other)
56            }
57        }
58    }
59
60    /// Creates a diagnostic string from the error.
61    pub fn emit_to_string(&self, wgsl_source: &str) -> String {
62        match self {
63            CreateModuleError::ParseError { error } => error.emit_to_string(wgsl_source),
64            CreateModuleError::ValidationError { error } => error.emit_to_string(wgsl_source),
65            other => {
66                format!("{other}")
67            }
68        }
69    }
70
71    /// Creates a diagnostic string from the error, including a source path.
72    pub fn emit_to_string_with_path(&self, wgsl_source: &str, path: impl AsRef<Path>) -> String {
73        let path = path.as_ref();
74        match self {
75            CreateModuleError::ParseError { error } => {
76                error.emit_to_string_with_path(wgsl_source, path)
77            }
78            CreateModuleError::ValidationError { error } => {
79                let path = path.to_string_lossy();
80                error.emit_to_string_with_path(wgsl_source, &path)
81            }
82            other => {
83                format!("{}: {}", path.to_string_lossy(), other)
84            }
85        }
86    }
87}