packages

csv::decode

On this page

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 Columns

Where each field of a row type is in the records: a position per field, usize::MAX for a column the header lacks.

nabi
let c = csv::Columns::of(["b", "z"], csv::Headers::new(["a", "b"]));
assert(c.position(0) == 1usize && c.position(1) is none);
  • pub fn of(names: List<str>, h: Headers) -> Columns§

    Source

    The positions of names in h; without a header (h empty), field k is column k.

    nabi
    let c = csv::Columns::of(["a", "b"], csv::Headers::new([]));
    assert(c.position(1) == 1usize);
    
  • pub fn position(self, k: usize) -> usize | none§

    Source

    The column of field k, or none when the header lacks it.

    nabi
    assert(csv::Columns::of(["a"], csv::Headers::new(["a"])).position(0) == 0usize);
    

CursorSource

pub struct Cursor

Reads one row's fields in order, for #[derive(Csv)].

nabi
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");
  • pub fn new(r: Record, c: Columns) -> Cursor§

    Source

    A cursor at the first field of r.

    nabi
    let c = csv::Cursor::new(csv::Record::new(), csv::Columns::of([], csv::Headers::new([])));
    assert(!c.has());
    
  • pub fn has(self) -> bool§

    Source

    Whether the next field is there and not empty.

    nabi
    let c = csv::Cursor::new(csv::Record::from_list([""]), csv::Columns::of(["a"], csv::Headers::new([])));
    assert(!c.has());
    
  • pub fn skip(ref self)§

    Source

    Step over the next field.

    nabi
    let 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);
    
  • pub fn next<T: FromField>(ref self) throws (CsvError) -> T§

    Source

    The next field as a T.

    nabi
    let 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.

nabi
#[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

Traits

FromFieldSource

pub trait FromField

A type one field converts to.

nabi
use csv::FromField;
assert(try! i32::from_field("-7") == -7);
let blank: bool | none = try! FromField::from_field("");
assert(blank is none);
  • fn from_field(s: str) throws (CsvError) -> Self§

    Source

    The value of field text s.

  • fn from_missing() throws (CsvError) -> Self§

    Source

    The value for a column the row does not have.

ToFieldSource

pub trait ToField

A type that writes as one field.

nabi
use csv::ToField;
let n: i32 | none = none;
assert(2.5.to_field() == "2.5" && n.to_field() == "");
  • fn to_field(self) -> str§

    Source

    The field text.

FromRecordSource

pub trait FromRecord

A row type: #[derive(Csv)] writes it.

nabi
#[derive(csv::Csv)]
struct P { x: f64; y: f64; }

use csv::FromRecord;
assert(P::columns() == ["x", "y"]);
  • fn columns() -> List<str>§

    Source

    The column names, in field order.

  • fn from_record(r: Record, c: Columns) throws (CsvError) -> Self§

    Source

    The row r, its fields at the positions c gives.

ToRecordSource

pub trait ToRecord

A row type the writer takes: #[derive(Csv)] writes it.

nabi
#[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"]);
  • fn header() -> List<str>§

    Source

    The header's names.

  • fn to_record(self, out: List<str>)§

    Source

    The fields, appended to out.