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
/// An enum with all possible kinds of errors that can be returned
/// from a decoder
#[derive(Debug, PartialEq, Eq, Clone)]
#[repr(C)]
pub enum InputError {
    /// Emitted when no custom decoder provided but input has custom encoding.
    ///
    /// You can return this error from your custom decoder if you don't support given encoding.
    UnsupportedEncoding(String),

    /// Generic error that can be emitted from a custom decoder
    DecodingError(String),
}

impl InputError {
    /// Constructs UnupportedEncoding variant
    pub fn new_unsupported_encoding(err: String) -> Self {
        Self::UnsupportedEncoding(err)
    }

    /// Constructs DecodingError variant
    pub fn new_decoding_error(err: String) -> Self {
        Self::DecodingError(err)
    }
}

#[cfg(test)]
impl InputError {
    pub(crate) fn is_unsupported_encoding(&self) -> bool {
        matches!(self, Self::UnsupportedEncoding(_))
    }

    pub(crate) fn is_decoding_error(&self) -> bool {
        matches!(self, Self::DecodingError(_))
    }

    pub(crate) fn get_unsupported_encoding_message(&self) -> &String {
        match self {
            Self::UnsupportedEncoding(message) => message,
            Self::DecodingError(_) => panic!("InputError is DecodingError"),
        }
    }

    pub(crate) fn get_decoding_error_message(&self) -> &String {
        match self {
            Self::DecodingError(message) => message,
            Self::UnsupportedEncoding(_) => panic!("InputError is UnsupportedEncoding"),
        }
    }
}