[][src]Crate fixed_width_derive

This create provides a derive macro for the fixed_width crate's Field trait by providing a set of struct field attributes that can be used to more easily derive the trait.

The derive only works on structs. Additionally, this crate uses features that require Rust version 1.30.0+ to run.

Installing

Start by adding the dependency to your project in Cargo.toml:

fixed_width = "0.1"
fixed_width_derive = "0.1"

Then in the root of your project:

#[macro_use]
extern crate fixed_width_derive;
extern crate fixed_width;

Usage

#[macro_use]
extern crate fixed_width_derive;
extern crate fixed_width;

#[derive(FixedWidth)]
struct Person {
    #[fixed_width(range = "0..6")]
    pub name: String,
    #[fixed_width(range = "6..9", pad_with = "0")]
    pub age: usize,
    #[fixed_width(range = "9..11", name = "height_cm", justify = "right")]
    pub height: usize,
}

The above sample is equivalent to implementing the following with the fixed_width crate alone:

extern crate fixed_width;

use fixed_width::{FixedWidth, Field, Justify};

struct Person {
    pub name: String,
    pub age: usize,
    pub height: usize,
}

impl FixedWidth for Person {
    fn fields() -> Vec<Field> {
        vec![
            Field::default().range(0..6),
            Field::default().range(6..9).pad_with('0'),
            Field::default().range(9..11).justify(Justify::Right).name(Some("height_cm")),
        ]
    }
}

The full set of options you can supply for the attribute annotations are:

range = "x..y"

Required. Range values must be of type usize. The byte range of the given field.

pad_with = "c"

Defaults to ' '. Must be of type char. The character to pad to the left or right after the value of the field has been converted to bytes. For instance, if the width of the field was 5, and the value is "foo", then a left justified field padded with a results in: "fooaa".

justify = "left|right"

Defaults to "left". Must be of enum type Justify. Indicates whether this field should be justified left or right once it has been converted to bytes.

name = "s"

Defaults to the name of the struct field. Indicates the name of the field. Useful if you wish to deserialize fixed width data into a HashMap.

Derive Macros

FixedWidth