1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
mod error;
mod iter;
mod serializer;
#[cfg(feature = "stream")]
mod stream;
mod writer;
pub use error::{Error, ErrorKind, Result};
pub use iter::Iter;
#[cfg(feature = "stream")]
pub use stream::Stream;
pub use writer::{Writer, WriterBuilder};
/// The quoting style to use when writing CSV data.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum QuoteStyle {
/// This puts quotes around every field. Always.
Always,
/// This puts quotes around fields only when necessary.
///
/// They are necessary when fields contain a quote, delimiter or record
/// terminator. Quotes are also necessary when writing an empty record
/// (which is indistinguishable from a record with one empty field).
///
/// This is the default.
Necessary,
/// This puts quotes around all fields that are non-numeric. Namely, when
/// writing a field that does not parse as a valid float or integer, then
/// quotes will be used even if they aren't strictly necessary.
NonNumeric,
/// This *never* writes quotes, even if it would produce invalid CSV data.
Never,
}
impl QuoteStyle {
fn to_core(self) -> csv_core::QuoteStyle {
match self {
QuoteStyle::Always => csv_core::QuoteStyle::Always,
QuoteStyle::Necessary => csv_core::QuoteStyle::Necessary,
QuoteStyle::NonNumeric => csv_core::QuoteStyle::NonNumeric,
QuoteStyle::Never => csv_core::QuoteStyle::Never,
}
}
}
impl Default for QuoteStyle {
fn default() -> QuoteStyle {
QuoteStyle::Necessary
}
}
/// A record terminator.
///
/// Use this to specify the record terminator while parsing CSV. The default is
/// CRLF, which treats `\r`, `\n` or `\r\n` as a single record terminator.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum Terminator {
/// Parses `\r`, `\n` or `\r\n` as a single record terminator.
CRLF,
/// Parses the byte given as a record terminator.
Any(u8),
}
impl Terminator {
/// Convert this to the csv_core type of the same name.
fn to_core(self) -> csv_core::Terminator {
match self {
Terminator::CRLF => csv_core::Terminator::CRLF,
Terminator::Any(b) => csv_core::Terminator::Any(b),
}
}
}
impl Default for Terminator {
fn default() -> Terminator {
Terminator::CRLF
}
}
/// The whitespace preservation behaviour when reading CSV data.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum Trim {
/// Preserves fields and headers. This is the default.
None,
/// Trim whitespace from headers.
Headers,
/// Trim whitespace from fields, but not headers.
Fields,
/// Trim whitespace from fields and headers.
All,
}
impl Default for Trim {
fn default() -> Trim {
Trim::None
}
}