ssdeep/internals/
compare_easy.rs1#![cfg(feature = "easy-functions")]
7
8#[cfg(all(not(feature = "std"), ffuzzy_error_in_core = "stable"))]
9use core::error::Error;
10#[cfg(feature = "std")]
11use std::error::Error;
12
13use crate::internals::hash::parser_state::{
14 ParseError, ParseErrorInfo, ParseErrorKind, ParseErrorOrigin,
15};
16use crate::internals::hash::LongFuzzyHash;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum ParseErrorSide {
26 Left,
28
29 Right,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct ParseErrorEither(ParseErrorSide, ParseError); impl ParseErrorEither {
39 pub fn side(&self) -> ParseErrorSide {
41 self.0
42 }
43}
44
45impl ParseErrorInfo for ParseErrorEither {
46 fn kind(&self) -> ParseErrorKind {
47 self.1.kind()
48 }
49 fn origin(&self) -> ParseErrorOrigin {
50 self.1.origin()
51 }
52 fn offset(&self) -> usize {
53 self.1.offset()
54 }
55}
56
57impl core::fmt::Display for ParseErrorEither {
58 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
59 write!(
60 f,
61 "error occurred while parsing fuzzy hash {3} ({1}, at byte offset {2}): {0}",
62 self.kind(),
63 self.origin(),
64 self.offset(),
65 match self.side() {
66 ParseErrorSide::Left => 1,
67 ParseErrorSide::Right => 2,
68 }
69 )
70 }
71}
72
73crate::internals::macros::impl_error!(ParseErrorEither {
74 fn source(&self) -> Option<&(dyn Error + 'static)> {
75 Some(&self.1)
76 }
77});
78
79pub fn compare(lhs: &str, rhs: &str) -> Result<u32, ParseErrorEither> {
97 let lhs: LongFuzzyHash = match str::parse(lhs) {
98 Ok(value) => value,
99 Err(err) => {
100 return Err(ParseErrorEither(ParseErrorSide::Left, err));
101 }
102 };
103 let rhs: LongFuzzyHash = match str::parse(rhs) {
104 Ok(value) => value,
105 Err(err) => {
106 return Err(ParseErrorEither(ParseErrorSide::Right, err));
107 }
108 };
109 Ok(lhs.compare(rhs.as_ref()))
110}
111
112mod tests;