1use std::fmt;
8
9use serde::{Deserialize, Serialize};
10use wgpu_types::error::{ErrorType, WebGpuError};
11
12#[derive(Clone, Debug, Eq, Hash, PartialEq)]
14pub struct ErrorScope {
15 pub errors: Vec<Error>,
16 pub filter: ErrorFilter,
17}
18
19impl ErrorScope {
20 pub fn new(filter: ErrorFilter) -> Self {
21 Self {
22 filter,
23 errors: Vec::new(),
24 }
25 }
26}
27
28#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
30pub enum ErrorFilter {
31 Validation,
32 OutOfMemory,
33 Internal,
34}
35
36#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
38pub enum Error {
39 Validation(String),
40 OutOfMemory(String),
41 Internal(String),
42}
43
44impl std::error::Error for Error {
45 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46 None
47 }
48}
49
50impl fmt::Display for Error {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 f.write_str(self.message())
53 }
54}
55
56impl Error {
57 pub fn filter(&self) -> ErrorFilter {
58 match self {
59 Error::Validation(_) => ErrorFilter::Validation,
60 Error::OutOfMemory(_) => ErrorFilter::OutOfMemory,
61 Error::Internal(_) => ErrorFilter::Internal,
62 }
63 }
64
65 pub fn message(&self) -> &str {
66 match self {
67 Error::Validation(m) => m,
68 Error::OutOfMemory(m) => m,
69 Error::Internal(m) => m,
70 }
71 }
72
73 pub fn from_wgpu_error<E: WebGpuError>(error: E) -> Option<Self> {
77 match error.webgpu_error_type() {
78 ErrorType::Validation => Some(Self::Validation(error.to_string())),
79 ErrorType::OutOfMemory => Some(Self::OutOfMemory(error.to_string())),
80 ErrorType::Internal => Some(Self::Internal(error.to_string())),
81 ErrorType::DeviceLost => None,
82 }
83 }
84}
85
86#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
87pub enum PopError {
88 Lost,
89 Empty,
90}