climer/
error.rs

1//! Error and Result types. Trying to be an idiomatic Rustacean.
2
3use std::error::Error;
4use std::fmt;
5use std::io;
6
7#[derive(Debug)]
8pub enum ClimerError {
9    NoTimeGiven,
10    TimerAlreadyRunning,
11    TimerNotRunning,
12    TimerCannotFinish,
13    NoTimeIdentifierValue(String),
14    InvalidTimeIdentifier(String),
15    InvalidInput(String),
16    InvalidPrintIntervalValue(String),
17    IoError(String),
18    Unimplemented(String),
19    UnknownError(String),
20}
21
22impl fmt::Display for ClimerError {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        use self::ClimerError::*;
25        match self {
26            NoTimeGiven => write!(f, "A time value must be given"),
27            TimerAlreadyRunning => write!(
28                f,
29                "This timer is already running. Call the `stop` method before \
30                 calling `start` again, or use the `restart` method."
31            ),
32            TimerNotRunning => write!(
33                f,
34                "This timer is not running. Call the `start` method before \
35                 calling methods such as `update` or `stop`, which require \
36                 the timer to be running before-hand."
37            ),
38            TimerCannotFinish => write!(
39                f,
40                "This timer will never finish naturally, because no \
41                 `target_time` was given. Methods such as `check_finished` \
42                 cannot be used with this timer. Give this timer a \
43                 `target_time` upon initialization if this is desired."
44            ),
45            NoTimeIdentifierValue(c) => write!(
46                f,
47                "Time string '{}' was given without a value\nExample: '10{}'",
48                c, c
49            ),
50            InvalidTimeIdentifier(input) => {
51                write!(f, "Invalid time identifier: '{}'", input)
52            }
53            InvalidInput(input) => write!(
54                f,
55                "This part of the input could not be parsed: '{}'",
56                input
57            ),
58            InvalidPrintIntervalValue(value) => write!(
59                f,
60                "Invalid value for '--interval': {}\nExpected an integer value",
61                value
62            ),
63            IoError(err) => write!(f, "IO error: {}", err),
64            Unimplemented(feature) => write!(
65                f,
66                "Sorry, this feature is not implemented yet ('{}')",
67                feature
68            ),
69            _ => write!(f, "{}", self),
70        }
71    }
72}
73
74impl Error for ClimerError {
75}
76
77impl From<io::Error> for ClimerError {
78    fn from(io_error: io::Error) -> Self {
79        ClimerError::IoError(io_error.to_string())
80    }
81}
82
83pub type ClimerResult<T = ()> = Result<T, ClimerError>;