use std::borrow::Cow;
use time::Tm;
use chrono::{DateTime, TimeZone};
use {Cookie, SameSite};
#[derive(Debug, Clone)]
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),
}
}
#[deprecated(
since = "0.13.1",
note = "Use the expire_ts() or expire_datetime() functions instead. It makes the interface independent from time crate. "
)]
pub fn expires(mut self, when: Tm) -> Self {
self.cookie.set_expires(when);
self
}
#[inline]
pub fn expires_ts(mut self, when_in_sec: i64) -> Self {
let tm: time::Tm = time::at_utc(time::Timespec::new(when_in_sec, 0));
self.cookie.set_expires(tm);
self
}
#[inline]
pub fn expires_datetime<T>(self, when: DateTime<T>) -> Self
where
T: TimeZone,
{
self.expires_ts(when.timestamp())
}
#[inline]
pub fn max_age(mut self, value: u64) -> 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 finish(self) -> Cookie<'c> {
self.cookie
}
}