Fixed Width [][tests]
The fixed_width
crate is designed to facilitate easy reading and writing of fixed width files
with serde support.
Fixed width in this case means that each line of the file is the same number of bytes. It supports unicode, but only if the unicode fits in the designated byte size of the record. It also provides a few useful abstractions to ease serializing and deserializing data into and out of fixed width files.
Usage
Add as a dependency:
[]
= "0.6"
# Optionally, if you are running Rust version 1.30.0 or above and want to derive fixed width field definitions:
= "0.6"
in the root of your crate:
use fixed_width;
// and if you are using fixed_width_derive:
use FixedWidth;
Example
Read fixed width data from a &str
:
use ;
let data = "1234554321";
let mut reader = from_string.width;
for record in reader.string_reader.filter_map
Read data from a &str
using serde, using fixed_width_derive
:
use ;
use FixedWidth;
use Deserialize;
// It is not necessary to use `fixed_width_derive`, you can manually implement the `FixedWidth` trait.
let data = "1234554321";
let mut reader = from_string.width;
for bytes in reader.byte_reader.filter_map
Read data where there are different record types in the file:
use ;
use FixedWidth;
use Deserialize;
let data = "0OHIO1 BOB";
let mut reader = from_string.width;
while let Some = reader.next_record
Read data from a file:
use Reader;
use fs;
// from a file path on disk
let filepath = "/path/to/file.txt";
let mut reader = from_file.width;
// from a file handle
let file = open;
let mut reader = from_reader.width;
License
Licensed under MIT.
Contributing
Issues and pull requests are welcome, please add tests and documentation (if necessary) for any changes you submit.