rama-http-headers 0.3.0

typed http headers for rama
Documentation
use std::fmt;

use rama_http_types::HeaderValue;

use crate::Error;

/// Reads a comma-delimited raw header into a Vec.
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 // don't split
            } else {
                if c == ',' {
                    true // split
                } else {
                    if c == '"' {
                        in_quotes = true;
                    }
                    false // don't split
                }
            }
        })
        .filter_map(|x| match x.trim() {
            "" => None,
            y => Some(y.parse().map_err(|_e| Error::invalid())),
        })
}

/// Format an array into a comma-delimited string.
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(())
}