Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion csv-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Terminator {
}

/// The quoting style to use when writing CSV data.
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[non_exhaustive]
pub enum QuoteStyle {
/// This puts quotes around every field. Always.
Expand Down
27 changes: 25 additions & 2 deletions csv-core/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ impl Writer {
/// In particular, it will write closing quotes if necessary.
pub fn finish(&mut self, mut output: &mut [u8]) -> (WriteResult, usize) {
let mut nout = 0;
if self.state.record_bytes == 0 && self.state.in_field {
if self.state.record_bytes == 0
&& self.state.in_field
&& self.style != QuoteStyle::Never
{
assert!(!self.state.quoting);
let (res, o) = self.write(&[self.quote, self.quote], output);
if o == 0 {
Expand Down Expand Up @@ -368,7 +371,7 @@ impl Writer {
mut output: &mut [u8],
) -> (WriteResult, usize) {
let mut nout = 0;
if self.state.record_bytes == 0 {
if self.state.record_bytes == 0 && self.style != QuoteStyle::Never {
assert!(!self.state.quoting);
let (res, o) = self.write(&[self.quote, self.quote], output);
if o == 0 {
Expand Down Expand Up @@ -1075,4 +1078,24 @@ mod tests {
);
assert_write!(wtr, finish, &mut out[..], 1, InputEmpty, "\"");
}

#[test]
fn never_quote_empty_field_terminator() {
let mut wtr =
WriterBuilder::new().quote_style(QuoteStyle::Never).build();
let out = &mut [0; 1024];

assert_field!(wtr, b(""), &mut out[..], 0, 0, InputEmpty, "");
assert_write!(wtr, terminator, &mut out[..], 1, InputEmpty, "\n");
}

#[test]
fn never_quote_empty_field_finish() {
let mut wtr =
WriterBuilder::new().quote_style(QuoteStyle::Never).build();
let out = &mut [0; 1024];

assert_field!(wtr, b(""), &mut out[..], 0, 0, InputEmpty, "");
assert_write!(wtr, finish, &mut out[..], 0, InputEmpty, "");
}
}
12 changes: 12 additions & 0 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,7 @@ mod tests {
};

use super::{Writer, WriterBuilder};
use crate::QuoteStyle;

fn wtr_as_string(wtr: Writer<Vec<u8>>) -> String {
String::from_utf8(wtr.into_inner().unwrap()).unwrap()
Expand Down Expand Up @@ -1450,4 +1451,15 @@ mod tests {
let buf = wtr.into_inner().unwrap();
assert_eq!(String::from_utf8(buf).unwrap(), "\"# comment\",another\n");
}

#[test]
fn never_quote_empty_field() {
let mut wtr = WriterBuilder::new()
.quote_style(QuoteStyle::Never)
.from_writer(vec![]);
wtr.write_record(["foo"]).unwrap();
wtr.write_record([""]).unwrap();
wtr.write_record(["bar,baz"]).unwrap();
assert_eq!(wtr_as_string(wtr), "foo\n\nbar,baz\n");
}
}
Loading