summaryrefslogtreecommitdiffstats
path: root/third_party/rust/headers/src/util/value_string.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/headers/src/util/value_string.rs
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/headers/src/util/value_string.rs')
-rw-r--r--third_party/rust/headers/src/util/value_string.rs93
1 files changed, 93 insertions, 0 deletions
diff --git a/third_party/rust/headers/src/util/value_string.rs b/third_party/rust/headers/src/util/value_string.rs
new file mode 100644
index 0000000000..865a3558b9
--- /dev/null
+++ b/third_party/rust/headers/src/util/value_string.rs
@@ -0,0 +1,93 @@
+use std::{
+ fmt,
+ str::{self, FromStr},
+};
+
+use bytes::Bytes;
+use http::header::HeaderValue;
+
+use super::IterExt;
+
+/// A value that is both a valid `HeaderValue` and `String`.
+#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub(crate) struct HeaderValueString {
+ /// Care must be taken to only set this value when it is also
+ /// a valid `String`, since `as_str` will convert to a `&str`
+ /// in an unchecked manner.
+ value: HeaderValue,
+}
+
+impl HeaderValueString {
+ pub(crate) fn from_val(val: &HeaderValue) -> Result<Self, ::Error> {
+ if val.to_str().is_ok() {
+ Ok(HeaderValueString { value: val.clone() })
+ } else {
+ Err(::Error::invalid())
+ }
+ }
+
+ pub(crate) fn from_string(src: String) -> Option<Self> {
+ // A valid `str` (the argument)...
+ let bytes = Bytes::from(src);
+ HeaderValue::from_maybe_shared(bytes)
+ .ok()
+ .map(|value| HeaderValueString { value })
+ }
+
+ pub(crate) fn from_static(src: &'static str) -> HeaderValueString {
+ // A valid `str` (the argument)...
+ HeaderValueString {
+ value: HeaderValue::from_static(src),
+ }
+ }
+
+ pub(crate) fn as_str(&self) -> &str {
+ // HeaderValueString is only created from HeaderValues
+ // that have validated they are also UTF-8 strings.
+ unsafe { str::from_utf8_unchecked(self.value.as_bytes()) }
+ }
+}
+
+impl fmt::Debug for HeaderValueString {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Debug::fmt(self.as_str(), f)
+ }
+}
+
+impl fmt::Display for HeaderValueString {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(self.as_str(), f)
+ }
+}
+
+impl super::TryFromValues for HeaderValueString {
+ fn try_from_values<'i, I>(values: &mut I) -> Result<Self, ::Error>
+ where
+ I: Iterator<Item = &'i HeaderValue>,
+ {
+ values
+ .just_one()
+ .map(HeaderValueString::from_val)
+ .unwrap_or_else(|| Err(::Error::invalid()))
+ }
+}
+
+impl<'a> From<&'a HeaderValueString> for HeaderValue {
+ fn from(src: &'a HeaderValueString) -> HeaderValue {
+ src.value.clone()
+ }
+}
+
+#[derive(Debug)]
+pub(crate) struct FromStrError(());
+
+impl FromStr for HeaderValueString {
+ type Err = FromStrError;
+
+ fn from_str(src: &str) -> Result<Self, Self::Err> {
+ // A valid `str` (the argument)...
+ src.parse()
+ .map(|value| HeaderValueString { value })
+ .map_err(|_| FromStrError(()))
+ }
+}