csv_stream/
lib.rs

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