use thiserror::Error as ThisError;
use crate::{
error::{Error, ErrorKind},
spec::BinarySubtype,
UuidRepresentation,
};
#[derive(Clone, Debug, ThisError)]
#[non_exhaustive]
pub enum UuidErrorKind {
#[error("invalid UUID string")]
#[non_exhaustive]
InvalidString {},
#[error(
"expected binary subtype {expected_binary_subtype:?} for representation \
{requested_representation:?}, got {actual_binary_subtype:?}"
)]
#[non_exhaustive]
RepresentationMismatch {
expected_binary_subtype: BinarySubtype,
actual_binary_subtype: BinarySubtype,
requested_representation: UuidRepresentation,
},
#[error("expected length of 16 bytes, got {length}")]
#[non_exhaustive]
InvalidLength {
length: usize,
},
}
impl Error {
pub(crate) fn invalid_uuid_string(message: impl ToString) -> Self {
Self::from(ErrorKind::Uuid {
kind: UuidErrorKind::InvalidString {},
})
.with_message(message)
}
pub(crate) fn uuid_representation_mismatch(
requested_representation: UuidRepresentation,
actual_binary_subtype: BinarySubtype,
expected_binary_subtype: BinarySubtype,
) -> Self {
ErrorKind::Uuid {
kind: UuidErrorKind::RepresentationMismatch {
expected_binary_subtype,
actual_binary_subtype,
requested_representation,
},
}
.into()
}
pub(crate) fn invalid_uuid_length(length: usize) -> Self {
ErrorKind::Uuid {
kind: UuidErrorKind::InvalidLength { length },
}
.into()
}
}