1use std::fmt;
2use std::io;
3
4pub type Result<T> = std::result::Result<T, Error>;
6
7#[derive(Debug, Copy, Clone, PartialEq)]
9pub enum ErrorKind {
10 MissingEventFields,
12
13 MissingOption,
15
16 UserFuncError,
18
19 ChannelError,
21
22 Io,
24}
25
26#[derive(Debug)]
28pub struct Error {
29 pub message: String,
31 pub kind: ErrorKind,
33}
34
35impl Error {
36 #[doc(hidden)]
37 pub(crate) fn missing_event_fields() -> Self {
38 Self {
39 message: String::from("event has no data"),
40 kind: ErrorKind::MissingEventFields,
41 }
42 }
43
44 #[doc(hidden)]
45 pub(crate) fn missing_option(option: &str, extra: &str) -> Self {
46 Self {
47 message: format!("missing option '{}', {}", option, extra),
48 kind: ErrorKind::MissingOption,
49 }
50 }
51
52 #[doc(hidden)]
53 pub(crate) fn sender_full(sender: &str) -> Self {
54 Self {
55 message: format!("sender '{}' is full", sender),
56 kind: ErrorKind::ChannelError,
57 }
58 }
59
60 #[doc(hidden)]
61 pub(crate) fn with_description(description: &str, kind: ErrorKind) -> Self {
62 Self {
63 message: format!("error: {}", description),
64 kind,
65 }
66 }
67}
68
69impl std::error::Error for Error {
70 fn description(&self) -> &str {
71 &*self.message
72 }
73}
74
75impl fmt::Display for Error {
76 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77 writeln!(f, "{}", self.message)
78 }
79}
80
81impl From<io::Error> for Error {
82 fn from(e: io::Error) -> Self {
83 Self::with_description(&e.to_string(), ErrorKind::Io)
84 }
85}
86
87impl<T> From<crossbeam_channel::SendError<T>> for Error {
88 fn from(e: crossbeam_channel::SendError<T>) -> Self {
89 Self::with_description(&e.to_string(), ErrorKind::ChannelError)
90 }
91}