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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use super::*;
pub struct Headers(Vec<String>);
pub struct Data {
cols: Headers,
rows: Vec<Vec<Cell>>,
}
pub enum Cell {
Num(f64),
Txt(String),
}
macro_rules! cell_impl {
($([$t:ty : $v:ident $f:path])*) => {
$(
impl From<$t> for Cell {
fn from(x: $t) -> Cell {
Cell::$v($f(x))
}
}
)*
}
}
cell_impl! {
[f64:Num std::convert::identity]
[f32:Num From::from]
[String:Txt std::convert::identity]
[&str:Txt ToString::to_string]
}
impl Headers {
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn find(&self, s: &str) -> Option<usize> {
self.find_match(|x| x.eq(s))
}
pub fn find_ignore_case(&self, s: &str) -> Option<usize> {
self.find_match(|x| x.eq_ignore_ascii_case(s))
}
pub fn find_ignore_case_and_ws(&self, s: &str) -> Option<usize> {
self.find_match(|a| str_eq_ignore_case_and_ws(a, s))
}
pub fn find_match<P>(&self, predicate: P) -> Option<usize>
where
P: Fn(&str) -> bool,
{
self.0
.iter()
.enumerate()
.find_map(|(i, x)| predicate(x).then_some(i))
}
pub fn fuzzy_match(&self, s: &str) -> impl Iterator<Item = String> + '_ {
let mut eng = simsearch::SimSearch::new();
self.0
.iter()
.enumerate()
.for_each(|(i, x)| eng.insert(i, x));
let r = eng.search(s);
r.into_iter().filter_map(|i| self.0.get(i)).map(|x| {
let mut x = x.clone();
x.retain(|x| !x.is_whitespace());
x
})
}
}
impl<T: AsRef<str>> FromIterator<T> for Headers {
fn from_iter<I: IntoIterator<Item = T>>(i: I) -> Self {
Headers(
i.into_iter()
.map(|t| t.as_ref().trim().to_string())
.collect(),
)
}
}
impl Data {
pub fn new<D, R, C>(headers: Headers, data: D) -> Result<Self>
where
D: IntoIterator<Item = R>,
R: IntoIterator<Item = C>,
C: Into<Cell>,
{
let headers_len = headers.len();
let d = data.into_iter();
let (l, u) = d.size_hint();
let mut rows = Vec::with_capacity(u.unwrap_or(l));
for (i, row) in d.into_iter().enumerate() {
let row = row.into_iter().map(Into::into).collect::<Vec<_>>();
ensure!(
headers_len == row.len(),
"row index {} does not have the same length as the headers",
i
);
rows.push(row);
}
Ok(Self {
cols: headers,
rows,
})
}
pub fn len(&self) -> usize {
self.rows.len()
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
pub fn headers(&self) -> &Headers {
&self.cols
}
pub fn rows(&self) -> impl ExactSizeIterator<Item = DataRow> {
self.rows.iter().enumerate().map(|(idx, vals)| DataRow {
idx,
vals,
hdrs: &self.cols,
})
}
}
#[derive(Copy, Clone)]
pub struct DataRow<'a> {
idx: usize,
vals: &'a [Cell],
hdrs: &'a Headers,
}
impl<'a> DataRow<'a> {
pub fn get_num(&self, colidx: usize) -> Option<Result<f64>> {
self.vals.get(colidx).map(|c| match c {
Cell::Num(x) => Ok(*x),
Cell::Txt(x) => Err(miette!("failed to parse '{}' as number", x))
.wrap_err_with(|| format!("in column index {colidx}"))
.wrap_err_with(|| format!("in row index {}", self.idx + 1)),
})
}
pub fn idx(&self) -> usize {
self.idx
}
pub fn headers(&self) -> &Headers {
self.hdrs
}
}
pub struct CsvReader {
rdr: csv::Reader<Box<dyn std::io::Read>>,
cols: Option<Headers>,
}
impl CsvReader {
pub fn new<R: std::io::Read + 'static>(rdr: R) -> Self {
Self {
rdr: csv::Reader::from_reader(Box::new(rdr)),
cols: None,
}
}
fn read_headers(&mut self) -> Result<()> {
let hdrs = self
.rdr
.headers()
.into_diagnostic()
.wrap_err("failed to read CSV header row")?;
ensure!(!hdrs.is_empty(), "headers row is empty");
self.cols = Some(hdrs.iter().collect());
Ok(())
}
pub fn headers(&mut self) -> Result<&Headers> {
if self.cols.is_none() {
self.read_headers()?;
}
self.cols
.as_ref()
.map(Ok)
.expect("should be some if read_headers succeeds")
}
pub fn into_data(self) -> Result<Data> {
Data::try_from(self)
}
}
impl TryFrom<CsvReader> for Data {
type Error = miette::Report;
fn try_from(mut rdr: CsvReader) -> Result<Data> {
rdr.headers()?; let mut data = Vec::new();
for (i, row) in rdr.rdr.records().enumerate() {
let row = row
.into_diagnostic()
.wrap_err_with(|| format!("failed to read row {} in CSV", i + 1))?;
let row: Vec<Cell> = row
.iter()
.map(|cell| {
cell.parse::<f64>()
.map(Cell::Num)
.unwrap_or_else(|_| Cell::Txt(cell.to_string()))
})
.collect();
data.push(row);
}
let headers = rdr.cols.expect("headers should be initialised");
Data::new(headers, data)
}
}
fn str_eq_ignore_case_and_ws(a: &str, b: &str) -> bool {
let mut a = a.chars().filter(|x| !x.is_whitespace());
let mut b = b.chars().filter(|x| !x.is_whitespace());
loop {
match (a.next(), b.next()) {
(None, None) => break true,
(None, Some(_)) | (Some(_), None) => break false, (Some(a), Some(b)) => match a.eq_ignore_ascii_case(&b) {
true => (), false => break false, },
}
}
}
pub(crate) fn match_hdr_help(hdrs: &Headers, col: &str) -> String {
let mut s = String::from("help - these headers are similar:");
let l = s.len();
for h in hdrs.fuzzy_match(col) {
s.push(' ');
s.push_str(&h);
}
if s.len() == l {
s.clear();
s.push_str("help - no columns match, use `cat <file> | head -n1` for inspect headers");
}
s
}
#[cfg(test)]
mod tests {
use super::*;
use fastrand::*;
use std::iter::*;
#[test]
fn eq_testing() {
use str_eq_ignore_case_and_ws as f;
assert!(f("", ""));
assert!(f(" ", " "));
assert!(f(" a ", " a"));
}
#[test]
fn fuzz_eq_testing() {
for _ in 0..10_000 {
let a: String = repeat_with(|| char(..)).take(usize(..100)).collect();
let b = a.chars().fold(String::new(), |mut s, c| {
s.extend(repeat(' ').take(usize(..2)));
s.push(c);
s
});
assert!(str_eq_ignore_case_and_ws(&a, &b));
}
}
#[test]
fn fuzz_eq_testing2() {
for _ in 0..10_000 {
let mut a: String = repeat_with(|| char(..)).take(usize(..100)).collect();
let mut b: String = repeat_with(|| char(..)).take(usize(..100)).collect();
let x = str_eq_ignore_case_and_ws(&a, &b);
a.retain(|c| !c.is_whitespace());
b.retain(|c| !c.is_whitespace());
let y = a.eq_ignore_ascii_case(&b);
assert_eq!(x, y);
}
}
}