1use std::fmt;
16
17use nix::errno::Errno;
18
19
20#[derive(Debug, Clone)]
21pub enum TimerErrorType
22{
23 Expired,
25
26 TimerError,
28
29 QueueEmpty,
31
32 EPoll(Errno),
34
35 Conversion,
37
38 ExternalError,
40
41 Duplicate,
43
44 AlreadyPolled,
46
47 NotFound,
49
50 BelongOtherInstance
52}
53
54impl fmt::Display for TimerErrorType
55{
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
57 {
58 match self
59 {
60 Self::Expired =>
61 write!(f, "absolute time has expired"),
62 Self::TimerError =>
63 write!(f, "timer setup error"),
64 Self::QueueEmpty =>
65 write!(f, "the queue is empty"),
66 Self::EPoll(errno) =>
67 write!(f, "epoll() error: {}", errno.desc()),
68 Self::Conversion =>
69 write!(f, "cannot convert time"),
70 Self::ExternalError =>
71 write!(f, "external error"),
72 Self::Duplicate =>
73 write!(f, "duplicate"),
74 Self::AlreadyPolled =>
75 write!(f, "instance is already on watch"),
76 Self::NotFound =>
77 write!(f, "not found"),
78 Self::BelongOtherInstance =>
79 write!(f, "item belongs to other instance")
80 }
81 }
82}
83
84#[derive(Debug, Clone)]
86pub struct TimerError
87{
88 reason: TimerErrorType,
90
91 msg: String
93}
94
95impl fmt::Display for TimerError
96{
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
98 {
99 write!(f, "{} {}", self.reason, self.msg)
100 }
101}
102
103impl TimerError
104{
105 pub
106 fn new(reason: TimerErrorType, msg: String) -> Self
107 {
108 return Self{ reason, msg };
109 }
110}
111
112pub type TimerResult<R> = Result<R, TimerError>;
114
115#[macro_export]
116macro_rules! timer_err
117{
118 ($reason:expr,$($arg:tt)*) => (
119 return std::result::Result::Err( $crate::error::TimerError::new($reason, format!($($arg)*)) )
120 )
121}
122
123#[macro_export]
124macro_rules! map_timer_err
125{
126 ($reason:expr, $($arg:tt)*) => (
127 $crate::error::TimerError::new($reason, format!($($arg)*))
128 )
129}