-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathcsv.rs
More file actions
79 lines (66 loc) · 1.72 KB
/
csv.rs
File metadata and controls
79 lines (66 loc) · 1.72 KB
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
use std::fmt::Debug;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
#[derive(Clone, Debug, Deserialize, Serialize, ToSchema)]
#[serde(default)]
pub struct CsvParserConfig {
/// Field delimiter (default `','`).
///
/// This must be an ASCII character.
pub delimiter: char,
/// Whether the input begins with a header line (which is ignored).
pub headers: bool,
}
impl CsvParserConfig {
pub fn delimiter(&self) -> CsvDelimiter {
self.delimiter.into()
}
}
impl Default for CsvParserConfig {
fn default() -> Self {
Self {
delimiter: CsvDelimiter::default().0.into(),
headers: false,
}
}
}
/// A delimiter between CSV records, typically `b','`.
#[derive(Copy, Clone)]
pub struct CsvDelimiter(pub u8);
impl CsvDelimiter {
pub const DEFAULT: CsvDelimiter = CsvDelimiter(b',');
}
impl Default for CsvDelimiter {
fn default() -> Self {
Self::DEFAULT
}
}
impl Debug for CsvDelimiter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("CsvDelimiter")
.field(&char::from(self.0))
.finish()
}
}
impl From<char> for CsvDelimiter {
fn from(value: char) -> Self {
Self(value.try_into().unwrap_or(b','))
}
}
#[derive(Debug, Deserialize, Serialize, ToSchema)]
#[serde(default)]
pub struct CsvEncoderConfig {
/// Field delimiter (default `','`).
///
/// This must be an ASCII character.
pub delimiter: char,
pub buffer_size_records: usize,
}
impl Default for CsvEncoderConfig {
fn default() -> Self {
Self {
delimiter: CsvDelimiter::default().0.into(),
buffer_size_records: 10_000,
}
}
}