1pub struct Cors<'cors, OriginState, MethodState, HeaderState, CredentialState, MaxAgeState> {
2 allow_origin: &'cors str,
3 allow_methods: &'cors [&'cors str],
4 allow_headers: &'cors [&'cors str],
5 allow_credential: bool,
6 max_age: u64,
7 originstate: std::marker::PhantomData<OriginState>,
8 methodstate: std::marker::PhantomData<MethodState>,
9 headersstate: std::marker::PhantomData<HeaderState>,
10 credentialstate: std::marker::PhantomData<CredentialState>,
11 maxagestate: std::marker::PhantomData<MaxAgeState>,
12}
13
14pub struct OriginPresent;
15pub struct OriginNotPresent;
16
17pub struct MethodsPresent;
18pub struct MethodNotPresent;
19
20pub struct HeaderPresent;
21pub struct HeaderNotPresent;
22
23pub struct CredentialPresent;
24
25pub struct CredentialNotPresent;
26
27pub struct MaxAgePresent;
28pub struct MaxAgeNotPresent;
29
30impl<'cors>
31 Cors<
32 'cors,
33 OriginNotPresent,
34 MaxAgeNotPresent,
35 HeaderNotPresent,
36 CredentialNotPresent,
37 MaxAgeNotPresent,
38 >
39{
40 pub fn new() -> Self {
41 Self {
42 allow_origin: "",
43 allow_methods: &[""],
44 allow_headers: &[""],
45 allow_credential: false,
46 max_age: 0,
47 originstate: std::marker::PhantomData,
48 methodstate: std::marker::PhantomData,
49 headersstate: std::marker::PhantomData,
50 credentialstate: std::marker::PhantomData,
51 maxagestate: std::marker::PhantomData,
52 }
53 }
54 pub fn allow_origin(
55 mut self,
56 allow_origin: &'cors str,
57 ) -> Cors<
58 'cors,
59 OriginPresent,
60 MethodNotPresent,
61 HeaderNotPresent,
62 CredentialNotPresent,
63 MaxAgeNotPresent,
64 > {
65 self.allow_origin = allow_origin;
66 Cors {
67 allow_origin: self.allow_origin,
68 allow_methods: self.allow_methods,
69 allow_headers: self.allow_headers,
70 allow_credential: self.allow_credential,
71 max_age: self.max_age,
72 originstate: std::marker::PhantomData,
73 methodstate: std::marker::PhantomData,
74 headersstate: std::marker::PhantomData,
75 credentialstate: std::marker::PhantomData,
76 maxagestate: std::marker::PhantomData,
77 }
78 }
79}
80
81impl<'method>
82 Cors<
83 'method,
84 OriginPresent,
85 MethodNotPresent,
86 HeaderNotPresent,
87 CredentialNotPresent,
88 MaxAgeNotPresent,
89 >
90{
91 pub fn allow_methods(
92 mut self,
93 allow_methods: &'method [&'method str],
94 ) -> Cors<
95 'method,
96 OriginPresent,
97 MethodsPresent,
98 HeaderNotPresent,
99 CredentialNotPresent,
100 MaxAgeNotPresent,
101 > {
102 self.allow_methods = allow_methods;
103 Cors {
104 allow_origin: self.allow_origin,
105 allow_methods: self.allow_methods,
106 allow_headers: self.allow_headers,
107 allow_credential: self.allow_credential,
108 max_age: self.max_age,
109 originstate: std::marker::PhantomData,
110 methodstate: std::marker::PhantomData,
111 headersstate: std::marker::PhantomData,
112 credentialstate: std::marker::PhantomData,
113 maxagestate: std::marker::PhantomData,
114 }
115 }
116}
117
118impl<'header>
119 Cors<
120 'header,
121 OriginPresent,
122 MethodsPresent,
123 HeaderNotPresent,
124 CredentialNotPresent,
125 MaxAgeNotPresent,
126 >
127{
128 pub fn allow_headers(
129 mut self,
130 allow_headers: &'header [&'header str],
131 ) -> Cors<
132 'header,
133 OriginPresent,
134 MethodsPresent,
135 HeaderPresent,
136 CredentialNotPresent,
137 MaxAgeNotPresent,
138 > {
139 self.allow_headers = allow_headers;
140 Cors {
141 allow_origin: self.allow_origin,
142 allow_methods: self.allow_methods,
143 allow_headers: self.allow_headers,
144 allow_credential: self.allow_credential,
145 max_age: self.max_age,
146 originstate: std::marker::PhantomData,
147 methodstate: std::marker::PhantomData,
148 headersstate: std::marker::PhantomData,
149 credentialstate: std::marker::PhantomData,
150 maxagestate: std::marker::PhantomData,
151 }
152 }
153}
154
155impl<'credential>
156 Cors<
157 'credential,
158 OriginPresent,
159 MethodsPresent,
160 HeaderPresent,
161 CredentialNotPresent,
162 MaxAgeNotPresent,
163 >
164{
165 pub fn allow_credential(
166 mut self,
167 allow_credential: bool,
168 ) -> Cors<
169 'credential,
170 OriginPresent,
171 MethodsPresent,
172 HeaderPresent,
173 CredentialPresent,
174 MaxAgeNotPresent,
175 > {
176 self.allow_credential = allow_credential;
177 Cors {
178 allow_origin: self.allow_origin,
179 allow_methods: self.allow_methods,
180 allow_headers: self.allow_headers,
181 allow_credential: self.allow_credential,
182 max_age: self.max_age,
183 originstate: std::marker::PhantomData,
184 methodstate: std::marker::PhantomData,
185 headersstate: std::marker::PhantomData,
186 credentialstate: std::marker::PhantomData,
187 maxagestate: std::marker::PhantomData,
188 }
189 }
190}
191
192impl<'max>
193 Cors<'max, OriginPresent, MethodsPresent, HeaderPresent, CredentialPresent, MaxAgeNotPresent>
194{
195 pub fn max_age(
196 mut self,
197 max_age: u64,
198 ) -> Cors<'max, OriginPresent, MethodsPresent, HeaderPresent, CredentialPresent, MaxAgePresent>
199 {
200 self.max_age = max_age;
201 Cors {
202 allow_origin: self.allow_origin,
203 allow_methods: self.allow_methods,
204 allow_headers: self.allow_headers,
205 allow_credential: self.allow_credential,
206 max_age: self.max_age,
207 originstate: std::marker::PhantomData,
208 methodstate: std::marker::PhantomData,
209 headersstate: std::marker::PhantomData,
210 credentialstate: std::marker::PhantomData,
211 maxagestate: std::marker::PhantomData,
212 }
213 }
214}
215
216impl<'max>
217 Cors<'max, OriginPresent, MethodsPresent, HeaderPresent, CredentialPresent, MaxAgePresent>
218{
219 pub fn build(self) -> String {
220 let mut headers = String::new();
221 for header in self.allow_headers {
222 headers.push_str(&format!("{},", header));
223 }
224 let mut method = String::new();
225 for methods in self.allow_methods {
226 method.push_str(&format!("{},", methods));
227 }
228 let method = method.trim_end_matches(",");
229 let headers = headers.trim_end_matches(",");
230 let cors = format!(
231 "Access-Control-Allow-Origin: {}\r\n\
232 Access-Control-Allow-Methods: {}\r\n\
233 Access-Control-Allow-Headers: {}\r\n\
234 Access-Control-Allow-Credentials: true\r\n\
235 Access-Control-Max-Age: {}",
236 self.allow_origin, method, headers, self.max_age
237 );
238 cors
239 }
240}