#![cfg(feature = "easy-functions")]
#[cfg(all(not(feature = "std"), ffuzzy_error_in_core = "stable"))]
use core::error::Error;
#[cfg(feature = "std")]
use std::error::Error;
use crate::hash::parser_state::{ParseError, ParseErrorInfo, ParseErrorKind, ParseErrorOrigin};
use crate::hash::LongFuzzyHash;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseErrorSide {
Left,
Right,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParseErrorEither(ParseErrorSide, ParseError);
impl ParseErrorEither {
pub fn side(&self) -> ParseErrorSide {
self.0
}
}
impl ParseErrorInfo for ParseErrorEither {
fn kind(&self) -> ParseErrorKind {
self.1.kind()
}
fn origin(&self) -> ParseErrorOrigin {
self.1.origin()
}
fn offset(&self) -> usize {
self.1.offset()
}
}
impl core::fmt::Display for ParseErrorEither {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"error occurred while parsing fuzzy hash {3} ({1}, at byte offset {2}): {0}",
self.kind(),
self.origin(),
self.offset(),
match self.side() {
ParseErrorSide::Left => 1,
ParseErrorSide::Right => 2,
}
)
}
}
crate::macros::impl_error!(ParseErrorEither {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.1)
}
});
pub fn compare(lhs: &str, rhs: &str) -> Result<u32, ParseErrorEither> {
let lhs: LongFuzzyHash = match str::parse(lhs) {
Ok(value) => value,
Err(err) => {
return Err(ParseErrorEither(ParseErrorSide::Left, err));
}
};
let rhs: LongFuzzyHash = match str::parse(rhs) {
Ok(value) => value,
Err(err) => {
return Err(ParseErrorEither(ParseErrorSide::Right, err));
}
};
Ok(lhs.compare(rhs.as_ref()))
}
mod tests;