Skip to main content

mln_gfx/
error.rs

1//! The error of the calls that make devices and render targets.
2//!
3//! An [`Error`] pairs an [`ErrorKind`], which callers branch on, with a
4//! message for people. The [`Gpu`](crate::gpu::Gpu) calls fail with
5//! [`GpuError`](crate::gpu::GpuError) instead.
6
7/// What went wrong, as a caller would react to it.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub enum ErrorKind {
10    /// An argument is invalid, such as a size the device can't make.
11    InvalidArgument,
12    /// The call doesn't fit the current state, such as a read before the
13    /// first frame.
14    InvalidState,
15    /// The platform, the build or the render target lacks the feature.
16    Unsupported,
17    /// The platform or the graphics driver failed.
18    Failed,
19    /// Nothing matches what the call names, such as a device on a GPU the
20    /// host doesn't have.
21    NotFound,
22}
23
24/// A failed call: its [`ErrorKind`] and a message that says what failed.
25#[derive(Clone, Debug, PartialEq, Eq, Hash, thiserror::Error)]
26#[error("{message}")]
27pub struct Error {
28    pub kind: ErrorKind,
29    pub message: String,
30}
31
32impl Error {
33    pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
34        Error { kind, message: message.into() }
35    }
36
37    pub fn invalid_argument(message: impl Into<String>) -> Self {
38        Error::new(ErrorKind::InvalidArgument, message)
39    }
40
41    pub fn invalid_state(message: impl Into<String>) -> Self {
42        Error::new(ErrorKind::InvalidState, message)
43    }
44
45    pub fn unsupported(message: impl Into<String>) -> Self {
46        Error::new(ErrorKind::Unsupported, message)
47    }
48
49    pub fn failed(message: impl Into<String>) -> Self {
50        Error::new(ErrorKind::Failed, message)
51    }
52
53    pub fn not_found(message: impl Into<String>) -> Self {
54        Error::new(ErrorKind::NotFound, message)
55    }
56}