Skip to main content

mln_util/
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
9use serde::{Deserialize, Serialize};
10
11/// What went wrong, as a caller would react to it.
12#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
13pub enum ErrorKind {
14    /// An argument is invalid, such as malformed JSON or a value out of range.
15    InvalidArgument,
16    /// The call doesn't fit the current state, such as adding a layer to a
17    /// style that hasn't loaded.
18    InvalidState,
19    /// The platform, the build or the render target lacks the feature.
20    Unsupported,
21    /// The library or the platform hit an error, such as a malformed tile or
22    /// a failed graphics driver call.
23    Native,
24    /// The work stopped before it finished, such as when its owner went away.
25    Cancelled,
26    /// Another call is still using the same object.
27    Busy,
28    /// The render target can't be drawn to anymore.
29    TargetLost,
30    /// Nothing has the given ID.
31    NotFound,
32    /// Something already has the given ID.
33    AlreadyExists,
34}
35
36/// A failed call: its [`ErrorKind`] and a message that says what failed.
37#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, thiserror::Error)]
38#[error("{message}")]
39pub struct Error {
40    pub kind: ErrorKind,
41    pub message: String,
42}
43
44impl Error {
45    pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
46        Error { kind, message: message.into() }
47    }
48
49    pub fn invalid_argument(message: impl Into<String>) -> Self {
50        Error::new(ErrorKind::InvalidArgument, message)
51    }
52
53    pub fn invalid_state(message: impl Into<String>) -> Self {
54        Error::new(ErrorKind::InvalidState, message)
55    }
56
57    pub fn unsupported(message: impl Into<String>) -> Self {
58        Error::new(ErrorKind::Unsupported, message)
59    }
60
61    pub fn native(message: impl Into<String>) -> Self {
62        Error::new(ErrorKind::Native, message)
63    }
64
65    pub fn cancelled(message: impl Into<String>) -> Self {
66        Error::new(ErrorKind::Cancelled, message)
67    }
68
69    pub fn busy(message: impl Into<String>) -> Self {
70        Error::new(ErrorKind::Busy, message)
71    }
72
73    pub fn target_lost(message: impl Into<String>) -> Self {
74        Error::new(ErrorKind::TargetLost, message)
75    }
76
77    pub fn not_found(message: impl Into<String>) -> Self {
78        Error::new(ErrorKind::NotFound, message)
79    }
80
81    pub fn already_exists(message: impl Into<String>) -> Self {
82        Error::new(ErrorKind::AlreadyExists, message)
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn display_is_the_message() {
92        let error = Error::invalid_state("Style is not done loading.");
93        assert_eq!(error.kind, ErrorKind::InvalidState);
94        assert_eq!(error.to_string(), "Style is not done loading.");
95    }
96}