csv_core

Struct Writer

Source
pub struct Writer { /* private fields */ }
Expand description

A writer for CSV data.

§RFC 4180

This writer conforms to RFC 4180 with one exception: it doesn’t guarantee that all records written are of the same length. Instead, the onus is on the caller to ensure that all records written are of the same length.

Note that the default configuration of a Writer uses \n for record terminators instead of \r\n as specified by RFC 4180. Use the terminator method on WriterBuilder to set the terminator to \r\n if it’s desired.

Implementations§

Source§

impl Writer

Source

pub fn new() -> Writer

Creates a new CSV writer with the default configuration.

Source

pub fn finish(&mut self, output: &mut [u8]) -> (WriteResult, usize)

Finish writing CSV data to output.

This must be called when one is done writing CSV data to output. In particular, it will write closing quotes if necessary.

Source

pub fn field( &mut self, input: &[u8], output: &mut [u8], ) -> (WriteResult, usize, usize)

Write a single CSV field from input to output while employing this writer’s quoting style.

This returns the result of writing field data, in addition to the number of bytes consumed from input and the number of bytes written to output.

The result of writing field data is either WriteResult::InputEmpty or WriteResult::OutputFull. The former occurs when all bytes in input were copied to output, while the latter occurs when output is too small to fit everything from input. The maximum number of bytes that can be written to output is 2 + (2 * input.len()) because of quoting. (The worst case is a field consisting entirely of quotes.)

Multiple successive calls to field will write more data to the same field. Subsequent fields can be written by calling either delimiter or terminator first.

If this writer’s quoting style is QuoteStyle::Necessary, then input should contain the entire field. Otherwise, whether the field needs to be quoted or not cannot be determined.

Source

pub fn delimiter(&mut self, output: &mut [u8]) -> (WriteResult, usize)

Write the configured field delimiter to output.

If the output buffer does not have enough room to fit a field delimiter, then nothing is written to output and WriteResult::OutputFull is returned. Otherwise, WriteResult::InputEmpty is returned along with the number of bytes written to output (which is 1 in case of an unquoted field, or 2 in case of an end quote and a field separator).

Source

pub fn terminator(&mut self, output: &mut [u8]) -> (WriteResult, usize)

Write the configured record terminator to output.

If the output buffer does not have enough room to fit a record terminator, then no part of the terminator is written and WriteResult::OutputFull is returned. Otherwise, WriteResult::InputEmpty is returned along with the number of bytes written to output (which is always 1 or 2).

Source

pub fn is_special_byte(&self, b: u8) -> bool

Returns true if and only if the given byte corresponds to a special byte in this CSV writer’s configuration.

Note that this does not take into account this writer’s quoting style.

Source

pub fn should_quote(&self, input: &[u8]) -> bool

Returns true if and only if we should put the given field data in quotes. This takes the quoting style into account.

Source

pub fn get_delimiter(&self) -> u8

Return the delimiter used for this writer.

Source

pub fn get_terminator(&self) -> Terminator

Return the terminator used for this writer.

Source

pub fn get_quote_style(&self) -> QuoteStyle

Return the quoting style used for this writer.

Source

pub fn get_quote(&self) -> u8

Return the quote character used for this writer.

Source

pub fn get_escape(&self) -> u8

Return the escape character used for this writer.

Source

pub fn get_double_quote(&self) -> bool

Return whether this writer doubles quotes or not. When the writer does not double quotes, it will escape them using the escape character.

Trait Implementations§

Source§

impl Clone for Writer

Source§

fn clone(&self) -> Writer

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Writer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Writer

Source§

fn default() -> Writer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Writer

§

impl RefUnwindSafe for Writer

§

impl Send for Writer

§

impl Sync for Writer

§

impl Unpin for Writer

§

impl UnwindSafe for Writer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.