1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use core::fmt;
use std::error::Error;

use crate::{gfx_select, global::Global};

pub struct ErrorFormatter<'a> {
    writer: &'a mut dyn fmt::Write,
    global: &'a Global,
}

impl<'a> ErrorFormatter<'a> {
    pub fn error(&mut self, err: &dyn Error) {
        writeln!(self.writer, "    {err}").expect("Error formatting error");
    }

    pub fn note(&mut self, note: &dyn fmt::Display) {
        writeln!(self.writer, "      note: {note}").expect("Error formatting error");
    }

    pub fn label(&mut self, label_key: &str, label_value: &String) {
        if !label_key.is_empty() && !label_value.is_empty() {
            self.note(&format!("{label_key} = `{label_value}`"));
        }
    }

    pub fn bind_group_label(&mut self, id: &crate::id::BindGroupId) {
        let label: String = gfx_select!(id => self.global.bind_group_label(*id));
        self.label("bind group", &label);
    }

    pub fn bind_group_layout_label(&mut self, id: &crate::id::BindGroupLayoutId) {
        let label: String = gfx_select!(id => self.global.bind_group_layout_label(*id));
        self.label("bind group layout", &label);
    }

    pub fn render_pipeline_label(&mut self, id: &crate::id::RenderPipelineId) {
        let label: String = gfx_select!(id => self.global.render_pipeline_label(*id));
        self.label("render pipeline", &label);
    }

    pub fn compute_pipeline_label(&mut self, id: &crate::id::ComputePipelineId) {
        let label: String = gfx_select!(id => self.global.compute_pipeline_label(*id));
        self.label("compute pipeline", &label);
    }

    pub fn buffer_label_with_key(&mut self, id: &crate::id::BufferId, key: &str) {
        let label: String = gfx_select!(id => self.global.buffer_label(*id));
        self.label(key, &label);
    }

    pub fn buffer_label(&mut self, id: &crate::id::BufferId) {
        self.buffer_label_with_key(id, "buffer");
    }

    pub fn texture_label_with_key(&mut self, id: &crate::id::TextureId, key: &str) {
        let label: String = gfx_select!(id => self.global.texture_label(*id));
        self.label(key, &label);
    }

    pub fn texture_label(&mut self, id: &crate::id::TextureId) {
        self.texture_label_with_key(id, "texture");
    }

    pub fn texture_view_label_with_key(&mut self, id: &crate::id::TextureViewId, key: &str) {
        let label: String = gfx_select!(id => self.global.texture_view_label(*id));
        self.label(key, &label);
    }

    pub fn texture_view_label(&mut self, id: &crate::id::TextureViewId) {
        self.texture_view_label_with_key(id, "texture view");
    }

    pub fn sampler_label(&mut self, id: &crate::id::SamplerId) {
        let label: String = gfx_select!(id => self.global.sampler_label(*id));
        self.label("sampler", &label);
    }

    pub fn command_buffer_label(&mut self, id: &crate::id::CommandBufferId) {
        let label: String = gfx_select!(id => self.global.command_buffer_label(*id));
        self.label("command buffer", &label);
    }

    pub fn query_set_label(&mut self, id: &crate::id::QuerySetId) {
        let label: String = gfx_select!(id => self.global.query_set_label(*id));
        self.label("query set", &label);
    }
}

pub trait PrettyError: Error + Sized {
    fn fmt_pretty(&self, fmt: &mut ErrorFormatter) {
        fmt.error(self);
    }
}

pub fn format_pretty_any(
    writer: &mut dyn fmt::Write,
    global: &Global,
    error: &(dyn Error + 'static),
) {
    let mut fmt = ErrorFormatter { writer, global };

    if let Some(pretty_err) = error.downcast_ref::<ContextError>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }

    if let Some(pretty_err) = error.downcast_ref::<crate::command::RenderCommandError>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::binding_model::CreateBindGroupError>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) =
        error.downcast_ref::<crate::binding_model::CreatePipelineLayoutError>()
    {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::command::ExecutionError>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::command::RenderPassErrorInner>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::command::RenderPassError>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::command::ComputePassErrorInner>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::command::ComputePassError>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::command::RenderBundleError>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::command::TransferError>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::command::PassErrorScope>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::track::UsageConflict>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }
    if let Some(pretty_err) = error.downcast_ref::<crate::command::QueryError>() {
        return pretty_err.fmt_pretty(&mut fmt);
    }

    // default
    fmt.error(error)
}

#[derive(Debug)]
pub struct ContextError {
    pub string: &'static str,
    #[cfg(send_sync)]
    pub cause: Box<dyn Error + Send + Sync + 'static>,
    #[cfg(not(send_sync))]
    pub cause: Box<dyn Error + 'static>,
    pub label_key: &'static str,
    pub label: String,
}

impl PrettyError for ContextError {
    fn fmt_pretty(&self, fmt: &mut ErrorFormatter) {
        fmt.error(self);
        fmt.label(self.label_key, &self.label);
    }
}

impl fmt::Display for ContextError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "In {}", self.string)
    }
}

impl Error for ContextError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(self.cause.as_ref())
    }
}