laxum 0.1.0

Make working with Axum a little smoother.
Documentation
use http::header::{AsHeaderName, HeaderMap};
use http::request::{Parts, Request};

/// Return a header only if it is a valid string.
///
/// Because the [`HeaderValue`](http::header::HeaderValue) can be not actual
/// visible UTF-8 text, if you want the header as a _string_ you have to
/// do this little extra dance. This trait cuts out the middleman.
pub trait GetStrHeaders {
    fn get_str<N: AsHeaderName>(&self, n: N) -> Option<&str>;
}

impl GetStrHeaders for HeaderMap {
    fn get_str<N: AsHeaderName>(&self, n: N) -> Option<&str> {
        let hval = self.get(n)?;
        match hval.to_str() {
            Ok(hstr) => Some(hstr),
            Err(_) => None,
        }
    }
}

impl<T> GetStrHeaders for Request<T> {
    fn get_str<N: AsHeaderName>(&self, n: N) -> Option<&str> {
        self.headers().get_str(n)
    }
}

impl GetStrHeaders for Parts {
    fn get_str<N: AsHeaderName>(&self, n: N) -> Option<&str> {
        self.headers.get_str(n)
    }
}