use std::borrow::{Cow, Borrow, BorrowMut};
use crate::{Cookie, SameSite, Expiration};
#[derive(Debug, Clone, PartialEq)]
pub struct CookieBuilder<'c> {
cookie: Cookie<'c>,
}
impl<'c> CookieBuilder<'c> {
pub fn new<N, V>(name: N, value: V) -> Self
where N: Into<Cow<'c, str>>,
V: Into<Cow<'c, str>>
{
CookieBuilder { cookie: Cookie::new(name, value) }
}
#[inline]
pub fn expires<E: Into<Expiration>>(mut self, when: E) -> Self {
self.cookie.set_expires(when);
self
}
#[inline]
pub fn max_age(mut self, value: time::Duration) -> Self {
self.cookie.set_max_age(value);
self
}
pub fn domain<D: Into<Cow<'c, str>>>(mut self, value: D) -> Self {
self.cookie.set_domain(value);
self
}
pub fn path<P: Into<Cow<'c, str>>>(mut self, path: P) -> Self {
self.cookie.set_path(path);
self
}
#[inline]
pub fn secure(mut self, value: bool) -> Self {
self.cookie.set_secure(value);
self
}
#[inline]
pub fn http_only(mut self, value: bool) -> Self {
self.cookie.set_http_only(value);
self
}
#[inline]
pub fn same_site(mut self, value: SameSite) -> Self {
self.cookie.set_same_site(value);
self
}
#[inline]
pub fn permanent(mut self) -> Self {
self.cookie.make_permanent();
self
}
#[inline]
pub fn inner(&self) -> &Cookie<'c> {
&self.cookie
}
#[inline]
pub fn inner_mut(&mut self) -> &mut Cookie<'c> {
&mut self.cookie
}
#[inline]
pub fn build(self) -> Cookie<'c> {
self.cookie
}
#[deprecated(since="0.18.0", note="`CookieBuilder` can be passed in to methods expecting a `Cookie`; for other cases, use `CookieBuilder::build()`")]
pub fn finish(self) -> Cookie<'c> {
self.cookie
}
}
impl std::fmt::Display for CookieBuilder<'_> {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.cookie.fmt(f)
}
}
impl<'a> Borrow<Cookie<'a>> for CookieBuilder<'a> {
fn borrow(&self) -> &Cookie<'a> {
&self.cookie
}
}
impl<'a> BorrowMut<Cookie<'a>> for CookieBuilder<'a> {
fn borrow_mut(&mut self) -> &mut Cookie<'a> {
&mut self.cookie
}
}
impl<'a> AsRef<Cookie<'a>> for CookieBuilder<'a> {
fn as_ref(&self) -> &Cookie<'a> {
&self.cookie
}
}
impl<'a> AsMut<Cookie<'a>> for CookieBuilder<'a> {
fn as_mut(&mut self) -> &mut Cookie<'a> {
&mut self.cookie
}
}
impl<'a, 'b> PartialEq<Cookie<'b>> for CookieBuilder<'a> {
fn eq(&self, other: &Cookie<'b>) -> bool {
&self.cookie == other
}
}
impl<'a, 'b> PartialEq<CookieBuilder<'b>> for Cookie<'a> {
fn eq(&self, other: &CookieBuilder<'b>) -> bool {
self == &other.cookie
}
}
impl<'c> From<Cookie<'c>> for CookieBuilder<'c> {
fn from(cookie: Cookie<'c>) -> Self {
CookieBuilder { cookie }
}
}