requiem_http/cookie/builder.rs
1use std::borrow::Cow;
2
3use chrono::Duration;
4use time::Tm;
5
6use super::{Cookie, SameSite};
7
8/// Structure that follows the builder pattern for building `Cookie` structs.
9///
10/// To construct a cookie:
11///
12/// 1. Call [`Cookie::build`](struct.Cookie.html#method.build) to start building.
13/// 2. Use any of the builder methods to set fields in the cookie.
14/// 3. Call [finish](#method.finish) to retrieve the built cookie.
15///
16/// # Example
17///
18/// ```rust
19/// use requiem_http::cookie::Cookie;
20///
21/// let cookie: Cookie = Cookie::build("name", "value")
22/// .domain("www.rust-lang.org")
23/// .path("/")
24/// .secure(true)
25/// .http_only(true)
26/// .max_age(84600)
27/// .finish();
28/// ```
29#[derive(Debug, Clone)]
30pub struct CookieBuilder {
31 /// The cookie being built.
32 cookie: Cookie<'static>,
33}
34
35impl CookieBuilder {
36 /// Creates a new `CookieBuilder` instance from the given name and value.
37 ///
38 /// This method is typically called indirectly via
39 /// [Cookie::build](struct.Cookie.html#method.build).
40 ///
41 /// # Example
42 ///
43 /// ```rust
44 /// use requiem_http::cookie::Cookie;
45 ///
46 /// let c = Cookie::build("foo", "bar").finish();
47 /// assert_eq!(c.name_value(), ("foo", "bar"));
48 /// ```
49 pub fn new<N, V>(name: N, value: V) -> CookieBuilder
50 where
51 N: Into<Cow<'static, str>>,
52 V: Into<Cow<'static, str>>,
53 {
54 CookieBuilder {
55 cookie: Cookie::new(name, value),
56 }
57 }
58
59 /// Sets the `expires` field in the cookie being built.
60 ///
61 /// # Example
62 ///
63 /// ```rust
64 /// use requiem_http::cookie::Cookie;
65 ///
66 /// let c = Cookie::build("foo", "bar")
67 /// .expires(time::now())
68 /// .finish();
69 ///
70 /// assert!(c.expires().is_some());
71 /// ```
72 #[inline]
73 pub fn expires(mut self, when: Tm) -> CookieBuilder {
74 self.cookie.set_expires(when);
75 self
76 }
77
78 /// Sets the `max_age` field in seconds in the cookie being built.
79 ///
80 /// # Example
81 ///
82 /// ```rust
83 /// use requiem_http::cookie::Cookie;
84 ///
85 /// let c = Cookie::build("foo", "bar")
86 /// .max_age(1800)
87 /// .finish();
88 ///
89 /// assert_eq!(c.max_age(), Some(time::Duration::seconds(30 * 60)));
90 /// ```
91 #[inline]
92 pub fn max_age(self, seconds: i64) -> CookieBuilder {
93 self.max_age_time(Duration::seconds(seconds))
94 }
95
96 /// Sets the `max_age` field in the cookie being built.
97 ///
98 /// # Example
99 ///
100 /// ```rust
101 /// use requiem_http::cookie::Cookie;
102 ///
103 /// let c = Cookie::build("foo", "bar")
104 /// .max_age_time(time::Duration::minutes(30))
105 /// .finish();
106 ///
107 /// assert_eq!(c.max_age(), Some(time::Duration::seconds(30 * 60)));
108 /// ```
109 #[inline]
110 pub fn max_age_time(mut self, value: Duration) -> CookieBuilder {
111 self.cookie.set_max_age(value);
112 self
113 }
114
115 /// Sets the `domain` field in the cookie being built.
116 ///
117 /// # Example
118 ///
119 /// ```rust
120 /// use requiem_http::cookie::Cookie;
121 ///
122 /// let c = Cookie::build("foo", "bar")
123 /// .domain("www.rust-lang.org")
124 /// .finish();
125 ///
126 /// assert_eq!(c.domain(), Some("www.rust-lang.org"));
127 /// ```
128 pub fn domain<D: Into<Cow<'static, str>>>(mut self, value: D) -> CookieBuilder {
129 self.cookie.set_domain(value);
130 self
131 }
132
133 /// Sets the `path` field in the cookie being built.
134 ///
135 /// # Example
136 ///
137 /// ```rust
138 /// use requiem_http::cookie::Cookie;
139 ///
140 /// let c = Cookie::build("foo", "bar")
141 /// .path("/")
142 /// .finish();
143 ///
144 /// assert_eq!(c.path(), Some("/"));
145 /// ```
146 pub fn path<P: Into<Cow<'static, str>>>(mut self, path: P) -> CookieBuilder {
147 self.cookie.set_path(path);
148 self
149 }
150
151 /// Sets the `secure` field in the cookie being built.
152 ///
153 /// # Example
154 ///
155 /// ```rust
156 /// use requiem_http::cookie::Cookie;
157 ///
158 /// let c = Cookie::build("foo", "bar")
159 /// .secure(true)
160 /// .finish();
161 ///
162 /// assert_eq!(c.secure(), Some(true));
163 /// ```
164 #[inline]
165 pub fn secure(mut self, value: bool) -> CookieBuilder {
166 self.cookie.set_secure(value);
167 self
168 }
169
170 /// Sets the `http_only` field in the cookie being built.
171 ///
172 /// # Example
173 ///
174 /// ```rust
175 /// use requiem_http::cookie::Cookie;
176 ///
177 /// let c = Cookie::build("foo", "bar")
178 /// .http_only(true)
179 /// .finish();
180 ///
181 /// assert_eq!(c.http_only(), Some(true));
182 /// ```
183 #[inline]
184 pub fn http_only(mut self, value: bool) -> CookieBuilder {
185 self.cookie.set_http_only(value);
186 self
187 }
188
189 /// Sets the `same_site` field in the cookie being built.
190 ///
191 /// # Example
192 ///
193 /// ```rust
194 /// use requiem_http::cookie::{Cookie, SameSite};
195 ///
196 /// let c = Cookie::build("foo", "bar")
197 /// .same_site(SameSite::Strict)
198 /// .finish();
199 ///
200 /// assert_eq!(c.same_site(), Some(SameSite::Strict));
201 /// ```
202 #[inline]
203 pub fn same_site(mut self, value: SameSite) -> CookieBuilder {
204 self.cookie.set_same_site(value);
205 self
206 }
207
208 /// Makes the cookie being built 'permanent' by extending its expiration and
209 /// max age 20 years into the future.
210 ///
211 /// # Example
212 ///
213 /// ```rust
214 /// use requiem_http::cookie::Cookie;
215 /// use chrono::Duration;
216 ///
217 /// let c = Cookie::build("foo", "bar")
218 /// .permanent()
219 /// .finish();
220 ///
221 /// assert_eq!(c.max_age(), Some(Duration::days(365 * 20)));
222 /// # assert!(c.expires().is_some());
223 /// ```
224 #[inline]
225 pub fn permanent(mut self) -> CookieBuilder {
226 self.cookie.make_permanent();
227 self
228 }
229
230 /// Finishes building and returns the built `Cookie`.
231 ///
232 /// # Example
233 ///
234 /// ```rust
235 /// use requiem_http::cookie::Cookie;
236 ///
237 /// let c = Cookie::build("foo", "bar")
238 /// .domain("crates.io")
239 /// .path("/")
240 /// .finish();
241 ///
242 /// assert_eq!(c.name_value(), ("foo", "bar"));
243 /// assert_eq!(c.domain(), Some("crates.io"));
244 /// assert_eq!(c.path(), Some("/"));
245 /// ```
246 #[inline]
247 pub fn finish(self) -> Cookie<'static> {
248 self.cookie
249 }
250}