crow/
error.rs

1use std::{
2    error,
3    fmt::{self, Display, Formatter},
4};
5
6/// The super type of every error in this crate.
7/// If this is used as a return type, the question mark operator can always be used.
8#[derive(Debug)]
9pub enum Error {
10    /// Tried to create a texture with dimensions which are
11    /// greater than the maximum allowed texture size or zero.
12    InvalidTextureSize {
13        /// The requested width.
14        width: u32,
15        /// The requested height.
16        height: u32,
17    },
18    /// Error created by `image::load`.
19    ImageError(image::ImageError),
20    /// Error created by `glutin::ContextBuilder::build_windowed`.
21    CreationError(glutin::CreationError),
22    /// Error created by `glutin::ContextWrapper::make_current`
23    /// or `glutin::ContextWrapper::swap_buffers`.
24    ContextError(glutin::ContextError),
25}
26
27impl Display for Error {
28    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
29        match self {
30            Self::InvalidTextureSize { width, height } => write!(
31                f,
32                "failed to create a texture of the given size: {}x{}",
33                width, height
34            ),
35            Self::ImageError(err) => write!(f, "{}", err),
36            Self::CreationError(err) => write!(f, "{}", err),
37            Self::ContextError(err) => write!(f, "{}", err),
38        }
39    }
40}
41
42impl error::Error for Error {}
43
44#[derive(Debug)]
45/// The error returned by `Context::new`.
46pub enum NewContextError {
47    /// Error created by `glutin::ContextBuilder::build_windowed`.
48    CreationError(glutin::CreationError),
49    /// Error created by `glutin::ContextWrapper::make_current`.
50    ContextError(glutin::ContextError),
51}
52
53impl Display for NewContextError {
54    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
55        match self {
56            Self::CreationError(err) => write!(f, "{}", err),
57            Self::ContextError(err) => write!(f, "{}", err),
58        }
59    }
60}
61
62impl error::Error for NewContextError {}
63
64impl From<NewContextError> for Error {
65    fn from(e: NewContextError) -> Self {
66        match e {
67            NewContextError::CreationError(e) => Error::CreationError(e),
68            NewContextError::ContextError(e) => Error::ContextError(e),
69        }
70    }
71}
72
73/// The error returned by `Context::finalize_frame`.
74#[derive(Debug)]
75pub enum FinalizeError {
76    /// Error created by `glutin::ContextWrapper::swap_buffers`.
77    ContextError(glutin::ContextError),
78}
79
80impl Display for FinalizeError {
81    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
82        match self {
83            Self::ContextError(err) => write!(f, "{}", err),
84        }
85    }
86}
87
88impl error::Error for FinalizeError {}
89
90impl From<FinalizeError> for Error {
91    fn from(e: FinalizeError) -> Self {
92        match e {
93            FinalizeError::ContextError(e) => Error::ContextError(e),
94        }
95    }
96}
97
98/// The error returned by `Texture::load`.
99#[derive(Debug)]
100pub enum LoadTextureError {
101    /// Tried to create a texture with dimensions which are
102    /// greater than the maximum allowed texture size or zero.
103    InvalidTextureSize {
104        /// The requested width.
105        width: u32,
106        /// The requested height.
107        height: u32,
108    },
109    /// Error created by `image::load`.
110    ImageError(image::ImageError),
111}
112
113impl Display for LoadTextureError {
114    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
115        match self {
116            Self::InvalidTextureSize { width, height } => write!(
117                f,
118                "failed to create a texture of the given size: {}x{}",
119                width, height
120            ),
121            Self::ImageError(err) => write!(f, "{}", err),
122        }
123    }
124}
125
126impl error::Error for LoadTextureError {}
127
128impl From<LoadTextureError> for Error {
129    fn from(e: LoadTextureError) -> Self {
130        match e {
131            LoadTextureError::InvalidTextureSize { width, height } => {
132                Error::InvalidTextureSize { width, height }
133            }
134            LoadTextureError::ImageError(e) => Error::ImageError(e),
135        }
136    }
137}
138
139/// The error returned by `Texture::new`.
140#[derive(Debug)]
141pub enum NewTextureError {
142    /// Tried to create a texture with dimensions which are
143    /// greater than the maximum allowed texture size or zero.
144    InvalidTextureSize {
145        /// The requested width.
146        width: u32,
147        /// The requested height.
148        height: u32,
149    },
150}
151
152impl Display for NewTextureError {
153    fn fmt<'a>(&'a self, f: &mut Formatter<'_>) -> fmt::Result {
154        match self {
155            Self::InvalidTextureSize { width, height } => write!(
156                f,
157                "failed to create a texture of the given size: {}x{}",
158                width, height
159            ),
160        }
161    }
162}
163
164impl error::Error for NewTextureError {}
165
166impl From<NewTextureError> for LoadTextureError {
167    fn from(e: NewTextureError) -> Self {
168        match e {
169            NewTextureError::InvalidTextureSize { width, height } => {
170                LoadTextureError::InvalidTextureSize { width, height }
171            }
172        }
173    }
174}
175
176impl From<NewTextureError> for Error {
177    fn from(e: NewTextureError) -> Self {
178        match e {
179            NewTextureError::InvalidTextureSize { width, height } => {
180                Error::InvalidTextureSize { width, height }
181            }
182        }
183    }
184}