1use thiserror::Error;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Debug, Error)]
6#[non_exhaustive]
7pub enum Error {
8 #[error("I/O error reading {1}: {0}")]
9 Io(#[source] std::io::Error, String),
10
11 #[error("no GRIB messages found in file")]
12 NoMessages,
13
14 #[error("message index {0} not found")]
15 MessageNotFound(usize),
16
17 #[error("unsupported GRIB edition: {0}")]
18 UnsupportedEdition(u8),
19
20 #[error("invalid GRIB message: {0}")]
21 InvalidMessage(String),
22
23 #[error("invalid section {section}: {reason}")]
24 InvalidSection { section: u8, reason: String },
25
26 #[error("invalid section order: {0}")]
27 InvalidSectionOrder(String),
28
29 #[error("unsupported grid definition template: {0}")]
30 UnsupportedGridTemplate(u16),
31
32 #[error("unsupported quasi-regular grid definition template: {template}")]
33 UnsupportedQuasiRegularGrid { template: u16 },
34
35 #[error(
36 "grid template {template} uses unsupported angular unit {basic_angle}/{subdivisions} degrees"
37 )]
38 UnsupportedGridAngularUnit {
39 template: u16,
40 basic_angle: u32,
41 subdivisions: u32,
42 },
43
44 #[error("unsupported data representation template: {0}")]
45 UnsupportedDataTemplate(u16),
46
47 #[error("unsupported complex packing group splitting method: {0}")]
48 UnsupportedGroupSplittingMethod(u8),
49
50 #[error("unsupported complex packing missing value management: {0}")]
51 UnsupportedMissingValueManagement(u8),
52
53 #[error("unsupported product definition template: {0}")]
54 UnsupportedProductTemplate(u16),
55
56 #[error("unsupported bitmap indicator: {0}")]
57 UnsupportedBitmapIndicator(u16),
58
59 #[error("unsupported packing width: {0} bits per value")]
60 UnsupportedPackingWidth(u8),
61
62 #[error("unsupported scanning mode: 0b{0:08b}")]
63 UnsupportedScanningMode(u8),
64
65 #[error("unsupported spatial differencing order: {0}")]
66 UnsupportedSpatialDifferencingOrder(u8),
67
68 #[error("data truncated at offset {offset}")]
69 Truncated { offset: u64 },
70
71 #[error("missing required field: {field}")]
72 MissingRequiredField { field: &'static str },
73
74 #[error("GRIB{edition} does not support {packing} packing")]
75 UnsupportedPackingForEdition { edition: u8, packing: &'static str },
76
77 #[error("data length mismatch: expected {expected}, got {actual}")]
78 DataLengthMismatch { expected: usize, actual: usize },
79
80 #[error("{what} limit exceeded: requested {requested}, limit {limit}")]
81 LimitExceeded {
82 what: &'static str,
83 requested: usize,
84 limit: usize,
85 },
86
87 #[error("bitmap indicates missing values but no bitmap section present")]
88 MissingBitmap,
89
90 #[error("bit offset overflow")]
91 BitOffsetOverflow,
92
93 #[error("arithmetic overflow while {operation}")]
94 ArithmeticOverflow { operation: &'static str },
95
96 #[error("value out of range: {0}")]
97 ValueOutOfRange(String),
98
99 #[error("invalid local parameter table: {0}")]
100 InvalidLocalParameterTable(String),
101
102 #[error("{codec} {operation} failed: {reason}")]
103 Codec {
104 codec: &'static str,
105 operation: &'static str,
106 reason: String,
107 },
108
109 #[error("failed to allocate {requested} {what}: {reason}")]
110 AllocationFailed {
111 what: String,
112 requested: usize,
113 reason: String,
114 },
115}
116
117impl Error {
118 pub fn allocation(what: impl Into<String>, requested: usize, reason: impl ToString) -> Self {
119 Self::AllocationFailed {
120 what: what.into(),
121 requested,
122 reason: reason.to_string(),
123 }
124 }
125}