use std::fmt;
use rama_http_types::HeaderValue;
use crate::Error;
pub 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: FromIterator<T>,
{
values
.flat_map(|value| {
value
.to_str()
.into_iter()
.flat_map(|string| split_csv_str(string))
})
.collect()
}
pub(crate) fn split_csv_str<T: std::str::FromStr>(
string: &str,
) -> impl Iterator<Item = Result<T, Error>> + use<'_, T> {
let mut in_quotes = false;
string
.split(move |c| {
#[expect(clippy::collapsible_else_if)]
if in_quotes {
if c == '"' {
in_quotes = false;
}
false } else {
if c == ',' {
true } else {
if c == '"' {
in_quotes = true;
}
false }
}
})
.filter_map(|x| match x.trim() {
"" => None,
y => Some(y.parse().map_err(|_e| Error::invalid())),
})
}
pub 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(())
}