/rust/registry/src/index.crates.io-1949cf8c6b5b557f/headers-0.4.1/src/util/csv.rs
Line | Count | Source |
1 | | use std::fmt; |
2 | | |
3 | | use http::HeaderValue; |
4 | | |
5 | | use crate::Error; |
6 | | |
7 | | /// Reads a comma-delimited raw header into a Vec. |
8 | 0 | pub(crate) fn from_comma_delimited<'i, I, T, E>(values: &mut I) -> Result<E, Error> |
9 | 0 | where |
10 | 0 | I: Iterator<Item = &'i HeaderValue>, |
11 | 0 | T: ::std::str::FromStr, |
12 | 0 | E: ::std::iter::FromIterator<T>, |
13 | | { |
14 | 0 | values |
15 | 0 | .flat_map(|value| { |
16 | 0 | value.to_str().into_iter().flat_map(|string| { |
17 | 0 | string |
18 | 0 | .split(',') |
19 | 0 | .filter_map(|x| match x.trim() { |
20 | 0 | "" => None, |
21 | 0 | y => Some(y), |
22 | 0 | }) |
23 | 0 | .map(|x| x.parse().map_err(|_| Error::invalid())) |
24 | 0 | }) |
25 | 0 | }) |
26 | 0 | .collect() |
27 | 0 | } |
28 | | |
29 | | /// Format an array into a comma-delimited string. |
30 | 0 | pub(crate) fn fmt_comma_delimited<T: fmt::Display>( |
31 | 0 | f: &mut fmt::Formatter, |
32 | 0 | mut iter: impl Iterator<Item = T>, |
33 | 0 | ) -> fmt::Result { |
34 | 0 | if let Some(part) = iter.next() { |
35 | 0 | fmt::Display::fmt(&part, f)?; |
36 | 0 | } |
37 | 0 | for part in iter { |
38 | 0 | f.write_str(", ")?; |
39 | 0 | fmt::Display::fmt(&part, f)?; |
40 | | } |
41 | 0 | Ok(()) |
42 | 0 | } |