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

added NONE Terminator #332

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions csv-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ pub enum Terminator {
CRLF,
/// Parses the byte given as a record terminator.
Any(u8),
/// Disable line terminator
/// This is useful when writing single line field by field with proper qoting
NONE,
/// Hints that destructuring should not be exhaustive.
///
/// This enum may grow additional variants, so this makes sure clients
Expand Down
4 changes: 4 additions & 0 deletions csv-core/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ impl WriterBuilder {
Any(b) => {
wtr.requires_quotes[b as usize] = true;
}
NONE => {
wtr.requires_quotes[usize::MIN] = true;
}
_ => unreachable!(),
}
wtr
Expand Down Expand Up @@ -375,6 +378,7 @@ impl Writer {
let (res, o) = match self.term {
Terminator::CRLF => write_pessimistic(&[b'\r', b'\n'], output),
Terminator::Any(b) => write_pessimistic(&[b], output),
Terminator::NONE => write_pessimistic(&[], output),
_ => unreachable!(),
};
if o == 0 {
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ pub enum Terminator {
CRLF,
/// Parses the byte given as a record terminator.
Any(u8),
/// Disable line terminator
/// This is useful when writing single line field by field with proper qoting
NONE,
/// Hints that destructuring should not be exhaustive.
///
/// This enum may grow additional variants, so this makes sure clients
Expand All @@ -236,6 +239,7 @@ impl Terminator {
match self {
Terminator::CRLF => csv_core::Terminator::CRLF,
Terminator::Any(b) => csv_core::Terminator::Any(b),
Terminator::NONE => csv_core::Terminator::NONE,
_ => unreachable!(),
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ impl<W: io::Write> Writer<W> {
self.buf.writable()[0] = b;
self.buf.written(1);
}
csv_core::Terminator::NONE => {}
_ => unreachable!(),
}
self.state.fields_written = 0;
Expand Down Expand Up @@ -1414,4 +1415,14 @@ mod tests {
wtr.serialize((true, 1.3, "hi")).unwrap();
assert_eq!(wtr_as_string(wtr), "true,1.3,hi\n");
}

#[test]
fn quoting_witout_terminator_char() {
let mut wtr = WriterBuilder::new()
.terminator(crate::Terminator::NONE)
.from_writer(vec![]);

wtr.write_record(&["foo \" bar"]).unwrap();
assert_eq!(wtr_as_string(wtr), "\"foo \"\" bar\"");
}
}