3 releases

0.1.2 Aug 29, 2024
0.1.1 Jun 25, 2024
0.1.0 Jun 24, 2024

#671 in Text processing

Download history 29/week @ 2024-07-29 124/week @ 2024-08-26 23/week @ 2024-09-16 8/week @ 2024-09-23 7/week @ 2024-09-30 1/week @ 2024-10-07

228 downloads per month

MIT/Apache

22KB
483 lines

Just CSV reader/writer

Inspired by csv_simple crate.

Conforms the RFC 4180. Please be aware of that RFC. According to RFC CSV-file is ASCII-only, this crate treats CSV-files as UTF-8.

Reader API

impl<R: BufRead> reader::CsvReader<R> {
	pub fn new(reader: R) -> Self {...}
	pub fn with_config(source: R, config: Config) -> Self {...}
	pub fn headers(&self) -> Option<&[String]> {...}
}

impl<R: BufRead> Iterator for reader::CsvReader<R> {
	type Item = crate::Result<Box<[String]>>;
	...
}

struct Config {
  pub has_headers: bool,
  pub separator: char,
  pub escape: char,
}

Config struct exported as CsvReaderConfig in lib.rs implements builder pattern and Default trait.

CsvReader::headers(...) returns Some if config.has_headers == true and parsing first record was successful. If you passed config.has_headers == true but get None from CsvReader::headers(...) means parsing first record failed. Note that iterating through such a reader is undefined behaviour.

Writer API

Is not as convinient as Reader API. It just helps you to escape special characters while writing to the dest.

Headers have no special treatment, they are just another row. Only if you try to write them after records you get an error.

impl<W: Write> CsvWriter<W> {
	pub fn new(dest: W) -> Self {...}
	pub fn with_config(dest: W, config: CsvWriterConfig) -> Self {...}
	pub fn write_row(&mut self, row: &[impl AsRef<str>]) -> crate::Result<()> {...}
	pub fn headers(&mut self, headers: &[impl AsRef<str>]) -> crate::Result<()> {...}
	pub fn write_doc(&mut self, doc: &[&[impl AsRef<str>]]) -> crate::Result<()> {...}
}

// as CsvWriterConfig
pub struct Config {
	pub separator: char,
  	pub escape: char,
  	pub newline: NewLine,
}

pub enum NewLine {
  	Rfc,
  	Unix,
  	Custom(String),
}

Config struct exported as CsvWriterConfig in lib.rs implements builder pattern and Default trait.

License

Due to nom the crate has MIT License.

Dependencies

~1.1–1.8MB
~36K SLoC