Simple CSV Library

Build status

This is a CSV (delimiter can be changed) parser & writer with a focus on: 1. Simplicity 2. Robustness 3. Performance (to a lesser extent)

Parser

The parser follows RFC 4180, but allows for non-conformant files to be processed.

In order to achieve this robustness, the parser makes the following assumptions:

  1. Commas on the end of a line results in a empty string for that column.
  2. Double quotes in a field that is not enclosed in double quotes are processed as a regular character and are included in the column string.
  3. Non-delimiter characters immediately following a quoted field are treated as part of the column data and are appended to the column string.
  4. An EOF in the middle of a quoted field is parsed as if the field was properly closed.
  5. There is no error for empty lines or varying number of columns per line.
  6. Lines are assumed to be UTF8 and are decoded "lossily" via Rust's String::from_utf8_lossy function.
  7. The return character \r in unquoted fields is always discarded.

Writer

The writer always produces RFC 4180 compliant output and can write to any object that implements the std::io::Writer trait.

Usage

Add to your Cargo.toml:

[dependencies] simple_csv = "~0.0.8"

Simple CSV Parsing usage

```rust let teststring = "1,2,3\r\n4,5,6".tostring(); let bytes = teststring.intobytes(); let testcsvreader = &*bytes;

let mut reader = SimpleCsvReader::new(testcsvreader);

asserteq!(reader.nextrow(), Ok(&vec!["1".to_string(),"2".to_string(),"3".to_string()])); assert_eq!(reader.next_row(), Ok(&vec!["4".tostring(),"5".tostring(),"6".tostring()])); assert!(reader.nextrow().is_err()); ```

Different Delimiter

```rust let teststring = "1|2|3\r\n4|5|6".tostring(); let bytes = teststring.intobytes(); let testcsvreader = &*bytes; let mut csvoptions: SimpleCsvReaderOptions = Default::default(); csvoptions.delimiter = '|'; let mut reader = SimpleCsvReader::withoptions(testcsvreader,csvoptions);

asserteq!(reader.nextrow(), Ok(&vec!["1".to_string(),"2".to_string(),"3".to_string()])); assert_eq!(reader.next_row(), Ok(&vec!["4".tostring(),"5".tostring(),"6".tostring()])); assert!(reader.nextrow().is_err()); ```

Using a iterator

```rust let teststring = "1,2,3\r\n4,5,6".tostring(); let bytes = teststring.intobytes(); let testcsvreader = &*bytes;

let mut reader = SimpleCsvReader::new(testcsvreader);

for row in reader { println!("{}",row); } ```

Different Text Enclosing Character

```rust let teststring = "1,#2#,3\r\n#4#,5,6".tostring(); let bytes = teststring.intobytes(); let testcsvreader = &*bytes; let mut csvoptions: SimpleCsvReaderOptions = Default::default(); csvoptions.textenclosure = '#'; let mut reader = SimpleCsvReader::withoptions(testcsvreader,csv_options);

asserteq!(reader.nextrow(), Ok(&vec!["1".to_string(),"2".to_string(),"3".to_string()])); assert_eq!(reader.next_row(), Ok(&vec!["4".tostring(),"5".tostring(),"6".tostring()])); assert!(reader.nextrow().is_err()); ```

Simple CSV Writing Usage

```rust let mut vec = Vec::new(); let mut writer = SimpleCsvWriter::new(vec); let _ = writer.writeall(&vec![ vec!["1".tostring(),"2".tostring(),"3".tostring()], vec!["4".tostring(),"5".tostring(),"6".tostring()]]); vec = writer.asinner();

let teststring = "1,2,3\n4,5,6"; asserteq!(vec, teststring.asbytes()); ```

To Do