timer_deque_rs/
error.rs

1/*-
2 * timer-rs - a Rust crate which provides timer and timer queues based on target OS
3 *  functionality.
4 * 
5 * Copyright (C) 2025 Aleksandr Morozov
6 * 
7 * The timer-rs crate can be redistributed and/or modified
8 * under the terms of either of the following licenses:
9 *
10 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
11 *                     
12 *   2. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
13 */
14
15use std::fmt;
16
17use nix::errno::Errno;
18
19
20#[derive(Debug, Clone)]
21pub enum TimerErrorType
22{
23    /// The procvided time has expired before setting
24    Expired,
25
26    /// Error in timer subsystem
27    TimerError,
28
29    /// Performing oprtation on empty queue.
30    QueueEmpty,
31
32    /// Epoll returned error [Errno].
33    EPoll(Errno),
34
35    /// Can not convert time.
36    Conversion,
37
38    /// Some error which came from other crate.
39    ExternalError,
40
41    /// Duplicate record.
42    Duplicate,
43
44    /// Already polled
45    AlreadyPolled,
46
47    /// Item not found
48    NotFound,
49
50    /// Item belong to other instance
51    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/// Error storage.
85#[derive(Debug, Clone)]
86pub struct TimerError
87{
88    /// Error code.
89    reason: TimerErrorType,
90
91    /// Error descrition.
92    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
112/// Alias for the [Result] used by timer queues.
113pub 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}