use std::path::Path;
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CreateModuleError {
#[error("bind groups are non-consecutive or do not start from 0")]
NonConsecutiveBindGroups,
#[error("duplicate binding found with index `{binding}`")]
DuplicateBinding { binding: u32 },
#[error("failed to parse: {error}")]
ParseError {
error: naga::front::wgsl::ParseError,
},
#[error("failed to validate: {error}")]
ValidationError {
error: naga::WithSpan<naga::valid::ValidationError>,
},
}
impl CreateModuleError {
pub fn emit_to_stderr(&self, wgsl_source: &str) {
match self {
CreateModuleError::ParseError { error } => error.emit_to_stderr(wgsl_source),
CreateModuleError::ValidationError { error } => error.emit_to_stderr(wgsl_source),
other => {
eprintln!("{other}")
}
}
}
pub fn emit_to_stderr_with_path(&self, wgsl_source: &str, path: impl AsRef<Path>) {
let path = path.as_ref();
match self {
CreateModuleError::ParseError { error } => {
error.emit_to_stderr_with_path(wgsl_source, path)
}
CreateModuleError::ValidationError { error } => {
let path = path.to_string_lossy();
error.emit_to_stderr_with_path(wgsl_source, &path)
}
other => {
eprintln!("{}: {}", path.to_string_lossy(), other)
}
}
}
pub fn emit_to_string(&self, wgsl_source: &str) -> String {
match self {
CreateModuleError::ParseError { error } => error.emit_to_string(wgsl_source),
CreateModuleError::ValidationError { error } => error.emit_to_string(wgsl_source),
other => {
format!("{other}")
}
}
}
pub fn emit_to_string_with_path(&self, wgsl_source: &str, path: &str) -> String {
match self {
CreateModuleError::ParseError { error } => {
error.emit_to_string_with_path(wgsl_source, path)
}
CreateModuleError::ValidationError { error } => {
error.emit_to_string_with_path(wgsl_source, path)
}
other => {
format!("{path}: {other}")
}
}
}
}