Crate fixed_width

source ·
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§

  • Helper macro for FieldSet createion with ease.

Structs§

  • An iterator of Vec<u8> records.
  • 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.
  • Defines a field in a fixed width record. There can be 1 or more fields in a fixed width record.
  • A fixed width data reader. It parses fixed width data and provides the data via iterators.
  • A serializer for fixed width data. Writes to the given Writer using the provided field definitions to determine how to serialize data into records.
  • An iterator of String records.
  • A fixed width data writer. It writes data provided in iterators to any type that implements io::Write.

Enums§

  • Errors that occur during deserialization.
  • An error produced while parsing fixed width data.
  • Field structure definition.
  • Justification of a fixed width field.
  • The type of line break between each record that should be inserted or skipped while reading.
  • Errors that occur during serialization.

Traits§

  • A trait to ease converting byte like data into a byte slice. This allows handling these types with one generic function.
  • Defines fixed width field definitions for a type.

Functions§

  • Deserialization helper for type that implements FixedWidth and Deserialize.
  • Deserializes a &[u8] into the given type that implements FixedWidth and Deserialize.
  • Deserializes &[u8] data to the given writer using the provided Fields.
  • Deserializes a &str into the given type that implements FixedWidth and Deserialize.
  • Deserializes &str data to the given writer using the provided Fields.
  • Serializes the given type that implements FixedWidth and Serialize to a String.
  • Serializes the given type that implements FixedWidth and Serialize to a String.
  • Serializes a type that implements FixedWidth to the given writer. Similar to to_writer_with_fields, but this function uses the fields defined in the trait implementation.
  • Serializes data to the given writer using the provided Fields.

Type Aliases§

  • Convenience type for Result types pertaining to this library.