diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/headers-core/src | |
parent | Initial commit. (diff) | |
download | firefox-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-core/src')
-rw-r--r-- | third_party/rust/headers-core/src/lib.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/third_party/rust/headers-core/src/lib.rs b/third_party/rust/headers-core/src/lib.rs new file mode 100644 index 0000000000..92e3d15ea0 --- /dev/null +++ b/third_party/rust/headers-core/src/lib.rs @@ -0,0 +1,68 @@ +#![deny(missing_docs)] +#![deny(missing_debug_implementations)] +#![cfg_attr(test, deny(warnings))] +#![doc(html_root_url = "https://docs.rs/headers-core/0.2.0")] + +//! # headers-core +//! +//! This is the core crate of the typed HTTP headers system, providing only +//! the relevant traits. All actual header implementations are in other crates. + +extern crate http; + +pub use http::header::{self, HeaderName, HeaderValue}; + +use std::error; +use std::fmt::{self, Display, Formatter}; + +/// A trait for any object that will represent a header field and value. +/// +/// This trait represents the construction and identification of headers, +/// and contains trait-object unsafe methods. +pub trait Header { + /// The name of this header. + fn name() -> &'static HeaderName; + + /// Decode this type from an iterator of `HeaderValue`s. + fn decode<'i, I>(values: &mut I) -> Result<Self, Error> + where + Self: Sized, + I: Iterator<Item = &'i HeaderValue>; + + /// Encode this type to a `HeaderMap`. + /// + /// This function should be infallible. Any errors converting to a + /// `HeaderValue` should have been caught when parsing or constructing + /// this value. + fn encode<E: Extend<HeaderValue>>(&self, values: &mut E); +} + +/// Errors trying to decode a header. +#[derive(Debug)] +pub struct Error { + kind: Kind, +} + +#[derive(Debug)] +enum Kind { + Invalid, +} + +impl Error { + /// Create an 'invalid' Error. + pub fn invalid() -> Error { + Error { + kind: Kind::Invalid, + } + } +} + +impl Display for Error { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match &self.kind { + Kind::Invalid => f.write_str("invalid HTTP header"), + } + } +} + +impl error::Error for Error {} |