typed_shapefile 0.4.1

Read & Write .dbf in Rust
Documentation
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use std::{collections::HashMap, io::BufReader, fs::File};
use crate::{FieldInfo, FieldType, shapefile::Error, };
pub use uuid::Uuid;

pub struct Shapefile {
    pub reader: crate::shapefile::Reader<BufReader<File>> 
}

impl Shapefile {
    pub fn len(&self) -> usize {
        self.reader.dbase_reader.header.num_records as usize
    }
    pub fn open(path: &str) -> Result<Self, Error> {
        Ok(
            Shapefile {
                reader: crate::shapefile::Reader::from_path(path)?
            }
        )
    }
    pub fn fields(&self) -> &[FieldInfo] {
        self.reader.dbase_reader.fields()
    }
}
 
#[derive(Debug)]
struct FieldMeta {
    kind: FieldType,
    length: u8,
    offset: u32,
}

#[derive(Debug)]
pub struct FieldMap<'a> {
    fields: HashMap<&'a str, FieldMeta>
}

impl<'a> FieldMap<'a> {
    pub fn new(fields: &'a [FieldInfo]) -> Self {
        let mut field_map: HashMap<&'a str, FieldMeta> = HashMap::default();
        let mut offset = 0;
        for (_, field) in fields.iter().enumerate() {
            field_map.insert(
                &field.name(),
                FieldMeta {
                    kind: field.field_type(),
                    offset,
                    length: field.length(),
                }
            );
            offset += field.length() as u32
        }
        FieldMap{
            fields: field_map
        }
    }
}

pub use time::Date;
pub use time::OffsetDateTime as DateTime;
#[macro_export]
macro_rules! __field_type {
    (Uuid) => {$crate::fields::Uuid};
    (None Uuid) => {Option<$crate::fields::Uuid>};
    (str) => {&'a str};
    (bool) => {bool};
    (f64) => {f64};
    (i64) => {i64};
    (usize) => {usize};
    (Date) => {$crate::fields::Date};
    (DateTime) => {$crate::fields::DateTime};
}

#[macro_export]
macro_rules! __offset_type {
    (None Uuid) => {$crate::fields::OptionalUuidFieldOffset};
    (f64) => {$crate::fields::FloatFieldOffset};
    (i64) => {$crate::fields::IntFieldOffset};
    (usize) => {$crate::fields::UsizeFieldOffset};
    (Uuid) => {$crate::fields::UuidFieldOffset};
    (str) => {$crate::fields::StringFieldOffset};
    (bool) => {$crate::fields::BooleanFieldOffset};
    (Date) => {$crate::fields::DateFieldOffset};
    (DateTime) => {$crate::fields::DateTimeFieldOffset};
}

#[macro_export]
macro_rules! __update_id {
    (id: Uuid, $set: ident, $val: ident) => {
       $set = Some($val); 
    };
    ($id: ident: $kine:ident, $set: ident, $val: ident) => {}
}

#[macro_export]
macro_rules! schema {
    ($name: ident {$($field: ident $key: literal
                     $(or $val: literal)?
                     : $kind: ident $(else $filter: ident)?
                     $(from $arg: expr)?
    ),* $(,)?}) => {
        mod $name {
            pub struct FieldMapping {
                $($field: $crate::__offset_type!($($filter)? $kind)),*
            }
            #[derive(Debug)]
            #[allow(dead_code)]
            pub struct Row<'a> {
                __lifetime_hack: &'a ()
                $(,pub $field: $crate::__field_type!($($filter)? $kind))*
            }
            impl FieldMapping {
                pub(crate) fn of<'a>(&self, bytes: &'a [u8]) -> Result<
                        (Row<'a>,  Option<$crate::fields::Uuid>),
                    (&'static str, Option<$crate::fields::Uuid>)
                        > {
                    let mut id = None;
                    Ok((Row {
                        __lifetime_hack: &(),
                        $( $field: if let Some(val) = self.$field.of(bytes)$(.or(Some($val)))?$(.or($filter))? {
                            $crate::__update_id!($field: $kind, id, val);
                            val
                        } else {
                            return Err(($key, id))
                        }),*
                    }, id))
                }
            }
            pub(crate) fn parser(map: &$crate::fields::FieldMap<'_>) -> $crate::fields::Ret<FieldMapping> {
                Ok(FieldMapping {
                    $( $field: <$crate::__offset_type!($($filter)? $kind)>::new(
                        map, $key $(, $arg)?
                    )?),*
                })
            }
            pub fn read(mut shp: $crate::Shapefile, mut func: impl FnMut(usize, $crate::shapefile::Shape, Row<'_>) -> Result<
                    (),
                Box<dyn std::error::Error>
                >) {
                let mut reader = shp.reader;
                let get_uasfm =parser(&$crate::fields::FieldMap::new(
                    reader.dbase_reader.fields()
                )).unwrap();
                
                let mut rows = reader.dbase_reader.iter_record_rows();
                let mut shapes = reader.shape_reader.iter_shapes();
                let mut row_index = 0;
                let mut errors = 0;
                while let Some(bytes) = rows.next() {
                    let shape = shapes.next().unwrap().unwrap();
                    match get_uasfm.of(bytes) {
                        Ok((row, id)) => { if let Err(err) = func(row_index, shape, row){
                            if errors < 5 {
                                if let Some(id) = id {
                                    tiny_log::warn!("DBase Row Failed", schema=stringify!($name), id, err);
                                } else {
                                    tiny_log::warn!("DBase Row Failed", schema=stringify!($name), row_index, err);
                                }
                            } else if errors == 6 {
                                tiny_log::warn!("Many errors of have occoured, truncating");
                            }
                            errors += 1;
                        }},
                        Err((field, id)) => {
                            if errors < 5 {
                                if let Some(id) = id {
                                    tiny_log::warn!("DBase Row Parsing Error",
                                                schema=stringify!($name),
                                                id,
                                                err="Invalid/Missing Field",
                                                field
                                    );
                                } else {
                                    tiny_log::warn!("DBase Row Parsing Error",
                                                schema=stringify!($name),
                                                row_index,
                                                err="Invalid/Missing Field",
                                                field
                                    );
                                }
                            } else if errors == 6 {
                                tiny_log::warn!("Many errors of have occoured, truncating");
                            }
                            errors += 1;
                        }
                    }
                    row_index += 1;
                }
                if errors > 0 {
                    tiny_log::error!("Failed ETL entries", schema=stringify!($name), errors);
                }
            }
        }
    };
}

pub type Ret<T> = Result<T, Box<dyn std::error::Error>>;
pub struct BooleanFieldOffset{
    offset:u32,
}
impl BooleanFieldOffset {
    pub fn new(map: &FieldMap<'_>, name: &str) -> Ret<BooleanFieldOffset> {
        let field = map.fields.get(name).ok_or_else(||{
            format!("{name}: Missing Field")
        })?;

        let is_valid_type = match field.kind {
            FieldType::Character | FieldType::Numeric => {
                field.length == 1 
            },
            FieldType::Logical => true,
            _ => false
        };
        if !is_valid_type {
            return Err(format!("{name}: Invalid Type").into())
        }
        Ok(BooleanFieldOffset {
            offset: field.offset,
        })
    }
    pub fn of(&self, row_bytes: &[u8]) -> Option<bool> { 
        match *row_bytes.get(self.offset as usize)? {
            b'1' | b'T' | b't' | b'Y' | b'y' => Some(true),
            b'N' | b'n' | b'F' | b'f' | b'0' => Some(false),
            _ => {
                None
            },
        }
    }
}
pub struct UsizeFieldOffset (IntFieldOffset);

impl UsizeFieldOffset {
    pub fn new(map: &FieldMap<'_>, name: &str) -> Ret<UsizeFieldOffset> {
        Ok(UsizeFieldOffset(IntFieldOffset::new(map ,name)?))
    }
    pub fn of<'a>(&self, row_bytes: &'a [u8]) -> Option<usize> { 
        self.0.of(row_bytes)?.try_into().ok()
    }
}
// enum IntFieldKind {
//     Text
// }
pub struct IntFieldOffset {
    offset: u32,
    length: u8,
    // kind: FloatFieldKind,
}
impl IntFieldOffset {
    pub fn new(map: &FieldMap<'_>, name: &str) -> Ret<IntFieldOffset> {
        let field = map.fields.get(name).ok_or_else(||{
            format!("{name}: Missing Field")
        })?;
        if field.kind != FieldType::Character && field.kind != FieldType::Numeric {
            return Err(format!("{name}: Invalid Type").into())
        }
        Ok(IntFieldOffset {
            offset: field.offset,
            length: field.length as u8,
            // kind: FloatFieldKind::Text,
        })
    }
    pub fn byte_range(&self) -> std::ops::Range<usize> {
        self.offset as usize.. self.offset as usize + self.length as usize
    }
    pub fn of<'a>(&self, row_bytes: &'a [u8]) -> Option<i64> { 
        let bytes = trim_field_data(row_bytes.get(self.byte_range())?);
        std::str::from_utf8(bytes).ok()?.parse().ok()
    }
}

pub struct FloatFieldOffset {
    offset: u32,
    length: u8,
}

impl FloatFieldOffset {
    pub fn new(map: &FieldMap<'_>, name: &str) -> Ret<FloatFieldOffset> {
        let field = map.fields.get(name).ok_or_else(||{
            format!("{name}: Missing Field")
        })?;
        if field.kind != FieldType::Character && field.kind != FieldType::Numeric {
            return Err(format!("{name}: Invalid Type").into())
        }
        Ok(FloatFieldOffset {
            offset: field.offset,
            length: field.length as u8,
        })
    }
    pub fn byte_range(&self) -> std::ops::Range<usize> {
        self.offset as usize.. self.offset as usize + self.length as usize
    }
    pub fn of<'a>(&self, row_bytes: &'a [u8]) -> Option<f64> { 
        let bytes = trim_field_data(row_bytes.get(self.byte_range())?);
        fast_float::parse(bytes).ok()
    }
}

pub struct UuidFieldOffset(StringFieldOffset);
impl UuidFieldOffset {
    pub fn new(map: &FieldMap<'_>, name: &str) -> Ret<UuidFieldOffset> {
        Ok(UuidFieldOffset(
            StringFieldOffset::new(map, name)?
        ))
    }
    pub fn byte_range(&self) -> std::ops::Range<usize> {
        self.0.byte_range()
    }
    pub fn of(&self, row_bytes: &[u8]) -> Option<Uuid> { 
        Uuid::try_parse_ascii(self.0.of(row_bytes)?.as_bytes()).ok()
    }
}
pub struct OptionalUuidFieldOffset(StringFieldOffset);
impl OptionalUuidFieldOffset {
    pub fn new(map: &FieldMap<'_>, name: &str) -> Ret<OptionalUuidFieldOffset> {
        Ok(OptionalUuidFieldOffset(
            StringFieldOffset::new(map, name)?
        ))
    }
    pub fn byte_range(&self) -> std::ops::Range<usize> {
        self.0.byte_range()
    }
    pub fn of(&self, row_bytes: &[u8]) -> Option<Option<Uuid>> { 
        Some(Uuid::try_parse_ascii(self.0.of(row_bytes)?.as_bytes()).ok())
    }
}

pub struct DateFieldOffset(
    StringFieldOffset,
    &'static [time::format_description::FormatItem<'static>]
);
impl DateFieldOffset {
    pub fn new(map: &FieldMap<'_>, name: &str,
               formating: &'static [time::format_description::FormatItem<'static>]
    ) -> Ret<DateFieldOffset> {
        Ok(DateFieldOffset(
            StringFieldOffset::new(map,name)?,
            formating
        ))
    }
    pub fn of(&self, row_bytes: & [u8]) -> Option<Date> { 
        Date::parse(self.0.of(row_bytes)?, self.1).ok()
    }
}
pub struct StringFieldOffset {
    offset: u32,
    length: u16,
}
impl StringFieldOffset {
    pub fn new(map: &FieldMap<'_>, name: &str) -> Ret<StringFieldOffset> {
        let field = map.fields.get(name).ok_or_else(||{
            format!("{name}: Missing Field")
        })?;
        if field.kind != FieldType::Character {
            return Err(format!("{name}: Invalid Type").into())
        }
        Ok(StringFieldOffset {
            offset: field.offset,
            length: field.length as u16,
        })
    }
    pub fn byte_range(&self) -> std::ops::Range<usize> {
        self.offset as usize.. self.offset as usize + self.length as usize
    }
    pub fn of<'a>(&self, row_bytes: &'a [u8]) -> Option<&'a str> { 
        let bytes = trim_field_data(row_bytes.get(self.byte_range())?);
        std::str::from_utf8(bytes).ok()
    }
}
pub struct DateTimeFieldOffset {
    offset: u32,
    length: u16,
}
impl DateTimeFieldOffset {
    pub fn new(map: &FieldMap<'_>, name: &str) -> Ret<DateTimeFieldOffset> {
        let field = map.fields.get(name).ok_or_else(||{
            format!("{name}: Missing Field")
        })?;
        if field.kind != FieldType::Date {
            return Err(format!("{name}: Invalid Type").into())
        }
        Ok(DateTimeFieldOffset {
            offset: field.offset,
            length: field.length as u16,
        })
    }
    pub fn byte_range(&self) -> std::ops::Range<usize> {
        self.offset as usize.. self.offset as usize + self.length as usize
    }
    pub fn of<'a>(&self, row_bytes: &'a [u8]) -> Option<DateTime> { 
        let bytes = trim_field_data(row_bytes.get(self.byte_range())?);
        let s = std::str::from_utf8(bytes).ok()?;
        let year = s[0..4].parse::<i32>().ok()?;
        let month = s[4..6].parse::<u8>().ok()?;
        let day = s[6..8].parse::<u8>().ok()?;
        let date = Date::from_calendar_date(year, month.try_into().ok()?, day).ok()?;
        Some(date.with_hms(0, 0, 0).ok()?.assume_utc())
    }
}

fn trim_field_data(bytes: &[u8]) -> &[u8] {
    // Value in the dbf file is surrounded by space characters (32u8). We discard them before
    // parsing the bytes into string. Doing so doubles the performance in comparison to
    // using String::trim() afterwards.
    let mut first = usize::MAX;
    let mut last = 0;
    let ptr = bytes.as_ptr();

    // Using unchecked indexing of the vector provides around 30% increase of reading speed.
    // SAFETY: index is always between 0 and bytes.len(), so using pointers here is safe.
    unsafe {
        for i in 0..bytes.len() {
            if *ptr.add(i) == 0u8 {
                break;
            }

            if *ptr.add(i) != 32 {
                if first == usize::MAX {
                    first = i;
                }

                last = i;
            }
        }
    }

    // Input starts with zero character or consists only of spaces.
    if first == usize::MAX {
        return &[];
    }

    // Discarding spaces in front and at the end. The space character (32u8) is 00110000 in binary
    // format, which makes it safe to drop without checking, as it cannot be part of the multi-byte
    // UTF-8 symbol (all such bytes must start with 10).
    &bytes[first..(last + 1)]
}