#csv #separated #comma #delimited #rfc4180

csvrow

Fast and simple crate for taking a string slice and iterating over the fields in a manner that adheres to RFC-4180

3 unstable releases

0.2.1 Apr 30, 2024
0.1.1 Mar 18, 2024
0.1.0 Mar 18, 2024

#2090 in Parser implementations

Unlicense/MIT

12KB
274 lines

csvrow

=== A small, fast utility library that allows you to wrap a string slice representing a line from a CSV file and iterate over its fields. Complies with RFC 4180 (CSV format) and UTF8.

Usage

Add csvrow to your Cargo.toml file directly, or alternatively type cargo add csvrow at a terminal prompt in your project root.

Example

Creating a CSV Row from a String slice and collecting the results into a Vec:

use CsvRow::*;

fn get_fields() {

    let row = "rust,is,awesome";
    let csv = CsvRow::new(row, ',', false);
    let vec_t: Vec<_> = csv.collect();
}

Fields wrapped in quotes, or containing escaped quotes (in accordance with RFC 4180) will by default be parsed and un-escaped. This behavior can be overridden with the 'literal' parameter of CsvRow::new

use CsvRow::*;

fn get_fields() {

    let row = r#""rust",is,"Awesome ""bring me the"" Sauce""#;
    let csv = CsvRow::new(row, ',', false);
    let vec_t: Vec<_> = csv.collect();

    // Will yield:
    // rust
    // is
    // Awesome "bring me the" Sauce

    let row = r#""rust",is,"Awesome ""bring me the"" Sauce""#;
    let csv = CsvRow::new(row, ',', true);
    let vec_t: Vec<_> = csv.collect();

    // Will yield:
    // "rust"
    // is
    // "Awesome ""bring me the"" Sauce"
}

License: Unlicense/MIT

No runtime deps