1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::fmt::{self, Display, Formatter};

pub struct LookupResult(Result<&'static &'static str, ConversionError>);

impl LookupResult {
    /// Returns WMO description of the code.
    pub fn description(&self) -> Option<String> {
        let Self(result) = self;
        let s = result.as_ref().ok()?.to_string();
        Some(s)
    }
}

impl Display for LookupResult {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let s = match &self.0 {
            Ok(s) => s.to_string(),
            Err(e) => e.to_string(),
        };
        write!(f, "{s}")
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ConversionError {
    Unimplemented(usize),
}

impl Display for ConversionError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::Unimplemented(code) => write!(f, "code '{code}' is not implemented"),
        }
    }
}

include!(concat!(env!("OUT_DIR"), "/cct.rs"));
include!(concat!(env!("OUT_DIR"), "/grib2_codeflag.rs"));

pub struct CommonCodeTable00;

impl ArrayLookup for CommonCodeTable00 {
    fn data(&self) -> &'static [&'static str] {
        COMMON_CODE_TABLE_00
    }
}

pub struct CommonCodeTable11;

impl ArrayLookup for CommonCodeTable11 {
    fn data(&self) -> &'static [&'static str] {
        COMMON_CODE_TABLE_11
    }
}

pub struct CodeTable0_0;

impl ArrayLookup for CodeTable0_0 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_0_0
    }
}

pub struct CodeTable1_1;

impl ArrayLookup for CodeTable1_1 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_1_1
    }
}

pub struct CodeTable1_2;

impl ArrayLookup for CodeTable1_2 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_1_2
    }
}

pub struct CodeTable1_3;

impl ArrayLookup for CodeTable1_3 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_1_3
    }
}

pub struct CodeTable1_4;

impl ArrayLookup for CodeTable1_4 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_1_4
    }
}

pub struct CodeTable3_1;

impl ArrayLookup for CodeTable3_1 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_3_1
    }
}

pub struct CodeTable4_0;

impl ArrayLookup for CodeTable4_0 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_4_0
    }
}

pub struct CodeTable4_1 {
    discipline: u8,
}

impl CodeTable4_1 {
    pub fn new(discipline: u8) -> Self {
        Self { discipline }
    }
}

impl ArrayLookup for CodeTable4_1 {
    fn data(&self) -> &'static [&'static str] {
        match self.discipline {
            0 => CODE_TABLE_4_1_0,
            1 => CODE_TABLE_4_1_1,
            2 => CODE_TABLE_4_1_2,
            3 => CODE_TABLE_4_1_3,
            4 => CODE_TABLE_4_1_4,
            10 => CODE_TABLE_4_1_10,
            20 => CODE_TABLE_4_1_20,
            _ => CODE_TABLE_UNSUPPORTED,
        }
    }
}

pub struct CodeTable4_2 {
    discipline: u8,
    parameter: u8,
}

impl CodeTable4_2 {
    pub fn new(discipline: u8, parameter: u8) -> Self {
        Self {
            discipline,
            parameter,
        }
    }
}

impl ArrayLookup for CodeTable4_2 {
    fn data(&self) -> &'static [&'static str] {
        match (self.discipline, self.parameter) {
            (0, 0) => CODE_TABLE_4_2_0_0,
            (0, 1) => CODE_TABLE_4_2_0_1,
            (0, 2) => CODE_TABLE_4_2_0_2,
            (0, 3) => CODE_TABLE_4_2_0_3,
            (0, 4) => CODE_TABLE_4_2_0_4,
            (0, 5) => CODE_TABLE_4_2_0_5,
            (0, 6) => CODE_TABLE_4_2_0_6,
            (0, 7) => CODE_TABLE_4_2_0_7,
            (0, 13) => CODE_TABLE_4_2_0_13,
            (0, 14) => CODE_TABLE_4_2_0_14,
            (0, 15) => CODE_TABLE_4_2_0_15,
            (0, 16) => CODE_TABLE_4_2_0_16,
            (0, 17) => CODE_TABLE_4_2_0_17,
            (0, 18) => CODE_TABLE_4_2_0_18,
            (0, 19) => CODE_TABLE_4_2_0_19,
            (0, 20) => CODE_TABLE_4_2_0_20,
            (0, 190) => CODE_TABLE_4_2_0_190,
            (0, 191) => CODE_TABLE_4_2_0_191,
            (1, 0) => CODE_TABLE_4_2_1_0,
            (1, 1) => CODE_TABLE_4_2_1_1,
            (1, 2) => CODE_TABLE_4_2_1_2,
            (2, 0) => CODE_TABLE_4_2_2_0,
            (2, 3) => CODE_TABLE_4_2_2_3,
            (2, 4) => CODE_TABLE_4_2_2_4,
            (2, 5) => CODE_TABLE_4_2_2_5,
            (3, 0) => CODE_TABLE_4_2_3_0,
            (3, 1) => CODE_TABLE_4_2_3_1,
            (3, 2) => CODE_TABLE_4_2_3_2,
            (3, 3) => CODE_TABLE_4_2_3_3,
            (3, 4) => CODE_TABLE_4_2_3_4,
            (3, 5) => CODE_TABLE_4_2_3_5,
            (3, 6) => CODE_TABLE_4_2_3_6,
            (4, 0) => CODE_TABLE_4_2_4_0,
            (4, 1) => CODE_TABLE_4_2_4_1,
            (4, 2) => CODE_TABLE_4_2_4_2,
            (4, 3) => CODE_TABLE_4_2_4_3,
            (4, 4) => CODE_TABLE_4_2_4_4,
            (4, 5) => CODE_TABLE_4_2_4_5,
            (4, 6) => CODE_TABLE_4_2_4_6,
            (4, 7) => CODE_TABLE_4_2_4_7,
            (4, 8) => CODE_TABLE_4_2_4_8,
            (4, 9) => CODE_TABLE_4_2_4_9,
            (4, 10) => CODE_TABLE_4_2_4_10,
            (10, 0) => CODE_TABLE_4_2_10_0,
            (10, 1) => CODE_TABLE_4_2_10_1,
            (10, 2) => CODE_TABLE_4_2_10_2,
            (10, 3) => CODE_TABLE_4_2_10_3,
            (10, 4) => CODE_TABLE_4_2_10_4,
            (10, 191) => CODE_TABLE_4_2_10_191,
            (20, 0) => CODE_TABLE_4_2_20_0,
            (20, 1) => CODE_TABLE_4_2_20_1,
            (20, 2) => CODE_TABLE_4_2_20_2,
            _ => CODE_TABLE_UNSUPPORTED,
        }
    }
}

pub struct CodeTable4_3;

impl ArrayLookup for CodeTable4_3 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_4_3
    }
}

pub struct CodeTable4_4;

impl ArrayLookup for CodeTable4_4 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_4_4
    }
}

pub struct CodeTable4_5;

impl ArrayLookup for CodeTable4_5 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_4_5
    }
}

pub struct CodeTable5_0;

impl ArrayLookup for CodeTable5_0 {
    fn data(&self) -> &'static [&'static str] {
        CODE_TABLE_5_0
    }
}

pub trait Lookup {
    fn lookup(&self, code: usize) -> LookupResult;
}

pub trait ArrayLookup: Lookup {
    fn data(&self) -> &'static [&'static str];
}

impl<T: ArrayLookup> Lookup for T {
    fn lookup(&self, code: usize) -> LookupResult {
        let result = self
            .data()
            .get(code)
            .ok_or(ConversionError::Unimplemented(code));
        LookupResult(result)
    }
}

const CODE_TABLE_UNSUPPORTED: &[&str] = &[];

pub(crate) const SUPPORTED_PROD_DEF_TEMPLATE_NUMBERS: [u16; 71] = [
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 20, 30, 31, 32, 33, 34, 35, 40, 41, 42,
    43, 44, 45, 46, 47, 48, 49, 51, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 67, 68, 70, 71, 72,
    73, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 91, 254, 1000, 1001, 1002, 1100, 1101,
];