use std::fmt;
use serde::{Deserialize, Serialize};
use wgpu_types::error::{ErrorType, WebGpuError};
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct ErrorScope {
pub errors: Vec<Error>,
pub filter: ErrorFilter,
}
impl ErrorScope {
pub fn new(filter: ErrorFilter) -> Self {
Self {
filter,
errors: Vec::new(),
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum ErrorFilter {
Validation,
OutOfMemory,
Internal,
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum Error {
Validation(String),
OutOfMemory(String),
Internal(String),
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.message())
}
}
impl Error {
pub fn filter(&self) -> ErrorFilter {
match self {
Error::Validation(_) => ErrorFilter::Validation,
Error::OutOfMemory(_) => ErrorFilter::OutOfMemory,
Error::Internal(_) => ErrorFilter::Internal,
}
}
pub fn message(&self) -> &str {
match self {
Error::Validation(m) => m,
Error::OutOfMemory(m) => m,
Error::Internal(m) => m,
}
}
pub fn from_wgpu_error<E: WebGpuError>(error: E) -> Option<Self> {
match error.webgpu_error_type() {
ErrorType::Validation => Some(Self::Validation(error.to_string())),
ErrorType::OutOfMemory => Some(Self::OutOfMemory(error.to_string())),
ErrorType::Internal => Some(Self::Internal(error.to_string())),
ErrorType::DeviceLost => None,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum PopError {
Lost,
Empty,
}