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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
use crate::{ReadError, ReadResult, Reader};
use std::io::{Read, Seek};

#[derive(Debug)]
pub enum DataStructure {
    SingleDataItem,
    LinearStructure,
    MultiDimensionalStructure,
    Unknown3,
}

impl DataStructure {
    fn from_char(value: char) -> ReadResult<DataStructure> {
        match value {
            '0' => Ok(DataStructure::SingleDataItem),
            '1' => Ok(DataStructure::LinearStructure),
            '2' => Ok(DataStructure::MultiDimensionalStructure),
            '3' => Ok(DataStructure::Unknown3),
            e => Err(ReadError::ParseError(format!(
                "Invalid Data Structure Code: {}",
                e
            ))),
        }
    }
}

#[derive(Debug)]
pub enum DataType {
    CharacterString = 0,
    ImplicitPoint = 1,
    ExplicitPoint = 2,
    Binary = 5,
    Mixed = 6,
}

impl DataType {
    fn from_char(value: char) -> ReadResult<DataType> {
        match value {
            '0' => Ok(DataType::CharacterString),
            '1' => Ok(DataType::ImplicitPoint),
            '2' => Ok(DataType::ExplicitPoint),
            '5' => Ok(DataType::Binary),
            '6' => Ok(DataType::Mixed),
            e => Err(ReadError::ParseError(format!(
                "Invalid Data Type Code: {}",
                e
            ))),
        }
    }
}

#[derive(Debug)]
pub enum LexicalLevel {
    Level0,
    Level1,
    Level2,
    UnknownG,
}

impl LexicalLevel {
    fn from_str(value: String) -> ReadResult<LexicalLevel> {
        match value.as_ref() {
            "   " => Ok(LexicalLevel::Level0),
            "-A " => Ok(LexicalLevel::Level1),
            "%/@" => Ok(LexicalLevel::Level2),
            //FIXME: Find out what this lexical level is
            "%/G" => Ok(LexicalLevel::UnknownG),
            e => Err(ReadError::ParseError(format!(
                "Invalid Truncated Escape Sequence: {}",
                e
            ))),
        }
    }
}

#[derive(Debug)]
pub struct FieldControls {
    data_structure: DataStructure,
    data_type: DataType,
    escape_sequence: LexicalLevel,
}

#[derive(Debug)]
pub struct DataDescriptiveField {
    field_controls: FieldControls,
    field_name: String,
    array_descriptor: String,
    format_controls: String,
}

impl DataDescriptiveField {
    pub fn read<T: Read + Seek>(reader: &mut Reader<T>) -> ReadResult<DataDescriptiveField> {
        // Data structure code
        let data_structure = reader.read_char()?;
        let data_structure = DataStructure::from_char(data_structure)?;

        // Data type code
        let data_type = reader.read_char()?;
        let data_type = DataType::from_char(data_type)?;

        // Auxiliary controls must be "00"
        let auxiliary_controls = reader.read_str(2)?;
        if auxiliary_controls != "00" {
            return Err(ReadError::ParseError(format!(
                "Invalid Auxiliary Controls: {}",
                auxiliary_controls
            )));
        }
        // Printable graphics must be ";&"
        let printable_graphics = reader.read_str(2)?;
        if printable_graphics != ";&" {
            return Err(ReadError::ParseError(format!(
                "Invalid Printable Graphics: {}",
                printable_graphics
            )));
        }
        // Truncated escape sequence
        let escape_sequence = reader.read_str(3)?;
        let escape_sequence = LexicalLevel::from_str(escape_sequence)?;
        let field_name = reader.read_str_ut()?;
        let array_descriptor = reader.read_str_ut()?;
        let format_controls = reader.read_str_ft()?;

        let field_controls = FieldControls {
            data_structure,
            data_type,
            escape_sequence,
        };

        Ok(DataDescriptiveField {
            field_controls,
            field_name,
            array_descriptor,
            format_controls,
        })
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use crate::{DataDescriptiveField, ReadResult, Reader, FIELD_TERMINATOR, UNIT_TERMINATOR};
    use std::io::{BufReader, Cursor};

    pub fn ascii_data_descriptive_field(index: usize) -> ReadResult<DataDescriptiveField> {
        let bytes = [
            [
                "0500;&   ISO 8211 Record Identifier".as_bytes(),
                &[UNIT_TERMINATOR, UNIT_TERMINATOR],
                "(b12)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "1600;&   Feature record identifier field".as_bytes(),
                &[UNIT_TERMINATOR],
                "RCNM!RCID!PRIM!GRUP!OBJL!RVER!RUIN".as_bytes(),
                &[UNIT_TERMINATOR],
                "(b11,b14,2b11,2b12,b11)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "1600;&   Feature object identifier field".as_bytes(),
                &[UNIT_TERMINATOR],
                "AGEN!FIDN!FIDS".as_bytes(),
                &[UNIT_TERMINATOR],
                "(b12,b14,b12)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2600;&-A Feature record attribute field".as_bytes(),
                &[UNIT_TERMINATOR],
                "*ATTL!ATVL".as_bytes(),
                &[UNIT_TERMINATOR],
                "(b12,A)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2600;&%/@Feature record national attribute field".as_bytes(),
                &[UNIT_TERMINATOR],
                "*ATTL!ATVL".as_bytes(),
                &[UNIT_TERMINATOR],
                "(b12,A)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "1600;&   Feature record to feature object pointer control field".as_bytes(),
                &[UNIT_TERMINATOR],
                "FFUI!FFIX!NFPT".as_bytes(),
                &[UNIT_TERMINATOR],
                "(b11,2b12)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2600;&   Feature record to feature object pointer field".as_bytes(),
                &[UNIT_TERMINATOR],
                "*LNAM!RIND!COMT".as_bytes(),
                &[UNIT_TERMINATOR],
                "(B(64),b11,A)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "1600;&   Feature record to spatial record pointer control field".as_bytes(),
                &[UNIT_TERMINATOR],
                "FSUI!FSIX!NSPT".as_bytes(),
                &[UNIT_TERMINATOR],
                "(b11,2b12)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2600;&   Feature record to spatial record pointer field".as_bytes(),
                &[UNIT_TERMINATOR],
                "*NAME!ORNT!USAG!MASK".as_bytes(),
                &[UNIT_TERMINATOR],
                "(B(40),3b11)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "1600;&   Vector record identifier field".as_bytes(),
                &[UNIT_TERMINATOR],
                "RCNM!RCID!RVER!RUIN".as_bytes(),
                &[UNIT_TERMINATOR],
                "(b11,b14,b12,b11)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2600;&   Vector record attribute field".as_bytes(),
                &[UNIT_TERMINATOR],
                "*ATTL!ATVL".as_bytes(),
                &[UNIT_TERMINATOR],
                "(b12,A)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "1600;&   Vector record pointer control field".as_bytes(),
                &[UNIT_TERMINATOR],
                "VPUI!VPIX!NVPT".as_bytes(),
                &[UNIT_TERMINATOR],
                "(b11,2b12)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2600;&   Vector record pointer field".as_bytes(),
                &[UNIT_TERMINATOR],
                "*NAME!ORNT!USAG!TOPI!MASK".as_bytes(),
                &[UNIT_TERMINATOR],
                "(B(40),4b11)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "1600;&   Coordinate control field".as_bytes(),
                &[UNIT_TERMINATOR],
                "CCUI!CCIX!CCNC".as_bytes(),
                &[UNIT_TERMINATOR],
                "(b11,2b12)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2500;&   2-D Coordinate field".as_bytes(),
                &[UNIT_TERMINATOR],
                "*YCOO!XCOO".as_bytes(),
                &[UNIT_TERMINATOR],
                "(2b24)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2500;&   3-D Coordinate field".as_bytes(),
                &[UNIT_TERMINATOR],
                "*YCOO!XCOO!VE3D".as_bytes(),
                &[UNIT_TERMINATOR],
                "(3b24)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "1600;&   Arc/Curve definition field".as_bytes(),
                &[UNIT_TERMINATOR],
                "ATYP!SURF!ORDR!RESO!FPMF".as_bytes(),
                &[UNIT_TERMINATOR],
                "(3b11,2b14)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2500;&   Arc coordinate field".as_bytes(),
                &[UNIT_TERMINATOR],
                "STPT!CTPT!ENPT*YCOO!XCOO".as_bytes(),
                &[UNIT_TERMINATOR],
                "(2b24)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2500;&   Ellipse coordinate field".as_bytes(),
                &[UNIT_TERMINATOR],
                "STPT!CTPT!ENPT!CDPM!CDPR*YCOO!XCOO".as_bytes(),
                &[UNIT_TERMINATOR],
                "(2b24)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
            [
                "2500;&   Curve coordinate field".as_bytes(),
                &[UNIT_TERMINATOR],
                "*YCOO!XCOO".as_bytes(),
                &[UNIT_TERMINATOR],
                "(2b24)".as_bytes(),
                &[FIELD_TERMINATOR],
            ]
            .concat(),
        ];
        let buffer = Cursor::new(&bytes[index]);
        let bufreader = BufReader::new(buffer);
        let mut reader = Reader::new(bufreader);

        let data_descriptive_field = DataDescriptiveField::read(&mut reader)?;
        Ok(data_descriptive_field)
    }

    #[test]
    fn test_data_descriptive_fields() {
        let mut test_cases: Vec<ReadResult<DataDescriptiveField>> = Vec::with_capacity(20);
        for i in 0..20{
            test_cases.push(ascii_data_descriptive_field(i));
        } 
        for i in &test_cases {
            assert_eq!(i.is_ok(), true);
        }
    }
}