rusty_format/
cookie.rs

1use crate::{
2    Cookie, KeyNotPresent, KeyPresent, Properties, SameSite, ValueNotPresent, ValuePresent,
3};
4
5impl<'cookie> Cookie<'cookie, KeyNotPresent, ValueNotPresent> {
6    pub fn new() -> Self {
7        Cookie {
8            key: "",
9            value: "",
10            http_only: None,
11            same_site: None,
12            secure: None,
13            path: None,
14            max_age: None,
15            properties: None,
16            keystate: std::marker::PhantomData,
17            valuestate: std::marker::PhantomData,
18        }
19    }
20    pub fn key(mut self, key: &'cookie str) -> Cookie<'cookie, KeyPresent, ValueNotPresent> {
21        self.key = key;
22        Cookie {
23            key: self.key,
24            value: self.value,
25            http_only: self.http_only,
26            same_site: self.same_site,
27            secure: self.secure,
28            path: self.path,
29            max_age: self.max_age,
30            properties: self.properties,
31            keystate: std::marker::PhantomData,
32            valuestate: std::marker::PhantomData,
33        }
34    }
35}
36
37impl<'cookie> Cookie<'cookie, KeyPresent, ValueNotPresent> {
38    pub fn value(mut self, value: &'cookie str) -> Cookie<'cookie, KeyPresent, ValuePresent> {
39        self.value = value;
40        Cookie {
41            key: self.key,
42            value: self.value,
43            http_only: self.http_only,
44            same_site: self.same_site,
45            secure: self.secure,
46            path: self.path,
47            max_age: self.max_age,
48            properties: self.properties,
49            keystate: std::marker::PhantomData,
50            valuestate: std::marker::PhantomData,
51        }
52    }
53}
54
55impl<'cookie> Cookie<'cookie, KeyPresent, ValuePresent> {
56    pub fn http_only(mut self, http_only: bool) -> Self {
57        self.http_only = Some(http_only);
58        self
59    }
60    pub fn same_site(mut self, same_site: SameSite) -> Self {
61        self.same_site = Some(same_site);
62        self
63    }
64    pub fn secure(mut self, secure: bool) -> Self {
65        self.secure = Some(secure);
66        self
67    }
68    pub fn path(mut self, path: &'cookie str) -> Self {
69        self.path = Some(path);
70        self
71    }
72    pub fn max_age(mut self, max_age: u64) -> Self {
73        self.max_age = Some(max_age);
74        self
75    }
76    pub fn properties(mut self, properties: Properties) -> Self {
77        self.properties = Some(properties);
78        self
79    }
80    pub fn build(self) -> String {
81        let value = self.value;
82        let key = self.key;
83        let http_only = self.http_only;
84        let same_site = self.same_site;
85        let secure = self.secure;
86        let path = self.path;
87        let max_age = self.max_age;
88        let properties = self.properties;
89
90        let pair = format!("Set-Cookie: {}={}; ", key, value);
91        let mut cookie = String::new();
92        cookie.push_str(&pair);
93        if let Some(http_only) = http_only {
94            cookie.push_str("HttpOnly; ");
95        }
96        if let Some(same_site) = same_site {
97            match same_site {
98                SameSite::Lax => {
99                    cookie.push_str("SameSite=Lax; ");
100                }
101                SameSite::None => {
102                    cookie.push_str("SameSite=None; ");
103                }
104                SameSite::Strict => {
105                    cookie.push_str("SameSite=Strict; ");
106                }
107            }
108        }
109        if let Some(secure) = secure {
110            match secure {
111                true => {
112                    cookie.push_str("Secure; ");
113                }
114                false => {}
115            }
116        }
117        if let Some(path) = path {
118            let path = format!("Path={}; ", path);
119            cookie.push_str(&path);
120        }
121        if let Some(max_age) = max_age {
122            let max_age = format!("Max-Age={}; ", max_age);
123            cookie.push_str(&max_age);
124        }
125        if let Some(properties) = properties {
126            match properties {
127                Properties::High => {
128                    cookie.push_str("Priority=High; ");
129                }
130                Properties::Low => {
131                    cookie.push_str("Priority=Low; ");
132                }
133                Properties::Medium => {
134                    cookie.push_str("Priority=Medium; ");
135                }
136            }
137        }
138        cookie
139    }
140}