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