use std::convert::TryInto;
use crate::header;
use std::fmt;
use std::time::SystemTime;
pub struct Cookie<'a>(cookie_crate::Cookie<'a>);
impl<'a> Cookie<'a> {
fn parse(value: &'a crate::header::HeaderValue) -> Result<Cookie<'a>, CookieParseError> {
std::str::from_utf8(value.as_bytes())
.map_err(cookie_crate::ParseError::from)
.and_then(cookie_crate::Cookie::parse)
.map_err(CookieParseError)
.map(Cookie)
}
pub(crate) fn into_inner(self) -> cookie_crate::Cookie<'a> {
self.0
}
pub fn name(&self) -> &str {
self.0.name()
}
pub fn value(&self) -> &str {
self.0.value()
}
pub fn http_only(&self) -> bool {
self.0.http_only().unwrap_or(false)
}
pub fn secure(&self) -> bool {
self.0.secure().unwrap_or(false)
}
pub fn same_site_lax(&self) -> bool {
self.0.same_site() == Some(cookie_crate::SameSite::Lax)
}
pub fn same_site_strict(&self) -> bool {
self.0.same_site() == Some(cookie_crate::SameSite::Strict)
}
pub fn path(&self) -> Option<&str> {
self.0.path()
}
pub fn domain(&self) -> Option<&str> {
self.0.domain()
}
pub fn max_age(&self) -> Option<std::time::Duration> {
self.0
.max_age()
.map(|d| d.try_into().expect("time::Duration into std::time::Duration"))
}
pub fn expires(&self) -> Option<SystemTime> {
self.0.expires().map(SystemTime::from)
}
}
impl<'a> fmt::Debug for Cookie<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
pub(crate) fn extract_response_cookies<'a>(
headers: &'a hyper::HeaderMap,
) -> impl Iterator<Item = Result<Cookie<'a>, CookieParseError>> + 'a {
headers
.get_all(header::SET_COOKIE)
.iter()
.map(|value| Cookie::parse(value))
}
#[derive(Default)]
pub(crate) struct CookieStore(pub(crate) cookie_store::CookieStore);
impl<'a> fmt::Debug for CookieStore {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
pub(crate) struct CookieParseError(cookie_crate::ParseError);
impl<'a> fmt::Debug for CookieParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl<'a> fmt::Display for CookieParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl std::error::Error for CookieParseError {}