Skip to main content

xuid/
error.rs

1use core::fmt;
2
3/// Errors returned by the xuid library.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum Error {
6    /// The parsed prefix did not match the one expected by [`Xuid::parse_prefix`].
7    ///
8    /// [`Xuid::parse_prefix`]: crate::Xuid::parse_prefix
9    BadPrefix {
10        /// The prefix the caller expected.
11        expected: String,
12        /// The prefix actually found in the input.
13        found: String,
14    },
15    /// The base32 body of an XUID could not be decoded.
16    InvalidEncoding,
17    /// The input could not be parsed as either an XUID or a standard UUID.
18    InvalidUuid,
19}
20
21impl fmt::Display for Error {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Error::BadPrefix { expected, found } => {
25                write!(f, "xuid: bad prefix {found:?}, expected {expected:?}")
26            }
27            Error::InvalidEncoding => write!(f, "xuid: invalid base32 encoding"),
28            Error::InvalidUuid => write!(f, "xuid: invalid uuid"),
29        }
30    }
31}
32
33impl std::error::Error for Error {}
34
35impl From<uuid::Error> for Error {
36    fn from(_: uuid::Error) -> Self {
37        Error::InvalidUuid
38    }
39}
40
41impl From<data_encoding::DecodeError> for Error {
42    fn from(_: data_encoding::DecodeError) -> Self {
43        Error::InvalidEncoding
44    }
45}