use core::fmt::{Display, Formatter, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseError {
LengthIsTooLarge,
InvalidPrefix,
InvalidCharacter,
InvalidStringLength,
InvalidChecksum,
}
impl Display for ParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_str(match self {
ParseError::LengthIsTooLarge => "length field is too large",
ParseError::InvalidPrefix => "encountered an invalid prefix",
ParseError::InvalidCharacter => "encountered an invalid character",
ParseError::InvalidStringLength => "string length is invalid",
ParseError::InvalidChecksum => "has an invalid checksum field",
})
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseError {}
#[cfg(all(not(feature = "std"), feature = "unstable"))]
impl core::error::Error for ParseError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum OperationError {
BufferIsTooSmall,
}
impl Display for OperationError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_str(match self {
OperationError::BufferIsTooSmall => "buffer is too small to store the result",
})
}
}
#[cfg(feature = "std")]
impl std::error::Error for OperationError {}
#[cfg(all(not(feature = "std"), feature = "unstable"))]
impl core::error::Error for OperationError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GeneratorErrorCategory {
DataLength,
DataDistribution,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum GeneratorError {
TooLargeInput,
TooSmallInput,
BucketsAreHalfEmpty,
BucketsAreThreeQuarterEmpty,
}
impl GeneratorError {
pub fn category(&self) -> GeneratorErrorCategory {
match *self {
GeneratorError::TooLargeInput => GeneratorErrorCategory::DataLength,
GeneratorError::TooSmallInput => GeneratorErrorCategory::DataLength,
GeneratorError::BucketsAreHalfEmpty => GeneratorErrorCategory::DataDistribution,
GeneratorError::BucketsAreThreeQuarterEmpty => GeneratorErrorCategory::DataDistribution,
}
}
}
impl Display for GeneratorError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_str(match self {
GeneratorError::TooLargeInput => "input data is too large to process",
GeneratorError::TooSmallInput => "input data is too small to process",
GeneratorError::BucketsAreHalfEmpty => {
"approximately half or more effective buckets are empty"
}
GeneratorError::BucketsAreThreeQuarterEmpty => {
"approximately 3/4 or more effective buckets are empty"
}
})
}
}
#[cfg(feature = "std")]
impl std::error::Error for GeneratorError {}
#[cfg(all(not(feature = "std"), feature = "unstable"))]
impl core::error::Error for GeneratorError {}
#[cfg(feature = "easy-functions")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseErrorSide {
Left,
Right,
}
#[cfg(feature = "easy-functions")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParseErrorEither(pub(crate) ParseErrorSide, pub(crate) ParseError);
#[cfg(feature = "easy-functions")]
impl ParseErrorEither {
pub fn side(&self) -> ParseErrorSide {
self.0
}
pub fn inner_err(&self) -> ParseError {
self.1
}
}
#[cfg(feature = "easy-functions")]
impl Display for ParseErrorEither {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
"error occurred while parsing fuzzy hash {side} ({msg})",
side = match self.side() {
ParseErrorSide::Left => 1,
ParseErrorSide::Right => 2,
},
msg = self.inner_err()
)
}
}
#[cfg(all(feature = "easy-functions", feature = "std"))]
impl std::error::Error for ParseErrorEither {}
#[cfg(all(feature = "easy-functions", not(feature = "std"), feature = "unstable"))]
impl core::error::Error for ParseErrorEither {}
#[cfg(all(feature = "easy-functions", feature = "std"))]
#[derive(Debug)]
pub enum GeneratorOrIOError {
GeneratorError(GeneratorError),
IOError(std::io::Error),
}
#[cfg(all(feature = "easy-functions", feature = "std"))]
impl Display for GeneratorOrIOError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
GeneratorOrIOError::GeneratorError(err) => err.fmt(f),
GeneratorOrIOError::IOError(err) => err.fmt(f),
}
}
}
#[cfg(all(feature = "easy-functions", feature = "std"))]
impl From<GeneratorError> for GeneratorOrIOError {
fn from(value: GeneratorError) -> Self {
GeneratorOrIOError::GeneratorError(value)
}
}
#[cfg(all(feature = "easy-functions", feature = "std"))]
impl From<std::io::Error> for GeneratorOrIOError {
fn from(value: std::io::Error) -> Self {
GeneratorOrIOError::IOError(value)
}
}
#[cfg(all(feature = "easy-functions", feature = "std"))]
impl std::error::Error for GeneratorOrIOError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
GeneratorOrIOError::GeneratorError(err) => Some(err),
GeneratorOrIOError::IOError(err) => Some(err),
}
}
}
mod tests;