1use serde::{Deserialize, Serialize};
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
13pub enum ErrorKind {
14 InvalidArgument,
16 InvalidState,
19 Unsupported,
21 Native,
24 Cancelled,
26 Busy,
28 TargetLost,
30 NotFound,
32 AlreadyExists,
34}
35
36#[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}