cookie/expiration.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
use time::OffsetDateTime;
/// A cookie's expiration: either session or a date-time.
///
/// An `Expiration` is constructible via `Expiration::from()` with an
/// `Option<OffsetDateTime>` or an `OffsetDateTime`:
///
/// * `None` -> `Expiration::Session`
/// * `Some(OffsetDateTime)` -> `Expiration::DateTime`
/// * `OffsetDateTime` -> `Expiration::DateTime`
///
/// ```rust
/// use cookie::Expiration;
/// use time::OffsetDateTime;
///
/// let expires = Expiration::from(None);
/// assert_eq!(expires, Expiration::Session);
///
/// let now = OffsetDateTime::now_utc();
/// let expires = Expiration::from(now);
/// assert_eq!(expires, Expiration::DateTime(now));
///
/// let expires = Expiration::from(Some(now));
/// assert_eq!(expires, Expiration::DateTime(now));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Expiration {
/// Expiration for a "permanent" cookie at a specific date-time.
DateTime(OffsetDateTime),
/// Expiration for a "session" cookie. Browsers define the notion of a
/// "session" and will automatically expire session cookies when they deem
/// the "session" to be over. This is typically, but need not be, when the
/// browser is closed.
Session,
}
impl Expiration {
/// Returns `true` if `self` is an `Expiration::DateTime`.
///
/// # Example
///
/// ```rust
/// use cookie::Expiration;
/// use time::OffsetDateTime;
///
/// let expires = Expiration::from(None);
/// assert!(!expires.is_datetime());
///
/// let expires = Expiration::from(OffsetDateTime::now_utc());
/// assert!(expires.is_datetime());
/// ```
pub fn is_datetime(&self) -> bool {
match self {
Expiration::DateTime(_) => true,
Expiration::Session => false
}
}
/// Returns `true` if `self` is an `Expiration::Session`.
///
/// # Example
///
/// ```rust
/// use cookie::Expiration;
/// use time::OffsetDateTime;
///
/// let expires = Expiration::from(None);
/// assert!(expires.is_session());
///
/// let expires = Expiration::from(OffsetDateTime::now_utc());
/// assert!(!expires.is_session());
/// ```
pub fn is_session(&self) -> bool {
match self {
Expiration::DateTime(_) => false,
Expiration::Session => true
}
}
/// Returns the inner `OffsetDateTime` if `self` is a `DateTime`.
///
/// # Example
///
/// ```rust
/// use cookie::Expiration;
/// use time::OffsetDateTime;
///
/// let expires = Expiration::from(None);
/// assert!(expires.datetime().is_none());
///
/// let now = OffsetDateTime::now_utc();
/// let expires = Expiration::from(now);
/// assert_eq!(expires.datetime(), Some(now));
/// ```
pub fn datetime(self) -> Option<OffsetDateTime> {
match self {
Expiration::Session => None,
Expiration::DateTime(v) => Some(v)
}
}
/// Applied `f` to the inner `OffsetDateTime` if `self` is a `DateTime` and
/// returns the mapped `Expiration`.
///
/// # Example
///
/// ```rust
/// use cookie::Expiration;
/// use time::{OffsetDateTime, Duration};
///
/// let now = OffsetDateTime::now_utc();
/// let one_week = Duration::weeks(1);
///
/// let expires = Expiration::from(now);
/// assert_eq!(expires.map(|t| t + one_week).datetime(), Some(now + one_week));
///
/// let expires = Expiration::from(None);
/// assert_eq!(expires.map(|t| t + one_week).datetime(), None);
/// ```
pub fn map<F>(self, f: F) -> Self
where F: FnOnce(OffsetDateTime) -> OffsetDateTime
{
match self {
Expiration::Session => Expiration::Session,
Expiration::DateTime(v) => Expiration::DateTime(f(v)),
}
}
}
impl<T: Into<Option<OffsetDateTime>>> From<T> for Expiration {
fn from(option: T) -> Self {
match option.into() {
Some(value) => Expiration::DateTime(value),
None => Expiration::Session
}
}
}