Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim the first row in case we don't have headers fixes #237 #236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vladcebo
Copy link

When user indicates that no headers are available, then the trim doesn't work very well for the first row

#[derive(serde::Deserialize, Debug)]
struct TestStruct {
    col1: String,
    col2: String,
    col3: String,
}

fn test() {
    let mut csv = String::from("a1, b1, c1\n");
    csv.push_str("a2, b2, c2\n");
    csv.push_str("a3, b3, c3\n");


    let mut csv_reader = ReaderBuilder::new()
    .trim(Trim::All)
    .has_headers(false)
    .flexible(true)
    .from_reader(BufReader::new(csv.as_bytes()));

    // Read as byte records, that should improve the performance without a lot of reallocations
    let mut raw_record = csv::ByteRecord::new();
    let headers = csv::ByteRecord::from(vec!["col1", "col2", "col3"]);

    while csv_reader.read_byte_record(&mut raw_record).unwrap() {
        let record = raw_record.deserialize::<TestStruct>(Some(&headers));
        println!("{:?}", record);
    }
}

Note that the first row is not trimmed properly.

Ok(TestStruct { col1: "a1", col2: " b1", col3: " c1" })
Ok(TestStruct { col1: "a2", col2: "b2", col3: "c2" })
Ok(TestStruct { col1: "a3", col2: "b3", col3: "c3" })

With the fix we should trim the fields like this:

Ok(TestStruct { col1: "a1", col2: "b1", col3: "c1" })
Ok(TestStruct { col1: "a2", col2: "b2", col3: "c2" })
Ok(TestStruct { col1: "a3", col2: "b3", col3: "c3" })

@vladcebo vladcebo changed the title Trim the first row in case we don't have headers Trim the first row in case we don't have headers #236 Jul 23, 2021
@vladcebo vladcebo changed the title Trim the first row in case we don't have headers #236 Trim the first row in case we don't have headers fixes #236 Jul 23, 2021
@vladcebo vladcebo changed the title Trim the first row in case we don't have headers fixes #236 Trim the first row in case we don't have headers fixes #237 Jul 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant