http-types-2 3.1.0

Common types for HTTP operations.
Documentation

use crate::headers::{HeaderName, HeaderValue, Headers};

/// A trait representing a [`HeaderName`] and [`HeaderValue`] pair.
pub trait Header {
    /// Access the header's name.
    fn header_name(&self) -> HeaderName;

    /// Access the header's value.
    fn header_value(&self) -> HeaderValue;

    /// Insert the header name and header value into something that looks like a
    /// [`Headers`] map.
    fn apply_header<H: AsMut<Headers>>(&self, mut headers: H) {
        let name = self.header_name();
        let value = self.header_value();

        // The value should already have been validated when it was created, so this should
        // possibly be done with an unsafe call
        headers.as_mut().insert(name, value).unwrap();
    }
}

impl Header for (&str, &str) {
    fn header_name(&self) -> HeaderName {
        HeaderName::from(self.0)
    }

    fn header_value(&self) -> HeaderValue {
        HeaderValue::from_bytes(self.1.to_owned().into_bytes())
            .expect("String slice should be valid ASCII")
    }
}

impl<T: Header> Header for &T {
    fn header_name(&self) -> HeaderName {
        (**self).header_name()
    }

    fn header_value(&self) -> HeaderValue {
        (**self).header_value()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn header_from_strings() {
        let strings = ("Content-Length", "12");
        assert_eq!(strings.header_name(), "Content-Length");
        assert_eq!(strings.header_value(), "12");
    }
}