1use std::fmt;
2
3use http::HeaderValue;
4
5use crate::Error;
6
7pub(crate) fn from_comma_delimited<'i, I, T, E>(values: &mut I) -> Result<E, Error>
9where
10 I: Iterator<Item = &'i HeaderValue>,
11 T: ::std::str::FromStr,
12 E: ::std::iter::FromIterator<T>,
13{
14 values
15 .flat_map(|value| {
16 value.to_str().into_iter().flat_map(|string| {
17 string
18 .split(',')
19 .filter_map(|x| match x.trim() {
20 "" => None,
21 y => Some(y),
22 })
23 .map(|x| x.parse().map_err(|_| Error::invalid()))
24 })
25 })
26 .collect()
27}
28
29pub(crate) fn fmt_comma_delimited<T: fmt::Display>(
31 f: &mut fmt::Formatter,
32 mut iter: impl Iterator<Item = T>,
33) -> fmt::Result {
34 if let Some(part) = iter.next() {
35 fmt::Display::fmt(&part, f)?;
36 }
37 for part in iter {
38 f.write_str(", ")?;
39 fmt::Display::fmt(&part, f)?;
40 }
41 Ok(())
42}