1#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, mln_wire::Wire)]
11pub enum ErrorKind {
12 InvalidArgument,
14 InvalidState,
17 Unsupported,
19 Failed,
22 Cancelled,
24 Busy,
26 TargetLost,
28 NotFound,
30 AlreadyExists,
32}
33
34#[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#[cfg(test)]
85mod tests {
86 use super::*;
87
88 #[test]
89 fn display_is_the_message() {
90 let error = Error::invalid_state("Style is not done loading.");
91 assert_eq!(error.kind, ErrorKind::InvalidState);
92 assert_eq!(error.to_string(), "Style is not done loading.");
93 }
94}
95
96#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, thiserror::Error)]
99#[error("unknown enumeration value")]
100pub struct ParseEnumError;