cuid/
error.rs

1use std::error as stderr;
2use std::ffi;
3use std::fmt;
4use std::io;
5use std::num;
6use std::time::SystemTimeError;
7
8/// Errors for the CUID library
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum CuidError {
12    CounterError,
13    IntegerConversionError(num::TryFromIntError),
14    FingerprintError(&'static str),
15    #[allow(clippy::upper_case_acronyms)]
16    IOError(io::Error),
17    OsStringError(ffi::OsString),
18    TextError(&'static str),
19    TimestampError(SystemTimeError),
20}
21
22impl fmt::Display for CuidError {
23    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24        match self {
25            CuidError::CounterError => write!(f, "Could not retrieve counter value!"),
26            CuidError::IntegerConversionError(err) => {
27                write!(f, "Failed to convert integer: {}", err)
28            }
29            CuidError::FingerprintError(err) => {
30                write!(f, "Could not generate fingerprint: {}", err)
31            }
32            CuidError::IOError(err) => write!(f, "Error reading or writing to the system: {}", err),
33            CuidError::OsStringError(err) => {
34                write!(f, "Failed to convert Operating System String: {:?}", err)
35            }
36            CuidError::TextError(err) => write!(f, "TextError: {}", err),
37            CuidError::TimestampError(err) => write!(f, "SystemTimeError: {}", err),
38        }
39    }
40}
41
42impl stderr::Error for CuidError {
43    fn description(&self) -> &str {
44        match self {
45            CuidError::CounterError => "Could not retrieve counter",
46            CuidError::IntegerConversionError(_) => "Failed to convert integer",
47            CuidError::FingerprintError(_) => "Could not generate fingerprint",
48            CuidError::IOError(_) => "Failed performing system IO",
49            CuidError::OsStringError(_) => "Could not convert OsString",
50            CuidError::TextError(_) => "Error processing text",
51            CuidError::TimestampError(_) => "Could not generate timestamp",
52        }
53    }
54}
55
56impl From<ffi::OsString> for CuidError {
57    fn from(err: ffi::OsString) -> Self {
58        CuidError::OsStringError(err)
59    }
60}
61impl From<num::TryFromIntError> for CuidError {
62    fn from(err: num::TryFromIntError) -> Self {
63        CuidError::IntegerConversionError(err)
64    }
65}
66impl From<SystemTimeError> for CuidError {
67    fn from(err: SystemTimeError) -> Self {
68        CuidError::TimestampError(err)
69    }
70}
71impl From<io::Error> for CuidError {
72    fn from(err: io::Error) -> Self {
73        CuidError::IOError(err)
74    }
75}