Skip to main content

mr_ulid/
error.rs

1use std::fmt;
2
3/// Errors that can occur when creating ULIDs out of foreign data.
4#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
5pub enum Error {
6    /// The ULID string is too short.
7    TooShort,
8    /// The ULID string is too long.
9    TooLong,
10    /// The ULID string contains an invalid character.
11    InvalidChar,
12    /// The value for the ULID is zero.
13    InvalidZero,
14    /// The given timestamp for the ULID is too large.
15    TimestampOutOfRange,
16    /// The given randomness for the ULID is too large.
17    RandomnessOutOfRange,
18}
19
20impl std::error::Error for Error {}
21
22impl fmt::Display for Error {
23    /// Formats the error message for display.
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        let message = match *self {
26            Self::TooShort => "string is too short",
27            Self::TooLong => "string is too long",
28            Self::InvalidChar => "string contains an invalid character",
29            Self::InvalidZero => "invalid zero value",
30            Self::TimestampOutOfRange => "timestamp is too large",
31            Self::RandomnessOutOfRange => "randomness is too large",
32        };
33        write!(f, "{message}")
34    }
35}