Skip to content

Commit 697e558

Browse files
committed
switched to defaults by serde
1 parent eb45586 commit 697e558

File tree

1 file changed

+31
-13
lines changed
  • lib/codecs/src/encoding/format

1 file changed

+31
-13
lines changed

lib/codecs/src/encoding/format/csv.rs

+31-13
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,38 @@ pub enum QuoteStyle {
7878
Never,
7979
}
8080

81+
const fn default_delimiter() -> u8 {
82+
b','
83+
}
84+
85+
const fn default_escape() -> u8 {
86+
b','
87+
}
88+
89+
const fn default_double_quote() -> bool {
90+
true
91+
}
92+
8193
/// Config used to build a `CsvSerializer`.
8294
#[crate::configurable_component]
8395
#[derive(Debug, Clone)]
8496
pub struct CsvSerializerOptions {
8597
/// The field delimiter to use when writing CSV.
98+
#[serde(
99+
default = "default_delimiter",
100+
with = "vector_core::serde::ascii_char",
101+
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
102+
)]
86103
pub delimiter: u8,
87104

88105
/// Enable double quote escapes.
89106
///
90107
/// This is enabled by default, but it may be disabled. When disabled, quotes in
91108
/// field data are escaped instead of doubled.
109+
#[serde(
110+
default = "default_double_quote",
111+
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
112+
)]
92113
pub double_quote: bool,
93114

94115
/// The escape character to use when writing CSV.
@@ -97,9 +118,18 @@ pub struct CsvSerializerOptions {
97118
/// like \ (instead of escaping quotes by doubling them).
98119
///
99120
/// To use this `double_quotes` needs to be disabled as well otherwise it is ignored
121+
#[serde(
122+
default = "default_escape",
123+
with = "vector_core::serde::ascii_char",
124+
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
125+
)]
100126
pub escape: u8,
101127

102128
/// The quoting style to use when writing CSV data.
129+
#[serde(
130+
default,
131+
skip_serializing_if = "vector_core::serde::skip_serializing_if_default"
132+
)]
103133
pub quote_style: QuoteStyle,
104134

105135
/// Configures the fields that will be encoded, as well as the order in which they
@@ -112,20 +142,8 @@ pub struct CsvSerializerOptions {
112142
pub fields: Vec<ConfigTargetPath>,
113143
}
114144

115-
impl Default for CsvSerializerOptions {
116-
fn default() -> CsvSerializerOptions {
117-
CsvSerializerOptions {
118-
delimiter: b',',
119-
double_quote: true,
120-
escape: b'"',
121-
quote_style: QuoteStyle::Necessary,
122-
fields: vec![],
123-
}
124-
}
125-
}
126-
127145
impl CsvSerializerOptions {
128-
const fn csv_quote_style(&self) -> csv::QuoteStyle {
146+
fn csv_quote_style(&self) -> csv::QuoteStyle {
129147
match self.quote_style {
130148
QuoteStyle::Always => csv::QuoteStyle::Always,
131149
QuoteStyle::NonNumeric => csv::QuoteStyle::NonNumeric,

0 commit comments

Comments
 (0)