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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! CSV source configuration.
use faucet_core::DEFAULT_BATCH_SIZE;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
/// Configuration for the CSV file source.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct CsvSourceConfig {
/// Path to the CSV file.
pub path: String,
/// Whether the file has a header row. Defaults to `true`.
#[serde(default = "default_true")]
pub has_headers: bool,
/// Field delimiter byte. Defaults to `b','`.
#[serde(default = "default_delimiter")]
pub delimiter: u8,
/// Quote character byte. Defaults to `b'"'`.
#[serde(default = "default_quote")]
pub quote: u8,
/// Whether to tolerate rows with a field count that differs from the
/// header (or from the first data row when header-less). Defaults to
/// `false` (strict).
///
/// When `false` (the default), a row whose field count does not match the
/// expected width is a structural defect and aborts the run with a typed
/// [`FaucetError::Source`](faucet_core::FaucetError::Source) naming the
/// offending line — silently emitting an incomplete record would corrupt
/// downstream consumers. Set to `true` only when ragged rows are expected
/// and acceptable (short rows yield records missing the trailing columns;
/// long rows gain `column_N` keys).
#[serde(default)]
pub flexible: bool,
/// Records per emitted [`StreamPage`](faucet_core::StreamPage). Rows are
/// parsed line-by-line from a tokio `BufReader` and yielded whenever the
/// buffer reaches this size. Defaults to [`DEFAULT_BATCH_SIZE`].
///
/// `batch_size = 0` is the "no batching" sentinel: the file is fully
/// drained and the entire result set is emitted in a single page. Useful
/// for small lookup tables or for sinks (e.g. SQL `COPY`, BigQuery load
/// jobs) that prefer one large request to many small ones.
#[serde(default = "default_batch_size")]
pub batch_size: usize,
/// Compression codec for the input file. Defaults to
/// [`CompressionConfig::Auto`](faucet_core::CompressionConfig::Auto) —
/// `.gz` / `.zst` suffix selects gzip / zstd. Requires the crate-local
/// `compression` feature.
#[cfg(feature = "compression")]
#[serde(default)]
pub compression: faucet_core::CompressionConfig,
}
fn default_true() -> bool {
true
}
fn default_delimiter() -> u8 {
b','
}
fn default_quote() -> u8 {
b'"'
}
fn default_batch_size() -> usize {
DEFAULT_BATCH_SIZE
}
impl CsvSourceConfig {
/// Create a new config with the required file path and sensible defaults.
pub fn new(path: impl Into<String>) -> Self {
Self {
path: path.into(),
has_headers: true,
delimiter: b',',
quote: b'"',
flexible: false,
batch_size: DEFAULT_BATCH_SIZE,
#[cfg(feature = "compression")]
compression: faucet_core::CompressionConfig::Auto,
}
}
/// Set whether the file has a header row.
pub fn has_headers(mut self, v: bool) -> Self {
self.has_headers = v;
self
}
/// Set the field delimiter byte.
pub fn delimiter(mut self, d: u8) -> Self {
self.delimiter = d;
self
}
/// Set the quote character byte.
pub fn quote(mut self, q: u8) -> Self {
self.quote = q;
self
}
/// Set whether to tolerate rows with an uneven field count.
///
/// Defaults to `false` (strict): a ragged row aborts the run. Pass `true`
/// to opt in to lenient parsing where short/long rows are accepted.
pub fn flexible(mut self, v: bool) -> Self {
self.flexible = v;
self
}
/// Set the per-page row count for [`Source::stream_pages`](faucet_core::Source::stream_pages).
///
/// Pass `0` to opt out of batching — the entire file is emitted in a
/// single [`StreamPage`](faucet_core::StreamPage).
pub fn with_batch_size(mut self, batch_size: usize) -> Self {
self.batch_size = batch_size;
self
}
/// Set the compression codec. Available only with the `compression` feature.
#[cfg(feature = "compression")]
pub fn compression(mut self, c: faucet_core::CompressionConfig) -> Self {
self.compression = c;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_config() {
let config = CsvSourceConfig::new("/tmp/data.csv");
assert_eq!(config.path, "/tmp/data.csv");
assert!(config.has_headers);
assert_eq!(config.delimiter, b',');
assert_eq!(config.quote, b'"');
}
#[test]
fn builder_methods() {
let config = CsvSourceConfig::new("/tmp/data.tsv")
.has_headers(false)
.delimiter(b'\t')
.quote(b'\'');
assert!(!config.has_headers);
assert_eq!(config.delimiter, b'\t');
assert_eq!(config.quote, b'\'');
}
#[test]
fn flexible_defaults_to_false_strict() {
let config = CsvSourceConfig::new("/tmp/data.csv");
assert!(!config.flexible);
}
#[test]
fn flexible_builder_and_serde_default() {
let config = CsvSourceConfig::new("/tmp/data.csv").flexible(true);
assert!(config.flexible);
// Absent in JSON => strict default.
let json = r#"{ "path": "/tmp/data.csv" }"#;
let parsed: CsvSourceConfig = serde_json::from_str(json).unwrap();
assert!(!parsed.flexible);
// Explicit value round-trips.
let json = r#"{ "path": "/tmp/data.csv", "flexible": true }"#;
let parsed: CsvSourceConfig = serde_json::from_str(json).unwrap();
assert!(parsed.flexible);
}
#[test]
fn batch_size_defaults_to_default_batch_size() {
let config = CsvSourceConfig::new("/tmp/data.csv");
assert_eq!(config.batch_size, faucet_core::DEFAULT_BATCH_SIZE);
}
#[test]
fn with_batch_size_overrides_default() {
let config = CsvSourceConfig::new("/tmp/data.csv").with_batch_size(500);
assert_eq!(config.batch_size, 500);
}
#[test]
fn batch_size_zero_is_accepted_as_no_batching_sentinel() {
let config = CsvSourceConfig::new("/tmp/data.csv").with_batch_size(0);
assert_eq!(config.batch_size, 0);
assert!(faucet_core::validate_batch_size(config.batch_size).is_ok());
}
#[test]
fn batch_size_above_max_is_rejected_by_validate_batch_size() {
let config =
CsvSourceConfig::new("/tmp/data.csv").with_batch_size(faucet_core::MAX_BATCH_SIZE + 1);
assert!(faucet_core::validate_batch_size(config.batch_size).is_err());
}
#[test]
fn batch_size_deserializes_from_json() {
let json = r#"{
"path": "/tmp/data.csv",
"batch_size": 250
}"#;
let config: CsvSourceConfig = serde_json::from_str(json).unwrap();
assert_eq!(config.batch_size, 250);
}
}