pub struct TableWriterBuilder { /* private fields */ }
Expand description
Builder to be used to create a TableWriter.
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()));
Implementations§
Source§impl TableWriterBuilder
impl TableWriterBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new builder with an empty dBase record definition
Sets the encoding to UnicodeLossy
Sourcepub fn with_encoding<E: Encoding + 'static>(encoding: E) -> Self
pub fn with_encoding<E: Encoding + 'static>(encoding: E) -> Self
Creates a new builder with an empty dBase record definition with the given encoding
Sourcepub fn from_reader<T: Read + Seek>(reader: Reader<T>) -> Self
pub fn from_reader<T: Read + Seek>(reader: Reader<T>) -> Self
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_table_info(table_info: TableInfo) -> Self
Sourcepub fn set_encoding<E: Encoding + 'static>(self, encoding: E) -> Self
pub fn set_encoding<E: Encoding + 'static>(self, encoding: E) -> Self
Changes the encoding of the writer.
Sourcepub fn add_character_field(self, name: FieldName, length: u8) -> Self
pub fn add_character_field(self, name: FieldName, length: u8) -> Self
Adds a Character field to the record definition, the length is the maximum number of bytes (not chars) that fields can hold
Sourcepub fn add_date_field(self, name: FieldName) -> Self
pub fn add_date_field(self, name: FieldName) -> Self
Adds a Date field
Sourcepub fn add_numeric_field(
self,
name: FieldName,
length: u8,
num_decimals: u8,
) -> Self
pub fn add_numeric_field( self, name: FieldName, length: u8, num_decimals: u8, ) -> Self
Adds a Numeric
Sourcepub fn add_float_field(
self,
name: FieldName,
length: u8,
num_decimals: u8,
) -> Self
pub fn add_float_field( self, name: FieldName, length: u8, num_decimals: u8, ) -> Self
Adds a Float
Sourcepub fn add_logical_field(self, name: FieldName) -> Self
pub fn add_logical_field(self, name: FieldName) -> Self
Adds a Logical
Sourcepub fn add_integer_field(self, name: FieldName) -> Self
pub fn add_integer_field(self, name: FieldName) -> Self
Adds a Integer
Sourcepub fn add_datetime_field(self, name: FieldName) -> Self
pub fn add_datetime_field(self, name: FieldName) -> Self
Adds a DateTime
Sourcepub fn add_double_field(self, name: FieldName) -> Self
pub fn add_double_field(self, name: FieldName) -> Self
Adds a Double
Sourcepub fn add_currency_field(self, name: FieldName) -> Self
pub fn add_currency_field(self, name: FieldName) -> Self
Adds a Currency
Sourcepub fn build_with_dest<W: Write + Seek>(self, dst: W) -> TableWriter<W>
pub fn build_with_dest<W: Write + Seek>(self, dst: W) -> TableWriter<W>
Builds the writer and set the dst as where the file data will be written
Sourcepub fn build_with_file_dest<P: AsRef<Path>>(
self,
path: P,
) -> Result<TableWriter<BufWriter<File>>, Error>
pub fn build_with_file_dest<P: AsRef<Path>>( self, path: P, ) -> Result<TableWriter<BufWriter<File>>, Error>
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.