1use std::fmt;
18
19use nadir_gfx::GfxError;
20
21#[derive(Clone, Debug, PartialEq, Eq)]
23pub enum RenderError {
24 Gfx(GfxError),
27 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}