use crate::types::{Cookie, SameSite};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Attribute {
Domain(String),
Path(String),
Expires(String),
MaxAge(i64),
Secure,
HttpOnly,
SameSite(SameSite),
Extension(String, Option<String>),
}
impl Attribute {
pub fn apply_to(&self, cookie: &mut Cookie) {
match self {
Attribute::Domain(d) => cookie.domain = Some(d.clone()),
Attribute::Path(p) => cookie.path = Some(p.clone()),
Attribute::MaxAge(age) => cookie.max_age = Some(*age),
Attribute::Secure => cookie.secure = true,
Attribute::HttpOnly => cookie.http_only = true,
Attribute::SameSite(ss) => cookie.same_site = Some(*ss),
Attribute::Expires(_) => {
}
Attribute::Extension(name, value) => {
cookie
.extensions
.insert(name.clone(), value.clone().unwrap_or_default());
}
}
}
#[must_use]
pub fn name(&self) -> &str {
match self {
Attribute::Domain(_) => "Domain",
Attribute::Path(_) => "Path",
Attribute::Expires(_) => "Expires",
Attribute::MaxAge(_) => "Max-Age",
Attribute::Secure => "Secure",
Attribute::HttpOnly => "HttpOnly",
Attribute::SameSite(_) => "SameSite",
Attribute::Extension(name, _) => name,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_attribute_apply() {
let mut cookie = Cookie::new("test".to_string(), "value".to_string());
let attr = Attribute::Secure;
attr.apply_to(&mut cookie);
assert!(cookie.secure);
let attr = Attribute::Domain("example.com".to_string());
attr.apply_to(&mut cookie);
assert_eq!(cookie.domain, Some("example.com".to_string()));
}
#[test]
fn test_attribute_name() {
assert_eq!(Attribute::Secure.name(), "Secure");
assert_eq!(Attribute::HttpOnly.name(), "HttpOnly");
}
}