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
//! Error types.

use std::error::Error;
use std::fmt;
use std::num::TryFromIntError;
use time::error::ComponentRange;

pub type Result<T> = std::result::Result<T, EpochError>;

/// A generic epoch error.
#[derive(Debug)]
pub struct EpochError {
    pub err: String,
}

impl EpochError {
    /// Used when an overflow or underflow occurs during time base conversions.
    pub fn numeric_precision(err: &str) -> Self {
        Self {
            err: format!("A numeric precision error occurred: {}", err),
        }
    }
}

impl fmt::Display for EpochError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "epoch-cli error: {}", self.err)
    }
}

impl Error for EpochError {}

/// Convert errors from the time crate to EpochErrors.
impl From<ComponentRange> for EpochError {
    fn from(err: ComponentRange) -> Self {
        Self {
            err: err.to_string(),
        }
    }
}

/// Convert errors during integer conversions to EpochErrors.
impl From<TryFromIntError> for EpochError {
    fn from(err: TryFromIntError) -> Self {
        Self {
            err: err.to_string(),
        }
    }
}