1use std::path::Path;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
6#[non_exhaustive]
7pub enum CreateModuleError {
8 #[error("bind groups are non-consecutive or do not start from 0")]
12 NonConsecutiveBindGroups,
13
14 #[error("duplicate binding found with index `{binding}`")]
16 DuplicateBinding { binding: u32 },
17
18 #[error("failed to parse: {error}")]
20 ParseError {
21 error: naga::front::wgsl::ParseError,
22 },
23
24 #[error("failed to validate: {error}")]
26 ValidationError {
27 error: naga::WithSpan<naga::valid::ValidationError>,
28 },
29}
30
31impl CreateModuleError {
32 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 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 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 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}