Skip to main content

nadir_render/
error.rs

1// Copyright 2026 Joseph Anthony Abbott III
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! The error type for `nadir-render`.
16
17use std::fmt;
18
19use nadir_gfx::GfxError;
20
21/// Errors produced by `nadir-render`.
22#[derive(Clone, Debug, PartialEq, Eq)]
23pub enum RenderError {
24    /// The GPU refused a call, or, as [`GfxError::Interface`], the shaders
25    /// do not fit their pipeline.
26    Gfx(GfxError),
27    /// The renderer's configuration cannot work.
28    BadConfig(&'static str),
29}
30
31impl fmt::Display for RenderError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::Gfx(error @ GfxError::Interface { .. }) => {
35                write!(f, "the shaders do not fit their pipeline: {error}")
36            }
37            Self::Gfx(error) => write!(f, "the GPU refused: {error}"),
38            Self::BadConfig(reason) => write!(f, "the render configuration cannot work: {reason}"),
39        }
40    }
41}
42
43impl std::error::Error for RenderError {
44    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
45        match self {
46            Self::Gfx(error) => Some(error),
47            Self::BadConfig(_) => None,
48        }
49    }
50}
51
52impl From<GfxError> for RenderError {
53    fn from(error: GfxError) -> Self {
54        Self::Gfx(error)
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn errors_say_what_went_wrong() {
64        assert_eq!(
65            RenderError::BadConfig("the virtual resolution is empty").to_string(),
66            "the render configuration cannot work: the virtual resolution is empty"
67        );
68        let error = RenderError::from(GfxError::NoPipeline);
69        assert!(error.to_string().starts_with("the GPU refused: "));
70        assert!(std::error::Error::source(&error).is_some());
71        let error = RenderError::from(GfxError::Interface {
72            label: "sprite".to_string(),
73            reason: "uniform block `View` is not one of the program's".to_string(),
74        });
75        assert_eq!(
76            error.to_string(),
77            "the shaders do not fit their pipeline: pipeline `sprite`: uniform block `View` is \
78             not one of the program's"
79        );
80    }
81}