diff options
Diffstat (limited to 'third_party/rust/headers/src/util/csv.rs')
-rw-r--r-- | third_party/rust/headers/src/util/csv.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/third_party/rust/headers/src/util/csv.rs b/third_party/rust/headers/src/util/csv.rs new file mode 100644 index 0000000000..2d0a8ed68d --- /dev/null +++ b/third_party/rust/headers/src/util/csv.rs @@ -0,0 +1,38 @@ +use std::fmt; + +/// 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| { + string + .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(()) +} |