Expand description
The fixed_width
crate is designed to facilitate easy reading and writing of fixed width files with
serde support.
It also provides a few useful abstractions to ease serializing and deserializing data into and out
of fixed width files.
Users of the crate will primarily use
Reader
to read fixed width data and
Writer
to write it.
You can read or write data as Vec<String>
or as Vec<Vec<u8>>
. If you use serde, then you
can also (de)serialize into and out of structs, HashMaps, etc. Since fixed width files are
not self describing, you will need to define the set of
FieldSet
definitions for your data up front so the (de)serialization code can work.
Several errors may occur while using the library. These are defined in the
Error
type.
§Installing
Start by adding the dependency to your project in Cargo.toml
:
fixed_width = "0.5"
Then in the root of your project:
use fixed_width;
There is also the fixed_width_derive
crate that provides a struct attribute syntax to ease deriving
field definitions for your types. It is optional, but if you wish to use it you can add it to your
project like so in your Cargo.toml
:
fixed_width = "0.5"
fixed_width_derive = "0.5"
§Usage
Reading a String
:
use fixed_width::Reader;
use std::result;
let mut reader = Reader::from_string("record1record2").width(7);
let records: Vec<String> = reader
.string_reader()
.filter_map(result::Result::ok)
.collect();
Reading a String
into a Vec
of user defined structs:
use serde_derive::Deserialize;
use serde;
use fixed_width::{Reader, FixedWidth, FieldSet};
use std::result;
#[derive(Deserialize)]
struct Person {
pub name: String,
pub age: usize,
}
impl FixedWidth for Person {
fn fields() -> FieldSet {
FieldSet::Seq(vec![
FieldSet::new_field(0..6),
FieldSet::new_field(6..9),
])
}
}
let mut reader = Reader::from_string("foobar 25barfoo 35").width(9);
let records: Vec<Person> = reader
.byte_reader()
.filter_map(result::Result::ok)
.map(|bytes| fixed_width::from_bytes(&bytes).unwrap())
.collect();
!
Macros§
- field_
seq - Helper macro for
FieldSet
createion with ease.
Structs§
- Byte
Reader - An iterator of
Vec<u8>
records. - Deserializer
- A deserialized for fixed width data. Reads from the given bytes using the provided field definitions to determine how many bytes to read for each deserialized value.
- Field
Config - Defines a field in a fixed width record. There can be 1 or more fields in a fixed width record.
- Reader
- A fixed width data reader. It parses fixed width data and provides the data via iterators.
- Serializer
- A serializer for fixed width data. Writes to the given Writer using the provided field definitions to determine how to serialize data into records.
- String
Reader - An iterator of
String
records. - Writer
- A fixed width data writer. It writes data provided in iterators to any type that implements io::Write.
Enums§
- Deserialize
Error - Errors that occur during deserialization.
- Error
- An error produced while parsing fixed width data.
- Field
Set - Field structure definition.
- Justify
- Justification of a fixed width field.
- Line
Break - The type of line break between each record that should be inserted or skipped while reading.
- Serialize
Error - Errors that occur during serialization.
Traits§
- AsByte
Slice - A trait to ease converting byte like data into a byte slice. This allows handling these types with one generic function.
- Fixed
Width - Defines fixed width field definitions for a type.
Functions§
- deserialize
- Deserialization helper for type that implements
FixedWidth
andDeserialize
. - from_
bytes - Deserializes a
&[u8]
into the given type that implementsFixedWidth
andDeserialize
. - from_
bytes_ with_ fields - Deserializes
&[u8]
data to the given writer using the providedField
s. - from_
str - Deserializes a
&str
into the given type that implementsFixedWidth
andDeserialize
. - from_
str_ with_ fields - Deserializes
&str
data to the given writer using the providedField
s. - to_
bytes - Serializes the given type that implements
FixedWidth
andSerialize
to aString
. - to_
string - Serializes the given type that implements
FixedWidth
andSerialize
to aString
. - to_
writer - Serializes a type that implements
FixedWidth
to the given writer. Similar toto_writer_with_fields
, but this function uses the fields defined in the trait implementation. - to_
writer_ with_ fields - Serializes data to the given writer using the provided
Field
s.
Type Aliases§
- Result
- Convenience type for
Result
types pertaining to this library.