epoch_cli/
errors.rs

1//! Error types.
2
3use std::error::Error;
4use std::fmt;
5use std::num::TryFromIntError;
6use time::error::ComponentRange;
7
8pub type Result<T> = std::result::Result<T, EpochError>;
9
10/// A generic epoch error.
11#[derive(Debug)]
12pub struct EpochError {
13    pub err: String,
14}
15
16impl EpochError {
17    /// Used when an overflow or underflow occurs during time base conversions.
18    pub fn numeric_precision(err: &str) -> Self {
19        Self {
20            err: format!("A numeric precision error occurred: {}", err),
21        }
22    }
23}
24
25impl fmt::Display for EpochError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        write!(f, "epoch-cli error: {}", self.err)
28    }
29}
30
31impl Error for EpochError {}
32
33/// Convert errors from the time crate to EpochErrors.
34impl From<ComponentRange> for EpochError {
35    fn from(err: ComponentRange) -> Self {
36        Self {
37            err: err.to_string(),
38        }
39    }
40}
41
42/// Convert errors during integer conversions to EpochErrors.
43impl From<TryFromIntError> for EpochError {
44    fn from(err: TryFromIntError) -> Self {
45        Self {
46            err: err.to_string(),
47        }
48    }
49}