Skip to main content

CsvReader

Struct CsvReader 

Source
pub struct CsvReader { /* private fields */ }
Expand description

Reads CSV or TSV text into Rows. See the module docs.

Implementations§

Source§

impl CsvReader

Source

pub fn new() -> Self

Sniff the dialect from DELIMITERS and the header, failing when the sniffer finds no delimiter (set a fallback to read such input anyway).

Source

pub fn tsv() -> Self

Tab-separated, with excel’s quoting (csv.get_dialect("excel-tab")).

Source

pub fn delimiter(self, delimiter: char) -> Self

Use this delimiter, with excel’s quoting, instead of sniffing.

Source

pub fn dialect(self, dialect: Dialect) -> Self

Use this dialect instead of sniffing.

Source

pub fn fallback(self, delimiter: char) -> Self

The delimiter to fall back to when the sniffer finds none, as rich --csv falls back to , for a .csv file and a tab for a .tsv one.

Source

pub fn header(self, header: bool) -> Self

Whether the first row is a header (true/false), instead of sniffing it.

Source

pub fn header_mode(self, header: Header) -> Self

Set the header decision, Header::Sniff included.

Source

pub fn detect(&self, content: &str) -> Option<(Dialect, bool)>

The dialect and header decision for content, as read makes them; None when the sniffer finds no delimiter and there is no fallback.

Source

pub fn read(&self, content: &str) -> Result<Rows, DataError>

Read content. Blank lines are skipped; a row longer than the header adds columns named by position, and a shorter one is padded with nulls, so no field is lost.

Source§

impl CsvReader

Source

pub fn source<R: BufRead>(&self, reader: R) -> Result<CsvSource<R>, DataError>

Stream rows from reader instead of reading the text whole, so an input larger than memory can be read (a profile samples it).

The dialect and header are decided from the first SAMPLE_CHARS characters, as read decides them, and records are parsed one at a time: a quoted field may span lines, up to MAX_RECORD bytes. The columns are decided from the rows the sniffer read, as read decides them from every row (a row longer than the header adds columns named by position); a later row is padded with nulls or cut to fit, and longer_rows counts the cut ones. Bytes that are not UTF-8 are replaced.

use rich_data::csv::CsvReader;
use rich_data::{RowSource, Value};

let text = "name,note\nweb,\"two\nlines\"\napi,ok\n";
let mut source = CsvReader::new().source(text.as_bytes()).unwrap();
assert_eq!(source.columns(), ["name", "note"]);
let first = source.next_row().unwrap().unwrap();
assert_eq!(first[1], Value::from("two\nlines"));
source.next_row().unwrap().unwrap();
assert!(source.next_row().is_none());

Trait Implementations§

Source§

impl Clone for CsvReader

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for CsvReader

Source§

impl Debug for CsvReader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CsvReader

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.