set_cookie/
lib.rs

1pub const SET_COOKIE: &str = "Set-Cookie";
2
3// https://developer.chrome.com/blog/cookie-max-age-expires?hl=zh-cn
4pub const MAX: u32 = 86400 * 400;
5
6pub struct Cookie {
7  pub domain: String,
8}
9
10pub fn new(domain: impl Into<String>) -> Cookie {
11  Cookie {
12    domain: domain.into(),
13  }
14}
15
16impl Cookie {
17  pub fn set_max_for_js(&self, key: impl AsRef<str>, val: impl AsRef<str>) -> String {
18    self.set_for_js(key, val, MAX)
19  }
20
21  pub fn set_for_js(&self, key: impl AsRef<str>, val: impl AsRef<str>, max_age: u32) -> String {
22    let key = key.as_ref();
23    let val = val.as_ref();
24    format!(
25      "{key}={val};Max-Age={max_age};Domain={};Secure;Path=/;Partitioned",
26      self.domain
27    )
28  }
29  pub fn set(&self, key: impl AsRef<str>, val: impl AsRef<str>, max_age: u32) -> String {
30    format!("{};HttpOnly", self.set_for_js(key, val, max_age),)
31  }
32
33  pub fn set_max(&self, key: impl AsRef<str>, val: impl AsRef<str>) -> String {
34    self.set(key, val, MAX)
35  }
36}