1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
extern crate libc;
extern crate time;

#[cfg(any(target_os = "linux", target_os = "android"))]
use self::linux as os_specific;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use self::mach as os_specific;
#[cfg(target_os = "windows")]
use self::windows as os_specific;

use std::error::Error;
use std::fmt;
use std::fmt::Display;
use std::io;
use time::Duration;

#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux;
#[cfg(any(target_os = "macos", target_os = "ios"))]
mod mach;
#[cfg(target_os = "windows")]
mod windows;

#[cfg(test)]
mod tests;

#[derive(Debug)]
pub enum SnoozeError {
    Unsupported(String),
    Other(io::Error)
}

#[allow(dead_code)]
impl SnoozeError {
    fn from_last_os_error() -> SnoozeError {
        SnoozeError::Other(io::Error::last_os_error())
    }
    fn from_io_error(error: io::Error) -> SnoozeError {
        SnoozeError::Other(error)
    }
}

impl Error for SnoozeError {
    fn description(&self) -> &str {
        match *self {
            SnoozeError::Unsupported(..) => "Unsupported system",
            SnoozeError::Other(..) => "System error"
        }
    }
}

impl Display for SnoozeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            SnoozeError::Unsupported(ref msg) => f.write_str(msg),
            SnoozeError::Other(ref error) => write!(f, "{}", error)
        }
    }
}

pub type SnoozeResult<T> = Result<T, SnoozeError>;

#[allow(missing_copy_implementations)]
pub struct Snooze(os_specific::Snooze);

impl Snooze {
    pub fn new(duration: Duration) -> SnoozeResult<Snooze> {
        Ok(Snooze(try!(os_specific::Snooze::new(duration))))
    }
    pub fn reset(&mut self) -> SnoozeResult<()> { self.0.reset() }
    /// Puts the current thread to sleep until the next wake-up time
    pub fn wait(&mut self) -> SnoozeResult<()> { self.0.wait() }
}