http_type/cookie/impl.rs
1use crate::*;
2
3impl CookieBuilder {
4 /// Creates a new CookieBuilder with the specified name and value.
5 ///
6 /// # Parameters
7 /// - `name`: The name of the cookie.
8 /// - `value`: The value of the cookie.
9 ///
10 /// # Returns
11 /// - A new CookieBuilder instance.
12 pub fn new<N, V>(name: N, value: V) -> Self
13 where
14 N: Into<CookieKey>,
15 V: Into<CookieValue>,
16 {
17 Self {
18 name: name.into(),
19 value: value.into(),
20 expires: None,
21 max_age: None,
22 domain: None,
23 path: None,
24 secure: false,
25 http_only: false,
26 same_site: None,
27 }
28 }
29
30 /// Sets the expiration date for the cookie.
31 ///
32 /// # Parameters
33 /// - `expires`: The expiration date string (e.g., "Wed, 21 Oct 2015 07:28:00 GMT").
34 ///
35 /// # Returns
36 /// - The CookieBuilder instance for method chaining.
37 pub fn expires<T>(&mut self, expires: T) -> &mut Self
38 where
39 T: Into<String>,
40 {
41 self.expires = Some(expires.into());
42 self
43 }
44
45 /// Sets the maximum age for the cookie in seconds.
46 ///
47 /// # Parameters
48 /// - `max_age`: The maximum age in seconds.
49 ///
50 /// # Returns
51 /// - The CookieBuilder instance for method chaining.
52 pub fn max_age(&mut self, max_age: i64) -> &mut Self {
53 self.max_age = Some(max_age);
54 self
55 }
56
57 /// Sets the domain for the cookie.
58 ///
59 /// # Parameters
60 /// - `domain`: The domain for the cookie.
61 ///
62 /// # Returns
63 /// - The CookieBuilder instance for method chaining.
64 pub fn domain<T>(&mut self, domain: T) -> &mut Self
65 where
66 T: Into<String>,
67 {
68 self.domain = Some(domain.into());
69 self
70 }
71
72 /// Sets the path for the cookie.
73 ///
74 /// # Parameters
75 /// - `path`: The path for the cookie.
76 ///
77 /// # Returns
78 /// - The CookieBuilder instance for method chaining.
79 pub fn path<T>(&mut self, path: T) -> &mut Self
80 where
81 T: Into<String>,
82 {
83 self.path = Some(path.into());
84 self
85 }
86
87 /// Sets the secure flag for the cookie.
88 ///
89 /// When set to true, the cookie will only be sent over HTTPS connections.
90 ///
91 /// # Returns
92 /// - The CookieBuilder instance for method chaining.
93 pub fn secure(&mut self) -> &mut Self {
94 self.secure = true;
95 self
96 }
97
98 /// Sets the HttpOnly flag for the cookie.
99 ///
100 /// When set to true, the cookie will be inaccessible to JavaScript.
101 ///
102 /// # Returns
103 /// - The CookieBuilder instance for method chaining.
104 pub fn http_only(&mut self) -> &mut Self {
105 self.http_only = true;
106 self
107 }
108
109 /// Sets the SameSite policy for the cookie.
110 ///
111 /// # Parameters
112 /// - `same_site`: The SameSite policy ("Strict", "Lax", or "None").
113 ///
114 /// # Returns
115 /// - The CookieBuilder instance for method chaining.
116 pub fn same_site<T>(&mut self, same_site: T) -> &mut Self
117 where
118 T: Into<String>,
119 {
120 self.same_site = Some(same_site.into());
121 self
122 }
123
124 /// Builds the cookie string representation.
125 ///
126 /// # Returns
127 /// - A formatted cookie string ready to be used in Set-Cookie headers.
128 pub fn build(&self) -> String {
129 if self.name.is_empty() {
130 return String::new();
131 }
132 let mut cookie_string: String = format!("{}={}", self.name, self.value);
133 if let Some(ref expires_value) = self.expires {
134 cookie_string.push_str("; Expires=");
135 cookie_string.push_str(expires_value);
136 }
137 if let Some(max_age_value) = self.max_age {
138 cookie_string.push_str("; Max-Age=");
139 cookie_string.push_str(&max_age_value.to_string());
140 }
141 if let Some(ref domain_value) = self.domain {
142 cookie_string.push_str("; Domain=");
143 cookie_string.push_str(domain_value);
144 }
145 if let Some(ref path_value) = self.path {
146 cookie_string.push_str("; Path=");
147 cookie_string.push_str(path_value);
148 }
149 if self.secure {
150 cookie_string.push_str("; Secure");
151 }
152 if self.http_only {
153 cookie_string.push_str("; HttpOnly");
154 }
155 if let Some(ref same_site_value) = self.same_site {
156 cookie_string.push_str("; SameSite=");
157 cookie_string.push_str(same_site_value);
158 }
159 cookie_string
160 }
161}