Skip to main content

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