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
use {
crate::nested_csv::{read::CsvReaderEnableNestedExt, write::CsvWriterEnableNestedExt},
anyhow::{Context, Result},
serde::{Deserialize, Serialize, de::DeserializeOwned},
std::io::{Read, Seek},
tap::Pipe,
tracing::info,
};
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Child {
field_1: bool,
field_2: i32,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Parent {
child_1: Child,
child_2: Child,
}
fn back_and_forth<'a, T>(mut data: impl Iterator<Item = &'a T> + 'a) -> Result<()>
where
T: Serialize + DeserializeOwned + Send + std::fmt::Debug + 'a,
{
csv::WriterBuilder::new()
.from_writer(std::io::Cursor::new(Vec::new()))
.pipe(|mut w| {
data.try_for_each(|p| {
info!(?p);
w.serialize(p)
})
.context("serializing")
.and_then(|()| w.into_inner().context("dropping writer"))
})
.and_then(|mut buffer| {
buffer
.rewind()
.context("rewinding")
.map(|()| buffer)
.and_then(|mut buffer| {
csv::ReaderBuilder::new()
.has_headers(true)
.from_reader(&mut buffer)
.pipe(Ok)
.and_then(|mut reader| {
reader
.deserialize::<T>()
.map(|r| r.context("deserializing"))
.collect::<Result<Vec<_>, _>>()
.map(|values| info!("OK!\n{values:#?}"))
.with_context(|| {
format!(
"deserializing contents of buffer:\n{}",
reader
.into_inner()
.pipe(|buffer| {
buffer.rewind().context("rewinding").and_then(
|_| {
String::new().pipe(|mut v| {
buffer
.read_to_string(&mut v)
.context("reading to string")
.map(|_| v)
})
},
)
})
.unwrap_or_else(|e| format!("{e:?}"))
)
})
})
})
})
}
fn back_and_forth_nesting_enabled<'a, T>(mut data: impl Iterator<Item = &'a T> + 'a) -> Result<()>
where
T: Serialize + DeserializeOwned + Send + std::fmt::Debug + 'a,
{
csv::WriterBuilder::new()
.from_writer(std::io::Cursor::new(Vec::new()))
.enable_nested()
.pipe(|mut w| {
data.try_for_each(|p| {
info!(?p);
w.serialize(p)
})
.context("serializing")
.and_then(|()| w.into_inner().context("dropping writer"))
})
.and_then(|mut buffer| {
buffer
.rewind()
.context("rewinding")
.map(|()| buffer)
.and_then(|mut buffer| {
csv::ReaderBuilder::new()
.has_headers(true)
.from_reader(&mut buffer)
.enable_nested::<T>()
.context("enabling nesting")
.and_then(|mut reader| {
reader
.deserialize()
.map(|r| r.context("deserializing"))
.collect::<Result<Vec<_>, _>>()
.map(|values| info!("OK!\n{values:#?}"))
.with_context(|| {
format!(
"deserializing contents of buffer:\n{}",
reader
.into_inner()
.pipe(|buffer| {
buffer.rewind().context("rewinding").and_then(
|_| {
String::new().pipe(|mut v| {
buffer
.read_to_string(&mut v)
.context("reading to string")
.map(|_| v)
})
},
)
})
.unwrap_or_else(|e| format!("{e:?}"))
)
})
})
})
})
}
const PARENT: Parent = Parent {
child_1: Child {
field_1: true,
field_2: 0,
},
child_2: Child {
field_1: false,
field_2: 1,
},
};
const DATA: &[Parent] = &[PARENT, PARENT, PARENT];
#[test]
#[should_panic(
expected = "cannot serialize Child container inside struct when writing headers from structs"
)]
fn test_normal_data_fails() {
back_and_forth(DATA.iter()).unwrap()
}
#[test_log::test]
fn test_flattening_fixes_the_problem() {
back_and_forth_nesting_enabled(DATA.iter()).expect("going back and forth with nesting enabled")
}