grib/
error.rs

1use std::{
2    error::Error,
3    fmt::{self, Display, Formatter},
4    io,
5};
6
7use crate::decoder::*;
8
9#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10pub enum GribError {
11    InternalDataError,
12    ParseError(ParseError),
13    DecodeError(DecodeError),
14    InvalidValueError(String),
15    NotSupported(String),
16    Unknown(String),
17}
18
19impl Error for GribError {
20    fn description(&self) -> &str {
21        "grib error"
22    }
23}
24
25impl From<io::Error> for GribError {
26    fn from(e: io::Error) -> Self {
27        Self::Unknown(e.to_string())
28    }
29}
30
31impl From<ParseError> for GribError {
32    fn from(e: ParseError) -> Self {
33        Self::ParseError(e)
34    }
35}
36
37impl From<DecodeError> for GribError {
38    fn from(e: DecodeError) -> Self {
39        Self::DecodeError(e)
40    }
41}
42
43impl Display for GribError {
44    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
45        match self {
46            Self::InternalDataError => write!(f, "Something unexpected happend"),
47            Self::ParseError(e) => write!(f, "{e}"),
48            Self::DecodeError(e) => write!(f, "{e:#?}"),
49            Self::InvalidValueError(s) => write!(f, "invalid value ({s})"),
50            Self::NotSupported(s) => write!(f, "not supported ({s})"),
51            Self::Unknown(s) => write!(f, "unknown error: {s}"),
52        }
53    }
54}
55
56#[derive(Debug, Clone, PartialEq, Eq, Hash)]
57pub enum ParseError {
58    ReadError(String),
59    #[deprecated(
60        since = "0.4.4",
61        note = "This error was used only in reading Section 0 and no more used"
62    )]
63    FileTypeCheckError(String),
64    NotGRIB,
65    GRIBVersionMismatch(u8),
66    UnknownSectionNumber(u8),
67    EndSectionMismatch,
68    UnexpectedEndOfData(usize),
69    InvalidSectionOrder(usize),
70    NoGridDefinition(usize),
71}
72
73impl Error for ParseError {
74    fn description(&self) -> &str {
75        "grib parse error"
76    }
77}
78
79impl Display for ParseError {
80    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
81        match self {
82            Self::ReadError(s) => write!(f, "Read error: {s}"),
83            #[allow(deprecated)]
84            Self::FileTypeCheckError(s) => write!(f, "Error in checking file type: {s}"),
85            Self::NotGRIB => write!(f, "Not GRIB data"),
86            Self::GRIBVersionMismatch(i) => write!(f, "Not GRIB version 2: {i}"),
87            Self::UnknownSectionNumber(s) => write!(f, "Unknown section number: {s}"),
88            Self::EndSectionMismatch => write!(f, "Content of End Section is not valid"),
89            Self::UnexpectedEndOfData(i) => {
90                write!(f, "Unexpected end of data at {i}")
91            }
92            Self::InvalidSectionOrder(i) => {
93                write!(f, "GRIB2 sections wrongly ordered at {i}")
94            }
95            Self::NoGridDefinition(i) => {
96                write!(f, "Grid Definition Section not found at {i}")
97            }
98        }
99    }
100}
101
102impl From<io::Error> for ParseError {
103    fn from(e: io::Error) -> Self {
104        Self::ReadError(e.to_string())
105    }
106}
107
108impl From<BuildError> for ParseError {
109    fn from(e: BuildError) -> Self {
110        Self::ReadError(e.to_string())
111    }
112}
113
114#[derive(Debug, Clone, PartialEq, Eq, Hash)]
115pub enum BuildError {
116    SectionSizeTooSmall(usize),
117}
118
119impl Display for BuildError {
120    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
121        match self {
122            Self::SectionSizeTooSmall(i) => write!(f, "Section size is too small: {i}"),
123        }
124    }
125}