Debian Codegraph @

third_party/rust/headers-core/src/lib.rs

raw
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.2.0")]
5
6 //! # 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.
10
11 extern crate http;
12
13 pub use http::header::{self, HeaderName, HeaderValue};
14
15 use std::error;
16 use std::fmt::{self, Display, Formatter};
17
18 /// 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.
22 pub trait Header {
23 /// The name of this header.
24 fn name() -> &'static HeaderName;
25
26 /// Decode this type from an iterator of `HeaderValue`s.
27 fn decode<'i, I>(values: &mut I) -> Result<Self, Error>
28 where
29 Self: Sized,
30 I: Iterator<Item = &'i HeaderValue>;
31
32 /// 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.
37 fn encode<E: Extend<HeaderValue>>(&self, values: &mut E);
38 }
39
40 /// Errors trying to decode a header.
41 #[derive(Debug)]
42 pub struct Error {
43 kind: Kind,
44 }
45
46 #[derive(Debug)]
47 enum Kind {
48 Invalid,
49 }
50
51 impl Error {
52 /// Create an 'invalid' Error.
53 pub fn invalid() -> Error {
54 Error {
55 kind: Kind::Invalid,
56 }
57 }
58 }
59
60 impl Display for Error {
61 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
62 match &self.kind {
63 Kind::Invalid => f.write_str("invalid HTTP header"),
64 }
65 }
66 }
67
68 impl error::Error for Error {}