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