forked from hyperium/headers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.rs
More file actions
60 lines (56 loc) · 1.81 KB
/
csv.rs
File metadata and controls
60 lines (56 loc) · 1.81 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
use std::fmt;
use http::HeaderValue;
use crate::Error;
/// Reads a comma-delimited raw header into a Vec.
pub(crate) fn from_comma_delimited<'i, I, T, E>(values: &mut I) -> Result<E, Error>
where
I: Iterator<Item = &'i HeaderValue>,
T: ::std::str::FromStr,
E: ::std::iter::FromIterator<T>,
{
values
.flat_map(|value| {
value.to_str().into_iter().flat_map(|string| {
let mut in_quotes = false;
string
.split(move |c| {
#[allow(clippy::collapsible_else_if)]
if in_quotes {
if c == '"' {
in_quotes = false;
}
false // dont split
} else {
if c == ',' {
true // split
} else {
if c == '"' {
in_quotes = true;
}
false // dont split
}
}
})
.filter_map(|x| match x.trim() {
"" => None,
y => Some(y),
})
.map(|x| x.parse().map_err(|_| Error::invalid()))
})
})
.collect()
}
/// Format an array into a comma-delimited string.
pub(crate) fn fmt_comma_delimited<T: fmt::Display>(
f: &mut fmt::Formatter,
mut iter: impl Iterator<Item = T>,
) -> fmt::Result {
if let Some(part) = iter.next() {
fmt::Display::fmt(&part, f)?;
}
for part in iter {
f.write_str(", ")?;
fmt::Display::fmt(&part, f)?;
}
Ok(())
}