1#![deny(missing_docs)]
2#![deny(missing_debug_implementations)]
3#![cfg_attr(test, deny(warnings))]
4#![doc(html_root_url = "https://docs.rs/headers-core/0.3.0")]
56//! # headers-core
7//!
8//! This is the core crate of the typed HTTP headers system, providing only
9//! the relevant traits. All actual header implementations are in other crates.
1011extern crate http;
1213pub use http::header::{self, HeaderName, HeaderValue};
1415use std::error;
16use std::fmt::{self, Display, Formatter};
1718/// A trait for any object that will represent a header field and value.
19///
20/// This trait represents the construction and identification of headers,
21/// and contains trait-object unsafe methods.
22pub trait Header {
23/// The name of this header.
24fn name() -> &'static HeaderName;
2526/// Decode this type from an iterator of `HeaderValue`s.
27fn decode<'i, I>(values: &mut I) -> Result<Self, Error>
28where
29Self: Sized,
30 I: Iterator<Item = &'i HeaderValue>;
3132/// Encode this type to a `HeaderMap`.
33 ///
34 /// This function should be infallible. Any errors converting to a
35 /// `HeaderValue` should have been caught when parsing or constructing
36 /// this value.
37fn encode<E: Extend<HeaderValue>>(&self, values: &mut E);
38}
3940/// Errors trying to decode a header.
41#[derive(Debug)]
42pub struct Error {
43 kind: Kind,
44}
4546#[derive(Debug)]
47enum Kind {
48 Invalid,
49}
5051impl Error {
52/// Create an 'invalid' Error.
53pub fn invalid() -> Error {
54 Error {
55 kind: Kind::Invalid,
56 }
57 }
58}
5960impl Display for Error {
61fn fmt(&self, f: &mut Formatter) -> fmt::Result {
62match &self.kind {
63 Kind::Invalid => f.write_str("invalid HTTP header"),
64 }
65 }
66}
6768impl error::Error for Error {}