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
use crate::source::InputError;

/// Result that is returned from decoding function
#[repr(C)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum DecoderResult {
    /// Ok + decoded bytes
    Ok(Vec<u8>),

    /// Err + reason
    Err(InputError),
}

impl DecoderResult {
    /// Constructs `Ok` variant
    pub fn new_ok(output: Vec<u8>) -> Self {
        Self::Ok(output)
    }

    /// Constructs `Err` variant
    pub fn new_err(err: InputError) -> Self {
        Self::Err(err)
    }

    pub(crate) fn into_result(self) -> Result<Vec<u8>, InputError> {
        match self {
            Self::Ok(value) => Ok(value),
            Self::Err(err) => Err(err),
        }
    }
}

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

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

    pub(crate) fn as_ok(&self) -> &Vec<u8> {
        match &self {
            Self::Ok(ok) => ok,
            Self::Err(_) => panic!("DecoderResult is Err"),
        }
    }

    pub(crate) fn as_err(&self) -> &InputError {
        match &self {
            Self::Err(err) => err,
            Self::Ok(_) => panic!("DecoderResult is Ok"),
        }
    }
}