tlsh_fixed/
error.rs

1use std::{fmt::Display, num::ParseIntError};
2
3/// An enum for possible errors that might occur while calculating hash values.
4#[derive(Debug)]
5pub enum TlshError {
6    /// Input's length is too big to handle. Maximal file size is 4GB.
7    DataLenOverflow,
8    /// The hash string is malformed and cannot be parsed.
9    InvalidHashValue,
10    /// TLSH requires an input of at least 50 bytes.
11    MinSizeNotReached,
12    /// Fails to parse a hex string to integer.
13    ParseHexFailed,
14    // No valid hash found. See https://github.com/trendmicro/tlsh/issues/79
15    NoValidHash,
16}
17
18impl From<ParseIntError> for TlshError {
19    fn from(_: ParseIntError) -> Self {
20        Self::ParseHexFailed
21    }
22}
23
24impl Display for TlshError {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            TlshError::DataLenOverflow => {
28                write!(f, "Input file is too big. Maximal file size is 4GB.")
29            }
30            TlshError::InvalidHashValue => write!(f, "Can't parse hash string"),
31            TlshError::MinSizeNotReached => {
32                write!(f, "TLSH requires an input of at least 50 bytes.")
33            }
34            TlshError::ParseHexFailed => write!(f, "Can't convert hex string to integer"),
35            TlshError::NoValidHash => write!(
36                f,
37                "No valid hash could be computed. See https://github.com/trendmicro/tlsh/issues/79"
38            ),
39        }
40    }
41}