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