csv::decode
Typed rows. FromField / ToField convert one field; FromRecord
/ ToRecord (written by #[derive(Csv)]) a whole row, its fields
matched to the header by name.
Structs
ColumnsSource
pub struct ColumnsWhere each field of a row type is in the records: a position per
field, usize::MAX for a column the header lacks.
let c = csv::Columns::of(["b", "z"], csv::Headers::new(["a", "b"]));
assert(c.position(0) == 1usize && c.position(1) is none);
The positions of
namesinh; without a header (hempty), fieldkis columnk.nabilet c = csv::Columns::of(["a", "b"], csv::Headers::new([])); assert(c.position(1) == 1usize);The column of field
k, ornonewhen the header lacks it.nabiassert(csv::Columns::of(["a"], csv::Headers::new(["a"])).position(0) == 0usize);
CursorSource
pub struct CursorReads one row's fields in order, for #[derive(Csv)].
let rec = csv::Record::from_list(["4", "x"]);
let c = csv::Cursor::new(rec, csv::Columns::of(["n", "s"], csv::Headers::new([])));
let n = try! c.next::<i64>();
let s = try! c.next::<str>();
assert(n == 4 && s == "x");
A cursor at the first field of
r.nabilet c = csv::Cursor::new(csv::Record::new(), csv::Columns::of([], csv::Headers::new([]))); assert(!c.has());Whether the next field is there and not empty.
nabilet c = csv::Cursor::new(csv::Record::from_list([""]), csv::Columns::of(["a"], csv::Headers::new([]))); assert(!c.has());Step over the next field.
nabilet c = csv::Cursor::new(csv::Record::from_list(["1", "2"]), csv::Columns::of(["a", "b"], csv::Headers::new([]))); c.skip(); assert(try! c.next::<i32>() == 2);The next field as a
T.nabilet c = csv::Cursor::new(csv::Record::from_list(["x"]), csv::Columns::of(["n"], csv::Headers::new([]))); let n = try c.next::<i32>() catch e { assert(e.message().ends_with("field `n`: `x` is not a whole number that fits in i32; write one such as `42`")); return; }; panic(f"read {n}");
RowsSource
pub struct Rows<T>Reader::decode: the rows as Ts.
#[derive(csv::Csv)]
struct Row { a: i32; b: i32 | none; }
let rows = try! csv::from_str("a,b\n1,2\n3,\n").decode::<Row>().collect::<List<Row>>();
assert(rows.len() == 2 && rows[1].b is none);
Trait implementations 1
impl<T: FromRecord> TryIterator for Rows<T>
Traits
FromFieldSource
pub trait FromFieldA type one field converts to.
use csv::FromField;
assert(try! i32::from_field("-7") == -7);
let blank: bool | none = try! FromField::from_field("");
assert(blank is none);
The value of field text
s.The value for a column the row does not have.
ToFieldSource
pub trait ToFieldA type that writes as one field.
use csv::ToField;
let n: i32 | none = none;
assert(2.5.to_field() == "2.5" && n.to_field() == "");
The field text.
FromRecordSource
pub trait FromRecordA row type: #[derive(Csv)] writes it.
#[derive(csv::Csv)]
struct P { x: f64; y: f64; }
use csv::FromRecord;
assert(P::columns() == ["x", "y"]);
The column names, in field order.
The row
r, its fields at the positionscgives.
ToRecordSource
pub trait ToRecordA row type the writer takes: #[derive(Csv)] writes it.
#[derive(csv::Csv)]
struct P { x: i32; tag: str; }
use csv::ToRecord;
let out: List<str> = List::new();
P { x: 1, tag: "a" }.to_record(out);
assert(P::header() == ["x", "tag"] && out == ["1", "a"]);
The header's names.
The fields, appended to
out.