use std::fmt;
use actix_web::{
error::ParseError,
http::header::{Header, HeaderName, HeaderValue, TryIntoHeaderValue, AUTHORIZATION},
HttpMessage,
};
use crate::headers::authorization::scheme::Scheme;
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Authorization<S: Scheme>(S);
impl<S: Scheme> Authorization<S> {
pub fn into_scheme(self) -> S {
self.0
}
}
impl<S: Scheme> From<S> for Authorization<S> {
fn from(scheme: S) -> Authorization<S> {
Authorization(scheme)
}
}
impl<S: Scheme> AsRef<S> for Authorization<S> {
fn as_ref(&self) -> &S {
&self.0
}
}
impl<S: Scheme> AsMut<S> for Authorization<S> {
fn as_mut(&mut self) -> &mut S {
&mut self.0
}
}
impl<S: Scheme> fmt::Display for Authorization<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl<S: Scheme> Header for Authorization<S> {
#[inline]
fn name() -> HeaderName {
AUTHORIZATION
}
fn parse<T: HttpMessage>(msg: &T) -> Result<Self, ParseError> {
let header = msg.headers().get(Self::name()).ok_or(ParseError::Header)?;
let scheme = S::parse(header).map_err(|_| ParseError::Header)?;
Ok(Authorization(scheme))
}
}
impl<S: Scheme> TryIntoHeaderValue for Authorization<S> {
type Error = <S as TryIntoHeaderValue>::Error;
fn try_into_value(self) -> Result<HeaderValue, Self::Error> {
self.0.try_into_value()
}
}