pub trait ReadFixed {
// Required method
fn read_fixed<R>(buf: &mut R) -> Result<Self, Error>
where Self: Sized,
R: Read;
// Provided methods
fn read_fixed_all<R>(buf: R) -> Iter<Self, R> ⓘ
where Self: Sized,
R: Read { ... }
fn read_fixed_str(s: &str) -> Result<Self, Error>
where Self: Sized { ... }
fn read_fixed_string(s: String) -> Result<Self, Error>
where Self: Sized { ... }
}Expand description
Trait for reading from fixed width (column based) serializaiton
This trait is the main entry point to using fixcol for deserializing
column delimited data files. This trait is not normally implemented manually
but derived. The deserialization behavior of individual columns is defined
using the #[fixcol(...)] annotation.
Required Methods§
Sourcefn read_fixed<R>(buf: &mut R) -> Result<Self, Error>
fn read_fixed<R>(buf: &mut R) -> Result<Self, Error>
Reads an instance of the object from the supplied buffer
Provides logic for deserializing an instance of the type read from a supplied buffer.
§Example
use std::fs::File;
use std::io;
use fixcol::ReadFixed;
use fixcol::error::Error;
#[derive(ReadFixed)]
struct Foo {
#[fixcol(width = 3)]
foo: String,
#[fixcol(width = 3)]
bar: String,
}
let mut buffer: &[u8] = "foobar".as_bytes();
let res: Result<Foo, Error> = Foo::read_fixed(&mut buffer);Provided Methods§
Sourcefn read_fixed_all<R>(buf: R) -> Iter<Self, R> ⓘ
fn read_fixed_all<R>(buf: R) -> Iter<Self, R> ⓘ
Consumes a buffer returning objects of type Self
Lazily reads the entier content of buf returning an Iterator
over deserialized objects.
§Example
#[derive(ReadFixed)]
struct MyType {
// ...
}
let mut file = File::open("my_file.txt")?;
for res in MyType::read_fixed_all(file) {
match res {
Ok(my_type) => {
// my_type is of type MyType ... do something with it here
}
Err(_) => {
// handle error
}
}
}Sourcefn read_fixed_str(s: &str) -> Result<Self, Error>where
Self: Sized,
fn read_fixed_str(s: &str) -> Result<Self, Error>where
Self: Sized,
Reads an instance of the object fom a &str
Deserializes a single item of the type from a fixed width representation
of the object stored in a &str.
§Examples
We can parse directly from str literals
#[derive(ReadFixed)]
struct Point {
#[fixcol(width = 3, align = "right")]
x: u8,
#[fixcol(width = 3, align = "right")]
y: u8,
}
let point = Point::read_fixed_str(" 42 7")?;
assert_eq!(point.x, 42);
assert_eq!(point.y, 7);It can also be useful to pull directly from slices.
let s = ">>12361 <<";
let point = Point::read_fixed_str(&s[2..8])?;
assert_eq!(point.x, 123);
assert_eq!(point.y, 61);Sourcefn read_fixed_string(s: String) -> Result<Self, Error>where
Self: Sized,
fn read_fixed_string(s: String) -> Result<Self, Error>where
Self: Sized,
Reads an instance of the object fom a String
Deserializes a single item of the type from a fixed width representation
of the object stored in a String.
§Examples
We can parse directly from str literals
#[derive(ReadFixed)]
struct Point {
#[fixcol(width = 3, align = "right")]
x: u8,
#[fixcol(width = 3, align = "right")]
y: u8,
}
let s = String::from(" 42 7");
let point = Point::read_fixed_string(s)?;
assert_eq!(point.x, 42);
assert_eq!(point.y, 7);