1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: Copyright (C) 2023, 2024 Tsukasa OI <floss_ssdeep@irq.a4lg.com>.


/// An enumeration representing a cause of
/// a [fuzzy hash](crate::hash::FuzzyHashData) parse error.
///
/// # Compatibility Note
///
/// Since the version 0.3, the representation of this enum will not be
/// specified as specific representation of this enum is not important.
#[repr(u8)]
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseErrorKind {
    /// Block size: is empty.
    BlockSizeIsEmpty,
    /// Block size: starts with zero.
    BlockSizeStartsWithZero,
    /// Block size: is not valid.
    BlockSizeIsInvalid,
    /// Block size: is too large to parse.
    BlockSizeIsTooLarge,
    /// Block hash (either 1 or 2): block hash is too long.
    BlockHashIsTooLong,
    /// Any: an unexpected character is encountered.
    UnexpectedCharacter,
    /// Any: an unexpected end-of-string is encountered.
    UnexpectedEndOfString,
}

impl core::fmt::Display for ParseErrorKind {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(match self {
            ParseErrorKind::BlockHashIsTooLong      => "block hash is too long",
            ParseErrorKind::BlockSizeIsEmpty        => "block size field is empty",
            ParseErrorKind::BlockSizeStartsWithZero => "block size starts with '0'",
            ParseErrorKind::BlockSizeIsInvalid      => "block size is not valid",
            ParseErrorKind::BlockSizeIsTooLarge     => "block size is too large",
            ParseErrorKind::UnexpectedCharacter     => "an unexpected character is encountered",
            ParseErrorKind::UnexpectedEndOfString   => "end-of-string is not expected",
        })
    }
}


/// A part which (possibly) caused a
/// [fuzzy hash](crate::hash::FuzzyHashData) parse error.
///
/// See [`FuzzyHashData`](crate::hash::FuzzyHashData) for corresponding parts.
///
/// Since the parser ignores the file name part,
/// this part is not in this enumeration.
///
/// # Compatibility Note
///
/// Since the version 0.3, the representation of this enum will not be
/// specified as specific representation of this enum is not important.
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseErrorOrigin {
    /// Block size.
    BlockSize,
    /// Block hash 1.
    BlockHash1,
    /// Block hash 2.
    BlockHash2,
}

impl core::fmt::Display for ParseErrorOrigin {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(match self {
            ParseErrorOrigin::BlockSize  => "block size",
            ParseErrorOrigin::BlockHash1 => "block hash 1",
            ParseErrorOrigin::BlockHash2 => "block hash 2",
        })
    }
}


/// The error type for parse operations of
/// [`FuzzyHashData`](crate::hash::FuzzyHashData).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParseError(
    pub(crate) ParseErrorKind,
    pub(crate) ParseErrorOrigin,
    pub(crate) usize
);

/// The trait implementing a [`FuzzyHashData`](crate::hash::FuzzyHashData)
/// parse error.
pub trait ParseErrorInfo {
    /// Returns the cause of the error.
    fn kind(&self) -> ParseErrorKind;
    /// Returns the part which (possibly) caused the error.
    fn origin(&self) -> ParseErrorOrigin;
    /// Returns the offset which (possibly) caused the error.
    ///
    /// Note that this offset may not be exact but may be usable as a hint.
    fn offset(&self) -> usize;
}

impl ParseErrorInfo for ParseError {
    fn kind(&self) -> ParseErrorKind { self.0 }
    fn origin(&self) -> ParseErrorOrigin { self.1 }
    fn offset(&self) -> usize { self.2 }
}

impl core::fmt::Display for ParseError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "error occurred while parsing a fuzzy hash ({1}, at byte offset {2}): {0}",
            self.kind(),
            self.origin(),
            self.offset()
        )
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ParseError {}
#[cfg(all(not(feature = "std"), feature = "nightly"))]
impl core::error::Error for ParseError {}


/// A type which represents a state after parsing a block hash.
///
/// Note that while some of them always represent one of error conditions,
/// some are valid depending on the context.
#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BlockHashParseState {
    /// The end of the string is encountered.
    MetEndOfString,
    /// A comma character (`,`) is encountered.
    MetComma,
    /// A colon character (`:`) is encountered.
    MetColon,
    /// A block hash is too long so that would cause an overflow.
    OverflowError,
    /// An invalid Base64 alphabet (or just an unexpected character) is encountered.
    Base64Error,
}





// grcov-excl-br-start
#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::test_utils::test_auto_clone;
    use crate::test_utils::test_auto_debug_for_enum;

    #[test]
    fn parse_error_kind_impls() {
        // Test Clone
        test_auto_clone::<ParseErrorKind>(&ParseErrorKind::BlockHashIsTooLong);
        // Test Display
        assert_eq!(format!("{}", ParseErrorKind::BlockHashIsTooLong),      "block hash is too long");
        assert_eq!(format!("{}", ParseErrorKind::BlockSizeIsEmpty),        "block size field is empty");
        assert_eq!(format!("{}", ParseErrorKind::BlockSizeStartsWithZero), "block size starts with '0'");
        assert_eq!(format!("{}", ParseErrorKind::BlockSizeIsInvalid),      "block size is not valid");
        assert_eq!(format!("{}", ParseErrorKind::BlockSizeIsTooLarge),     "block size is too large");
        assert_eq!(format!("{}", ParseErrorKind::UnexpectedCharacter),     "an unexpected character is encountered");
        assert_eq!(format!("{}", ParseErrorKind::UnexpectedEndOfString),   "end-of-string is not expected");
        // Test Debug
        test_auto_debug_for_enum!(
            ParseErrorKind,
            [
                BlockSizeIsEmpty,
                BlockSizeStartsWithZero,
                BlockSizeIsInvalid,
                BlockSizeIsTooLarge,
                BlockHashIsTooLong,
                UnexpectedCharacter,
                UnexpectedEndOfString,
            ]
        );
    }

    #[test]
    fn parse_error_origin_impls() {
        // Test Clone
        test_auto_clone::<ParseErrorOrigin>(&ParseErrorOrigin::BlockSize);
        #[cfg(feature = "alloc")]
        {
            // Test Display
            assert_eq!(format!("{}", ParseErrorOrigin::BlockSize),  "block size");
            assert_eq!(format!("{}", ParseErrorOrigin::BlockHash1), "block hash 1");
            assert_eq!(format!("{}", ParseErrorOrigin::BlockHash2), "block hash 2");
            // Test Debug
            test_auto_debug_for_enum!(
                ParseErrorOrigin,
                [
                    BlockSize,
                    BlockHash1,
                    BlockHash2,
                ]
            );
        }
    }

    #[test]
    fn block_hash_parse_state_impls() {
        // Test Clone
        test_auto_clone::<BlockHashParseState>(&BlockHashParseState::MetEndOfString);
        #[cfg(feature = "alloc")]
        {
            // no Display
            // Test Debug
            test_auto_debug_for_enum!(
                BlockHashParseState,
                [
                    MetEndOfString,
                    MetComma,
                    MetColon,
                    OverflowError,
                    Base64Error,
                ]
            );
        }
    }

    #[test]
    fn parse_error_basic_and_impls() {
        // Internal values
        const KIND:   ParseErrorKind   = ParseErrorKind::UnexpectedEndOfString;
        const ORIGIN: ParseErrorOrigin = ParseErrorOrigin::BlockHash1;
        const OFFSET: usize = 2;
        // Construct an error object.
        let err = ParseError(KIND, ORIGIN, OFFSET);
        // Test Clone
        test_auto_clone::<ParseError>(&err);
        // Check internal values.
        assert_eq!(err.kind(),   KIND);
        assert_eq!(err.origin(), ORIGIN);
        assert_eq!(err.offset(), OFFSET);
    }

    pub(crate) const PARSE_ERROR_CASES: [(ParseError, &str, &str); 3] = [
        (ParseError(ParseErrorKind::UnexpectedEndOfString, ParseErrorOrigin::BlockSize,  0), "(block size, at byte offset 0): end-of-string is not expected",   "ParseError(UnexpectedEndOfString, BlockSize, 0)"),
        (ParseError(ParseErrorKind::UnexpectedEndOfString, ParseErrorOrigin::BlockHash1, 2), "(block hash 1, at byte offset 2): end-of-string is not expected", "ParseError(UnexpectedEndOfString, BlockHash1, 2)"),
        (ParseError(ParseErrorKind::BlockSizeIsInvalid,    ParseErrorOrigin::BlockSize,  0), "(block size, at byte offset 0): block size is not valid",         "ParseError(BlockSizeIsInvalid, BlockSize, 0)"),
    ];

    #[test]
    fn parse_error_impls_display_and_debug() {
        for (err, err_str_display, err_str_debug) in PARSE_ERROR_CASES {
            // Test Display
            assert_eq!(
                format!("{}", err),
                format!("error occurred while parsing a fuzzy hash {}", err_str_display)
            );
            // Test Debug
            assert_eq!(format!("{:?}", err), err_str_debug);
        }
    }
}
// grcov-excl-br-stop