dbase 0.5.0

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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! Module with all structs & functions charged of writing .dbf file content
use std::fs::File;
use std::io::{BufWriter, Cursor, Seek, SeekFrom, Write};
use std::path::Path;

use byteorder::WriteBytesExt;

use crate::encoding::{AsCodePageMark, DynEncoding};
use crate::field::{types::FieldType, DeletionFlag, FieldInfo, FieldName};
use crate::header::Header;
use crate::reading::TERMINATOR_VALUE;
use crate::reading::{TableInfo, BACKLINK_SIZE};
use crate::{Encoding, Error, ErrorKind, FieldIOError, Record, UnicodeLossy};

/// A dbase file ends with this byte
const FILE_TERMINATOR: u8 = 0x1A;

pub(crate) fn write_header_parts<W>(
    dst: &mut W,
    header: &Header,
    fields_info: &[FieldInfo],
) -> Result<(), Error>
where
    W: Write,
{
    header
        .write_to(dst)
        .map_err(|error| Error::io_error(error, 0))?;

    for record_info in fields_info.iter() {
        record_info
            .write_to(dst)
            .map_err(|error| Error::io_error(error, 0))?;
    }
    dst.write_u8(TERMINATOR_VALUE)
        .map_err(|error| Error::io_error(error, 0))?;

    // TODO foxpro adds this backlink thing
    //  Since we don't have a spec for we just write zeros
    if header.file_type.is_visual_fox_pro() {
        for _ in 0..BACKLINK_SIZE {
            dst.write_u8(0).map_err(|error| Error::io_error(error, 0))?;
        }
    }

    Ok(())
}

/// Builder to be used to create a [TableWriter](struct.TableWriter.html).
///
/// The dBase format is akin to a database, thus you have to specify the fields
/// of the record you are going to write
///
/// # Example
///
/// Here we will create a writer that will be able to write records with 2 character fields
/// where both fields cannot exceed 50 bytes in length.
///
/// The writer will write its data to a cursor, but files are also supported.
/// ```
/// use dbase::{TableWriterBuilder, FieldName};
/// use std::convert::TryFrom;
/// use std::io::Cursor;
///
/// let writer = TableWriterBuilder::new()
///     .add_character_field(FieldName::try_from("First Name").unwrap(), 50)
///     .add_character_field(FieldName::try_from("Last Name").unwrap(), 50)
///     .build_with_dest(Cursor::new(Vec::<u8>::new()));
/// ```
pub struct TableWriterBuilder {
    v: Vec<FieldInfo>,
    hdr: Header,
    encoding: DynEncoding,
}

impl TableWriterBuilder {
    /// Creates a new builder with an empty dBase record definition
    ///
    /// Sets the encoding to [UnicodeLossy]
    pub fn new() -> Self {
        Self {
            v: vec![],
            hdr: Header::new(0, 0, 0),
            encoding: DynEncoding::new(UnicodeLossy),
        }
    }

    /// Creates a new builder with an empty dBase record definition with the given encoding
    pub fn with_encoding<E: Encoding + 'static>(encoding: E) -> Self {
        Self {
            v: vec![],
            hdr: Header::new(0, 0, 0),
            encoding: DynEncoding::new(encoding),
        }
    }

    /// Gets the field definition from the reader to construct the TableWriter
    ///
    /// # Example
    /// ```
    /// use dbase::{FieldValue, TableWriterBuilder};
    /// use std::io::Cursor;
    /// let mut  reader = dbase::Reader::from_path("tests/data/stations.dbf").unwrap();
    /// let mut stations = reader.read().unwrap();
    /// let old_name = stations[0].insert("name".parse().unwrap(), String::from("Montparnasse").into());
    /// assert_eq!(old_name, Some(FieldValue::Character(Some("Van Dorn Street".parse().unwrap()))));
    ///
    /// let mut writer = TableWriterBuilder::from_reader(reader)
    ///     .build_with_dest(Cursor::new(Vec::<u8>::new()));
    ///
    /// // from_reader picked up the record definition,
    /// // so writing will work
    /// let writing_result = writer.write_records(&stations);
    /// assert_eq!(writing_result.is_ok(), true);
    /// ```
    pub fn from_reader<T: std::io::Read + Seek>(reader: crate::reading::Reader<T>) -> Self {
        Self::from_table_info(reader.into_table_info())
    }

    pub fn from_table_info(table_info: TableInfo) -> Self {
        let fields_info = table_info.fields_info;
        let mut hdr = table_info.header;
        hdr.update_date();
        hdr.num_records = 0;
        Self {
            v: fields_info,
            hdr,
            encoding: table_info.encoding,
        }
    }

    /// Changes the encoding of the writer.
    pub fn set_encoding<E: Encoding + 'static>(mut self, encoding: E) -> Self {
        self.encoding = DynEncoding::new(encoding);
        self
    }

    /// Adds a Character field to the record definition,
    /// the length is the maximum number of bytes (not chars) that fields can hold
    pub fn add_character_field(mut self, name: FieldName, length: u8) -> Self {
        self.v
            .push(FieldInfo::new(name, FieldType::Character, length));
        self
    }

    /// Adds a [Date](struct.Date.html) field
    pub fn add_date_field(mut self, name: FieldName) -> Self {
        self.v.push(FieldInfo::new(
            name,
            FieldType::Date,
            FieldType::Date.size().unwrap(),
        ));
        self
    }

    /// Adds a [Numeric](enum.FieldValue.html#variant.Numeric)
    pub fn add_numeric_field(mut self, name: FieldName, length: u8, num_decimals: u8) -> Self {
        let mut info = FieldInfo::new(name, FieldType::Numeric, length);
        info.num_decimal_places = num_decimals;
        self.v.push(info);
        self
    }

    /// Adds a [Float](enum.FieldValue.html#variant.Float)
    pub fn add_float_field(mut self, name: FieldName, length: u8, num_decimals: u8) -> Self {
        let mut info = FieldInfo::new(name, FieldType::Float, length);
        info.num_decimal_places = num_decimals;
        self.v.push(info);
        self
    }

    /// Adds a [Logical](enum.FieldValue.html#variant.Logical)
    pub fn add_logical_field(mut self, name: FieldName) -> Self {
        self.v.push(FieldInfo::new(
            name,
            FieldType::Logical,
            FieldType::Logical
                .size()
                .expect("Internal error Logical field date should be known"),
        ));
        self
    }

    /// Adds a [Integer](enum.FieldValue.html#variant.Integer)
    pub fn add_integer_field(mut self, name: FieldName) -> Self {
        self.v.push(FieldInfo::new(
            name,
            FieldType::Integer,
            FieldType::Integer
                .size()
                .expect("Internal error Integer field date should be known"),
        ));
        self.hdr.file_type = crate::header::Version::FoxPro2 {
            supports_memo: false,
        };
        self
    }

    /// Adds a [DateTime](enum.FieldValue.html#variant.DateTime)
    pub fn add_datetime_field(mut self, name: FieldName) -> Self {
        self.v.push(FieldInfo::new(
            name,
            FieldType::DateTime,
            FieldType::DateTime
                .size()
                .expect("Internal error datetime field date should be known"),
        ));
        self.hdr.file_type = crate::header::Version::FoxPro2 {
            supports_memo: false,
        };
        self
    }

    /// Adds a [Double](enum.FieldValue.html#variant.Double)
    pub fn add_double_field(mut self, name: FieldName) -> Self {
        self.v.push(FieldInfo::new(
            name,
            FieldType::Double,
            FieldType::Double
                .size()
                .expect("Internal error Double field date should be known"),
        ));
        self.hdr.file_type = crate::header::Version::FoxPro2 {
            supports_memo: false,
        };
        self
    }

    /// Adds a [Currency](enum.FieldValue.html#variant.Currency)
    pub fn add_currency_field(mut self, name: FieldName) -> Self {
        self.v.push(FieldInfo::new(
            name,
            FieldType::Currency,
            FieldType::Currency
                .size()
                .expect("Internal error Currency field date should be known"),
        ));
        self.hdr.file_type = crate::header::Version::FoxPro2 {
            supports_memo: false,
        };
        self
    }

    fn sync_header(&mut self) {
        let mut offset_to_first_record =
            Header::SIZE + (self.v.len() * FieldInfo::SIZE) + std::mem::size_of::<u8>();

        if self.hdr.file_type.is_visual_fox_pro() {
            offset_to_first_record += BACKLINK_SIZE as usize;
        }

        let size_of_record = self
            .v
            .iter()
            .fold(1u16, |s, info| s + info.field_length as u16);

        self.hdr.offset_to_first_record = offset_to_first_record as u16;
        self.hdr.size_of_record = size_of_record;
        self.hdr.code_page_mark = self.encoding.code_page_mark();
    }

    /// Builds the writer and set the dst as where the file data will be written
    pub fn build_with_dest<W: Write + Seek>(mut self, dst: W) -> TableWriter<W> {
        self.sync_header();
        TableWriter::new(dst, self.v, self.hdr, self.encoding)
    }

    /// Helper function to set create a file at the given path
    /// and make the writer write to the newly created file.
    ///
    /// This function wraps the `File` in a `BufWriter` to increase performance.
    pub fn build_with_file_dest<P: AsRef<Path>>(
        self,
        path: P,
    ) -> Result<TableWriter<BufWriter<File>>, Error> {
        let file = File::create(path).map_err(|err| Error::io_error(err, 0))?;
        let dst = BufWriter::new(file);
        Ok(self.build_with_dest(dst))
    }

    pub fn build_table_info(mut self) -> TableInfo {
        self.sync_header();
        TableInfo {
            header: self.hdr,
            fields_info: self.v,
            encoding: self.encoding,
        }
    }
}

mod private {
    pub trait Sealed {}

    macro_rules! impl_sealed_for {
        ($type:ty) => {
            impl Sealed for $type {}
        };
    }

    impl_sealed_for!(bool);
    impl_sealed_for!(Option<bool>);
    impl_sealed_for!(std::string::String);
    impl_sealed_for!(Option<std::string::String>);
    impl_sealed_for!(&str);
    impl_sealed_for!(f64);
    impl_sealed_for!(f32);
    impl_sealed_for!(i32);
    impl_sealed_for!(Option<f64>);
    impl_sealed_for!(Option<f32>);
    impl_sealed_for!(crate::field::types::Date);
    impl_sealed_for!(Option<crate::field::types::Date>);
    impl_sealed_for!(crate::field::types::FieldValue);
    impl_sealed_for!(crate::field::types::DateTime);
}

/// Trait implemented by types we can write as dBase types
///
/// This trait is 'private' and cannot be implemented on your custom types.
pub trait WritableAsDbaseField: private::Sealed {
    fn write_as<E: Encoding, W: Write>(
        &self,
        field_info: &FieldInfo,
        encoding: &E,
        dst: &mut W,
    ) -> Result<(), ErrorKind>;
}

/// Trait to be implemented by struct that you want to be able to write to (serialize)
/// a dBase file
pub trait WritableRecord {
    /// Use the FieldWriter to write the fields of the record
    fn write_using<'a, W: Write>(
        &self,
        field_writer: &mut FieldWriter<'a, W>,
    ) -> Result<(), FieldIOError>;
}

impl WritableRecord for Record {
    fn write_using<'a, W: Write>(
        &self,
        field_writer: &mut FieldWriter<'a, W>,
    ) -> Result<(), FieldIOError> {
        while let Some(name) = field_writer.next_field_name() {
            let value = self.get(name).ok_or_else(|| {
                FieldIOError::new(
                    ErrorKind::Message(format!(
                        "Could not find field named '{}' in the record map",
                        name
                    )),
                    None,
                )
            })?;
            field_writer.write_next_field_value(value)?;
        }
        Ok(())
    }
}

/// Struct that knows how to write a record
///
/// You give it the values you want to write and it writes them.
/// The order and type of value must match the one given when creating the
/// [TableWriter](struct.TableWriter.html), otherwise an error will occur.
pub struct FieldWriter<'a, W: Write> {
    pub(crate) dst: &'a mut W,
    pub(crate) fields_info: std::iter::Peekable<std::slice::Iter<'a, FieldInfo>>,
    pub(crate) field_buffer: &'a mut Cursor<&'a mut [u8]>,
    pub(crate) encoding: &'a DynEncoding,
}

impl<'a, W: Write> FieldWriter<'a, W> {
    /// Returns the name of the next field that is expected to be written
    pub fn next_field_name(&mut self) -> Option<&'a str> {
        self.fields_info.peek().map(|info| info.name.as_str())
    }

    /// Writes the given `field_value` to the record.
    ///
    /// # Notes
    ///
    /// If the corresponding `FieldType` of the the field_value type (`T`) does not
    /// match the expected type an error is returned.
    ///
    /// Values for which the number of bytes written would exceed the specified field_length
    /// (if it had to be specified) will be truncated
    ///
    /// Trying to write more values than was declared when creating the writer will cause
    /// an `EndOfRecord` error.
    pub fn write_next_field_value<T: WritableAsDbaseField>(
        &mut self,
        field_value: &T,
    ) -> Result<(), FieldIOError> {
        if let Some(field_info) = self.fields_info.next() {
            let pad_before = matches!(
                field_info.field_type(),
                FieldType::Numeric | FieldType::Float | FieldType::Memo
            );

            self.field_buffer.set_position(0);
            field_value
                .write_as(field_info, self.encoding, &mut self.field_buffer)
                .map_err(|kind| FieldIOError::new(kind, Some(field_info.clone())))?;
            let value_len = self.field_buffer.position() as usize;
            let bytes_to_pad = usize::from(field_info.field_length).saturating_sub(value_len);

            if bytes_to_pad > 0 && pad_before {
                self.write_pad(bytes_to_pad, field_info)?;
            }

            // If the current field value size exceeds the one one set
            // when creating the writer, it will be cropped
            let write_len = value_len.min(field_info.field_length as usize);
            let field_bytes = self.field_buffer.get_ref();
            self.dst
                .write_all(&field_bytes[..write_len])
                .map_err(|error| {
                    FieldIOError::new(ErrorKind::IoError(error), Some(field_info.clone()))
                })?;

            if bytes_to_pad > 0 && !pad_before {
                self.write_pad(bytes_to_pad, field_info)?;
            }

            Ok(())
        } else {
            Err(FieldIOError::new(ErrorKind::TooManyFields, None))
        }
    }

    fn write_pad(&mut self, len: usize, field_info: &FieldInfo) -> Result<(), FieldIOError> {
        for _ in 0..len {
            write!(self.dst, " ").map_err(|error| {
                FieldIOError::new(ErrorKind::IoError(error), Some(field_info.clone()))
            })?;
        }
        Ok(())
    }

    #[cfg(feature = "serde")]
    pub(crate) fn write_next_field_raw(&mut self, value: &[u8]) -> Result<(), FieldIOError> {
        if let Some(field_info) = self.fields_info.next() {
            let pad_before = matches!(
                field_info.field_type(),
                FieldType::Numeric | FieldType::Float | FieldType::Memo
            );

            if value.len() == field_info.field_length as usize {
                self.dst.write_all(value).map_err(|error| {
                    FieldIOError::new(ErrorKind::IoError(error), Some(field_info.clone()))
                })?;
            } else if value.len() < field_info.field_length as usize {
                if pad_before {
                    self.write_pad(field_info.field_length as usize - value.len(), field_info)?;
                }
                self.dst.write_all(value).map_err(|error| {
                    FieldIOError::new(ErrorKind::IoError(error), Some(field_info.clone()))
                })?;
                if !pad_before {
                    self.write_pad(field_info.field_length as usize - value.len(), field_info)?;
                }
            } else {
                self.dst
                    .write_all(&value[..field_info.field_length as usize])
                    .map_err(|error| {
                        FieldIOError::new(ErrorKind::IoError(error), Some(field_info.clone()))
                    })?;
            }
            Ok(())
        } else {
            Err(FieldIOError::new(ErrorKind::EndOfRecord, None))
        }
    }

    pub(crate) fn write_deletion_flag(&mut self) -> std::io::Result<()> {
        DeletionFlag::NotDeleted.write_to(self.dst)
    }

    fn all_fields_were_written(&mut self) -> bool {
        self.fields_info.peek().is_none()
    }
}

/// Structs that writes dBase records to a destination
///
/// The only way to create a TableWriter is to use its
/// [TableWriterBuilder](struct.TableWriterBuilder.html)
pub struct TableWriter<W: Write + Seek> {
    dst: W,
    fields_info: Vec<FieldInfo>,
    /// contains the header of the input file
    /// if this writer was created form a reader
    header: Header,
    /// Buffer used by the FieldWriter
    buffer: [u8; 255],
    closed: bool,
    encoding: DynEncoding,
}

impl<W: Write + Seek> TableWriter<W> {
    fn new(
        dst: W,
        fields_info: Vec<FieldInfo>,
        origin_header: Header,
        encoding: DynEncoding,
    ) -> Self {
        Self {
            dst,
            fields_info,
            header: origin_header,
            buffer: [0u8; 255],
            closed: false,
            encoding,
        }
    }

    /// Writes a record the inner destination
    ///
    /// # Example
    ///
    /// ```
    /// use std::convert::TryFrom;
    ///
    /// # fn main() -> Result<(), dbase::Error> {
    /// let mut writer = dbase::TableWriterBuilder::new()
    ///     .add_character_field(dbase::FieldName::try_from("First Name").unwrap(), 50)
    ///     .build_with_file_dest("records.dbf")?;
    ///
    /// let mut record = dbase::Record::default();
    /// record.insert("First Name".to_string(), dbase::FieldValue::Character(Some("Yoshi".to_string())));
    ///
    /// writer.write_record(&record)?;
    ///
    /// # let ignored_result = std::fs::remove_file("record.dbf");
    /// Ok(())
    /// # }
    /// ```
    pub fn write_record<R: WritableRecord>(&mut self, record: &R) -> Result<(), Error> {
        if self.header.num_records == 0 {
            // reserve the header
            self.write_header()?;
        }

        let mut field_writer = FieldWriter {
            dst: &mut self.dst,
            fields_info: self.fields_info.iter().peekable(),
            field_buffer: &mut Cursor::new(&mut self.buffer),
            encoding: &self.encoding,
        };

        let current_record_num = self.header.num_records as usize;

        field_writer
            .write_deletion_flag()
            .map_err(|error| Error::io_error(error, current_record_num))?;

        record
            .write_using(&mut field_writer)
            .map_err(|error| Error::new(error, current_record_num))?;

        if !field_writer.all_fields_were_written() {
            return Err(Error {
                record_num: current_record_num,
                field: None,
                kind: ErrorKind::NotEnoughFields,
            });
        }

        self.header.num_records += 1;
        Ok(())
    }

    /// Writes the records to the inner destination
    ///
    /// Values for which the number of bytes written would exceed the specified field_length
    /// (if it had to be specified) will be truncated
    ///
    /// # Example
    /// ```
    /// use dbase::{TableWriterBuilder, FieldName, WritableRecord, FieldWriter, ErrorKind, FieldIOError, Encoding};
    /// use std::convert::TryFrom;
    /// use std::io::{Cursor, Write};
    ///
    /// struct User {
    ///     first_name: String,
    /// }
    ///
    /// impl WritableRecord for User {
    ///     fn write_using<'a, W>(&self,field_writer: &mut FieldWriter<'a, W>) -> Result<(), FieldIOError>
    ///         where W: Write {
    ///         field_writer.write_next_field_value(&self.first_name)
    ///     }
    /// }
    ///
    /// let mut cursor = Cursor::new(Vec::<u8>::new());
    /// let writer = TableWriterBuilder::new()
    ///     .add_character_field(FieldName::try_from("First Name").unwrap(), 50)
    ///     .build_with_dest(&mut cursor);
    ///
    /// let records = vec![
    ///     User {
    ///         first_name: "Yoshi".to_owned(),
    ///     }
    /// ];
    /// writer.write_records(&records).unwrap();
    /// assert_eq!(cursor.position(), 117)
    /// ```
    pub fn write_records<'a, R: WritableRecord + 'a, C: IntoIterator<Item = &'a R>>(
        mut self,
        records: C,
    ) -> Result<(), Error> {
        for record in records.into_iter() {
            self.write_record(record)?;
        }
        Ok(())
    }

    /// Close the writer
    ///
    /// Automatically closed when the writer is dropped,
    /// use it if you want to handle error that can happen when the writer is closing
    ///
    /// Calling close on an already closed writer is a no-op
    pub fn close(&mut self) -> Result<(), Error> {
        if !self.closed {
            self.dst
                .seek(SeekFrom::Start(0))
                .map_err(|error| Error::io_error(error, self.header.num_records as usize))?;
            self.write_header()?;
            self.dst
                .seek(SeekFrom::End(0))
                .map_err(|error| Error::io_error(error, self.header.num_records as usize))?;
            self.dst
                .write_u8(FILE_TERMINATOR)
                .map_err(|error| Error::io_error(error, self.header.num_records as usize))?;
            self.closed = true;
        }
        Ok(())
    }

    fn write_header(&mut self) -> Result<(), Error> {
        write_header_parts(&mut self.dst, &self.header, &self.fields_info)
    }
}

impl<T: Write + Seek> Drop for TableWriter<T> {
    fn drop(&mut self) {
        let _ = self.close();
    }
}