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


// aliases
pub(crate) type NomErrorKind = nom::error::ErrorKind;
pub(crate) type NomError<'a> = nom::Err<(&'a[u8], NomErrorKind)>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseHeaderError {
    pub kind: ParseHeaderErrorKind,
    pub invalid_bytes: InvalidBytes,
}

impl ParseHeaderError {

    pub(crate) fn new<'a>(err: NomError<'a>, kind: ParseHeaderErrorKind) -> Self {
        Self {
            kind: kind,
            invalid_bytes: InvalidBytes::from(err),
        }
    }

    pub fn is_incomplete(&self) -> bool {
        match self.invalid_bytes {
            InvalidBytes::Incomplete(_) => true,
            _ => false,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidBytes {
    Incomplete(nom::Needed),
    Bytes(Vec<u8>)
}

impl<'a> std::convert::From<NomError<'a>> for InvalidBytes {
    fn from(err: NomError<'a>) -> Self {
        match err {
            NomError::Incomplete(needed) => InvalidBytes::Incomplete(needed),
            NomError::Error((err_bytes, _err_kind)) => InvalidBytes::Bytes(err_bytes.to_owned()),
            NomError::Failure((err_bytes, _err_kind)) => InvalidBytes::Bytes(err_bytes.to_owned()),
        }
    }
}


#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseHeaderErrorKind {
    MagicWord,
    VersionNumber,
    NonNegativeI32,
    // NameString,
    ZeroPadding,
    DimTag,
    // DimId,
    AttrTag,
    VarTag,
    DataType,
    DataElements,
    Utf8,
    Offset,
}