use hex::FromHexError;
use thiserror::Error as ThisError;
use crate::error::{Error, ErrorKind};
#[derive(Clone, Debug, ThisError)]
#[non_exhaustive]
pub enum ObjectIdErrorKind {
#[error("invalid character '{c}' encountered at index {index}")]
#[non_exhaustive]
InvalidHexStringCharacter {
c: char,
index: usize,
},
#[error("invalid hex string length {length}")]
#[non_exhaustive]
InvalidHexStringLength {
length: usize,
},
}
impl Error {
pub(crate) fn from_hex_error(error: FromHexError, length: usize) -> Self {
let kind = match error {
FromHexError::InvalidHexCharacter { c, index } => {
ObjectIdErrorKind::InvalidHexStringCharacter { c, index }
}
FromHexError::InvalidStringLength | FromHexError::OddLength => {
ObjectIdErrorKind::InvalidHexStringLength { length }
}
};
ErrorKind::ObjectId { kind }.into()
}
pub(crate) fn oid_invalid_length(length: usize) -> Self {
ErrorKind::ObjectId {
kind: ObjectIdErrorKind::InvalidHexStringLength { length },
}
.into()
}
}