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
use std::str::{from_utf8, FromStr};

pub mod cct_csv;
pub mod grib2_codeflag_csv;

pub struct CodeRange {
    start: usize,
    end: usize,
}

impl CodeRange {
    pub fn size(&self) -> usize {
        self.end - self.start + 1
    }
}

impl FromStr for CodeRange {
    type Err = CodeRangeParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let input = s.as_bytes();
        let pos = 0;

        fn read_number(
            input: &[u8],
            mut pos: usize,
        ) -> Result<(usize, usize), CodeRangeParseError> {
            let start = pos;
            while pos < input.len() && input[pos].is_ascii_digit() {
                pos += 1;
            }
            let number = from_utf8(&input[start..pos])
                .unwrap()
                .parse::<usize>()
                .or(Err(CodeRangeParseError::NumberNotFound))?;
            Ok((number, pos))
        }

        fn read_hyphen(input: &[u8], pos: usize) -> Result<usize, CodeRangeParseError> {
            if input[pos] == b'-' {
                Ok(pos + 1)
            } else {
                Err(CodeRangeParseError::HyphenNotFound)
            }
        }

        let (start, pos) = read_number(input, pos)?;
        if pos == input.len() {
            return Ok(CodeRange { start, end: start });
        }

        let pos = read_hyphen(input, pos)?;
        let (end, _pos) = read_number(input, pos)?;

        Ok(CodeRange { start, end })
    }
}

pub enum CodeRangeParseError {
    NumberNotFound,
    HyphenNotFound,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodeTable {
    desc: String,
    data: Vec<(String, String)>,
}

impl CodeTable {
    fn new(desc: String) -> Self {
        Self {
            desc,
            data: Vec::new(),
        }
    }

    fn export(&self, name: &str) -> String {
        format!(
            "\
/// {}
const {}: &[& str] = &{:#?};",
            self.desc,
            name,
            self.to_vec(),
        )
    }

    fn to_vec(&self) -> Vec<String> {
        let mut output = Vec::new();

        let mut count = 0;
        let mut empty_count = 0;

        for entry in self.data.iter() {
            let (id, string) = entry;
            let string = match string.as_str() {
                "Future versions" => None,
                "Reserved" => None,
                "Reserved for local use" => None,
                "Reserved for other centres" => None,
                "Missing" => None,
                "Missing value" => None,
                ")" => None,
                _ => Some(string),
            };

            if let Ok(range) = id.parse::<CodeRange>() {
                if let Some(string) = string {
                    while empty_count > 0 {
                        output.push(String::new());
                        count += 1;
                        empty_count -= 1;
                    }

                    if count != range.start {
                        return Vec::new(); // Sparse code tables are not
                                           // supported at the moment.
                    }
                    if range.size() == 1 {
                        output.push(string.to_string());
                    } else {
                        for _i in range.start..=range.end {
                            output.push(string.clone());
                        }
                    }
                    count += range.size();
                } else {
                    empty_count += range.size();
                }
            }
        }
        output
    }
}