Skip to main content

mln_style/
error.rs

1//! The error type of the library's fallible calls.
2//!
3//! An [`Error`] pairs an [`ErrorKind`], which callers branch on, with a
4//! message for people. The kinds are maplibre-native-ffi's `mln_status`
5//! values, so a C binding maps one to the other. Failures that callers handle
6//! case by case have their own types instead, such as a style's load error or
7//! a resource request's error.
8
9/// What went wrong, as a caller would react to it.
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, mln_wire::Wire)]
11pub enum ErrorKind {
12    /// An argument is invalid, such as malformed JSON or a value out of range.
13    InvalidArgument,
14    /// The call doesn't fit the current state, such as adding a layer to a
15    /// style that hasn't loaded.
16    InvalidState,
17    /// The platform, the build or the render target lacks the feature.
18    Unsupported,
19    /// The library or the platform hit an error, such as a malformed tile or
20    /// a failed graphics driver call.
21    Failed,
22    /// The work stopped before it finished, such as when its owner went away.
23    Cancelled,
24    /// Another call is still using the same object.
25    Busy,
26    /// The render target can't be drawn to anymore.
27    TargetLost,
28    /// Nothing has the given ID.
29    NotFound,
30    /// Something already has the given ID.
31    AlreadyExists,
32}
33
34/// A failed call: its [`ErrorKind`] and a message that says what failed.
35#[derive(Clone, Debug, PartialEq, Eq, Hash, thiserror::Error, mln_wire::Wire)]
36#[error("{message}")]
37pub struct Error {
38    pub kind: ErrorKind,
39    pub message: String,
40}
41
42impl Error {
43    pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
44        Error { kind, message: message.into() }
45    }
46
47    pub fn invalid_argument(message: impl Into<String>) -> Self {
48        Error::new(ErrorKind::InvalidArgument, message)
49    }
50
51    pub fn invalid_state(message: impl Into<String>) -> Self {
52        Error::new(ErrorKind::InvalidState, message)
53    }
54
55    pub fn unsupported(message: impl Into<String>) -> Self {
56        Error::new(ErrorKind::Unsupported, message)
57    }
58
59    pub fn failed(message: impl Into<String>) -> Self {
60        Error::new(ErrorKind::Failed, message)
61    }
62
63    pub fn cancelled(message: impl Into<String>) -> Self {
64        Error::new(ErrorKind::Cancelled, message)
65    }
66
67    pub fn busy(message: impl Into<String>) -> Self {
68        Error::new(ErrorKind::Busy, message)
69    }
70
71    pub fn target_lost(message: impl Into<String>) -> Self {
72        Error::new(ErrorKind::TargetLost, message)
73    }
74
75    pub fn not_found(message: impl Into<String>) -> Self {
76        Error::new(ErrorKind::NotFound, message)
77    }
78
79    pub fn already_exists(message: impl Into<String>) -> Self {
80        Error::new(ErrorKind::AlreadyExists, message)
81    }
82}
83
84/// What parsing an enum from its name fails with: the string names none of
85/// the enum's values.
86#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, thiserror::Error)]
87#[error("unknown enumeration value")]
88pub struct ParseEnumError;