This is a CSV (delimiter can be changed) reader with a focus on: 1. Simplicity 2. Robustness 3. Performance (to a lesser extent)
It follows RFC 4180, but allows for non-conformant files to be processed. In order to accomplish this, it makes the following assumptions:
1,2,3,
is parsed as ["1","2","3",""]
1,2",3
is parsed as ["1","2\"","3"]
1,2,"3"123
is parsed as ["1","2","3123"]
1,2,"3*EOF*
is parsed as ["1","2","3"]
[""]
String::from_utf8_lossy
function.\r
in unquoted fields is always discardedAdd to your Cargo.toml:
[dependencies]
simple_csv = "~0.0.8"
```rust let teststring = "1,2,3\r\n4,5,6".tostring(); let bytes = teststring.intobytes(); let mut testcsvreader = bytes.as_slice();
let mut reader = SimpleCsvReader::new(testcsvreader);
asserteq!(reader.nextrow(), Ok(vec!["1".tostring(),"2".tostring(),"3".tostring()].asslice())); asserteq!(reader.nextrow(), Ok(vec!["4".tostring(),"5".tostring(),"6".tostring()].asslice())); assert!(reader.nextrow().iserr()); ```
```rust let teststring = "1|2|3\r\n4|5|6".tostring(); let bytes = teststring.intobytes(); let testcsvreader = bytes.as_slice();
let mut reader = SimpleCsvReader::withdelimiter(testcsv_reader,'|');
asserteq!(reader.nextrow(), Ok(vec!["1".tostring(),"2".tostring(),"3".tostring()].asslice())); asserteq!(reader.nextrow(), Ok(vec!["4".tostring(),"5".tostring(),"6".tostring()].asslice())); assert!(reader.nextrow().iserr()); ```
```rust let teststring = "1|2|3\r\n4|5|6".tostring(); let bytes = teststring.intobytes(); let testcsvreader = bytes.as_slice();
let mut reader = SimpleCsvReader::withdelimiter(testcsv_reader,'|');
for row in reader { println!("{}",row); } ```
```rust let teststring = "1,#2#,3\r\n#4#,5,6".tostring(); let bytes = teststring.intobytes(); let testcsvreader = bytes.as_slice();
let mut reader = SimpleCsvReader::withcustomchars(testcsvreader, ',', '#', '\n');
asserteq!(reader.nextrow(), Ok(vec!["1".tostring(),"2".tostring(),"3".tostring()].asslice())); asserteq!(reader.nextrow(), Ok(vec!["4".tostring(),"5".tostring(),"6".tostring()].asslice())); assert!(reader.nextrow().iserr()); ```