pxid/
error.rs

1use std::str::Utf8Error;
2
3use thiserror::Error;
4
5use crate::id::{ENCODED_LENGTH, PREFIX_LENGTH, XID_ENCODED_LENGTH};
6
7#[derive(Clone, Debug, Error, PartialEq, Eq)]
8pub enum DecodeError {
9    /// Failed to retrieve the prefix from the provided encoded PXID.
10    /// This could happen if the `_` is not present.
11    #[error("Failed to retrieve the prefix from the provided encoded PXID {0}")]
12    MissingPrefix(String),
13
14    /// The provided `String` has an invalid length and cannot be decoded
15    /// into an instance of PXID
16    #[error("String cannot be decoded into a PXID instance. {0} length is not valid. Expected length {ENCODED_LENGTH}, but received {1}")]
17    InvalidLength(String, usize),
18
19    /// The provided `String` has an invalid length and cannot be decoded
20    /// into an instance of PXID
21    #[error("String cannot be decoded into a PXID instance. {0} length is not valid. Expected length {PREFIX_LENGTH}, but received {1}")]
22    InvalidPrefixLength(String, usize),
23
24    /// The provided `String` contains an invalid character and cannot be decoded
25    /// into an instance of PXID
26    #[error("String cannot be decoded into a PXID instance. {0} length is not valid. Found invalid char {1}.")]
27    InvalidChar(String, char),
28
29    /// Invalid UTF-8 character encountered
30    #[error("Invalid UTF-8 character encountered")]
31    InvalidUtf8(Utf8Error),
32
33    /// The provided `String` has an invalid length and cannot be decoded
34    /// into an instance of PXID
35    #[error("String cannot be decoded into a PXID instance. {0} XID length is not valid. Expected length {XID_ENCODED_LENGTH}, but received {1}")]
36    InvalidXidLength(String, usize),
37}
38
39#[derive(Clone, Debug, Error, PartialEq, Eq)]
40pub enum Error {
41    /// An error ocurred decoding a value into an instance of XID
42    #[error("Failed to decode into a XID. {0}")]
43    Decode(DecodeError),
44
45    /// Failed to retrieve Machine ID
46    #[error("Failed to retrieve Machine ID. {0}")]
47    MachineID(String),
48
49    /// Prefix is too long
50    #[error("Provided prefix: {0} is too long. Max allowed characters are 4.")]
51    PrefixExceedsMaxLength(String),
52}