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
use std::{
    error::Error,
    fmt::{self, Display, Formatter},
    io,
};

use crate::decoder::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GribError {
    InternalDataError,
    ParseError(ParseError),
    DecodeError(DecodeError),
    InvalidValueError(String),
    NotSupported(String),
    Unknown(String),
}

impl Error for GribError {
    fn description(&self) -> &str {
        "grib error"
    }
}

impl From<ParseError> for GribError {
    fn from(e: ParseError) -> Self {
        Self::ParseError(e)
    }
}

impl From<DecodeError> for GribError {
    fn from(e: DecodeError) -> Self {
        Self::DecodeError(e)
    }
}

impl Display for GribError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::InternalDataError => write!(f, "Something unexpected happend"),
            Self::ParseError(e) => write!(f, "{e}"),
            Self::DecodeError(e) => write!(f, "{e:#?}"),
            Self::InvalidValueError(s) => write!(f, "invalid value ({s})"),
            Self::NotSupported(s) => write!(f, "not supported ({s})"),
            Self::Unknown(s) => write!(f, "unknown error: {s}"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ParseError {
    ReadError(String),
    #[deprecated(
        since = "0.4.4",
        note = "This error was used only in reading Section 0 and no more used"
    )]
    FileTypeCheckError(String),
    NotGRIB,
    GRIBVersionMismatch(u8),
    UnknownSectionNumber(u8),
    EndSectionMismatch,
    UnexpectedEndOfData(usize),
    InvalidSectionOrder(usize),
    NoGridDefinition(usize),
}

impl Error for ParseError {
    fn description(&self) -> &str {
        "grib parse error"
    }
}

impl Display for ParseError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::ReadError(s) => write!(f, "Read error: {s}"),
            #[allow(deprecated)]
            Self::FileTypeCheckError(s) => write!(f, "Error in checking file type: {s}"),
            Self::NotGRIB => write!(f, "Not GRIB data"),
            Self::GRIBVersionMismatch(i) => write!(f, "Not GRIB version 2: {i}"),
            Self::UnknownSectionNumber(s) => write!(f, "Unknown section number: {s}"),
            Self::EndSectionMismatch => write!(f, "Content of End Section is not valid"),
            Self::UnexpectedEndOfData(i) => {
                write!(f, "Unexpected end of data at {i}")
            }
            Self::InvalidSectionOrder(i) => {
                write!(f, "GRIB2 sections wrongly ordered at {i}")
            }
            Self::NoGridDefinition(i) => {
                write!(f, "Grid Definition Section not found at {i}")
            }
        }
    }
}

impl From<io::Error> for ParseError {
    fn from(e: io::Error) -> Self {
        Self::ReadError(e.to_string())
    }
}

impl From<BuildError> for ParseError {
    fn from(e: BuildError) -> Self {
        Self::ReadError(e.to_string())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BuildError {
    SectionSizeTooSmall(usize),
}

impl Display for BuildError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::SectionSizeTooSmall(i) => write!(f, "Section size is too small: {i}"),
        }
    }
}