rusty_format/
client.rs

1use crate::*;
2
3impl<'reesponse>
4    ClientResponse<
5        'reesponse,
6        MethodNotPresent,
7        PathNotPresent,
8        HostNotPresent,
9        AuthorizationNotPresent,
10        ContentTypeNotPresent,
11        ContentNotPresent,
12    >
13{
14    pub fn new() -> Self {
15        Self {
16            method: Vec::new(),
17            path: Vec::new(),
18            host: Vec::new(),
19            authorization: Vec::new(),
20            content_type: Vec::new(),
21            content: Vec::new(),
22            methodstate: std::marker::PhantomData,
23            pathstate: std::marker::PhantomData,
24            hoststate: std::marker::PhantomData,
25            authorizationstate: std::marker::PhantomData,
26            contenttypestate: std::marker::PhantomData,
27            contentstate: std::marker::PhantomData,
28        }
29    }
30    pub fn method(
31        mut self,
32        method: &'reesponse str,
33    ) -> ClientResponse<
34        'reesponse,
35        MethodPresent,
36        PathNotPresent,
37        HostNotPresent,
38        AuthorizationNotPresent,
39        ContentTypeNotPresent,
40        ContentNotPresent,
41    > {
42        self.method.push(method);
43        ClientResponse {
44            method: self.method,
45            path: self.path,
46            host: self.host,
47            authorization: self.authorization,
48            content_type: self.content_type,
49            content: self.content,
50            methodstate: std::marker::PhantomData,
51            pathstate: std::marker::PhantomData,
52            hoststate: std::marker::PhantomData,
53            authorizationstate: std::marker::PhantomData,
54            contenttypestate: std::marker::PhantomData,
55            contentstate: std::marker::PhantomData,
56        }
57    }
58}
59
60impl<'response>
61    ClientResponse<
62        'response,
63        MethodPresent,
64        PathNotPresent,
65        HostNotPresent,
66        AuthorizationNotPresent,
67        ContentTypeNotPresent,
68        ContentNotPresent,
69    >
70{
71    pub fn path(
72        mut self,
73        path: &'response str,
74    ) -> ClientResponse<
75        'response,
76        MethodPresent,
77        PathPresent,
78        HostNotPresent,
79        AuthorizationNotPresent,
80        ContentTypeNotPresent,
81        ContentNotPresent,
82    > {
83        self.path.push(path);
84        ClientResponse {
85            method: self.method,
86            path: self.path,
87            host: self.host,
88            authorization: self.authorization,
89            content_type: self.content_type,
90            content: self.content,
91            methodstate: std::marker::PhantomData,
92            pathstate: std::marker::PhantomData,
93            hoststate: std::marker::PhantomData,
94            authorizationstate: std::marker::PhantomData,
95            contenttypestate: std::marker::PhantomData,
96            contentstate: std::marker::PhantomData,
97        }
98    }
99}
100
101impl<'response>
102    ClientResponse<
103        'response,
104        MethodPresent,
105        PathPresent,
106        HostNotPresent,
107        AuthorizationNotPresent,
108        ContentTypeNotPresent,
109        ContentNotPresent,
110    >
111{
112    pub fn host(
113        mut self,
114        host: &'response str,
115    ) -> ClientResponse<
116        'response,
117        MethodPresent,
118        PathPresent,
119        HostPresent,
120        AuthorizationNotPresent,
121        ContentTypeNotPresent,
122        ContentNotPresent,
123    > {
124        self.host.push(host);
125        ClientResponse {
126            method: self.method,
127            path: self.path,
128            host: self.host,
129            authorization: self.authorization,
130            content_type: self.content_type,
131            content: self.content,
132            methodstate: std::marker::PhantomData,
133            pathstate: std::marker::PhantomData,
134            hoststate: std::marker::PhantomData,
135            authorizationstate: std::marker::PhantomData,
136            contenttypestate: std::marker::PhantomData,
137            contentstate: std::marker::PhantomData,
138        }
139    }
140}
141
142impl<'response>
143    ClientResponse<
144        'response,
145        MethodPresent,
146        PathPresent,
147        HostPresent,
148        AuthorizationNotPresent,
149        ContentTypeNotPresent,
150        ContentNotPresent,
151    >
152{
153    pub fn authorization(
154        mut self,
155        authorization: Option<&'response str>,
156    ) -> ClientResponse<
157        'response,
158        MethodPresent,
159        PathPresent,
160        HostPresent,
161        AuthorizationPresent,
162        ContentTypeNotPresent,
163        ContentNotPresent,
164    > {
165        self.authorization.push(authorization);
166        ClientResponse {
167            method: self.method,
168            path: self.path,
169            host: self.host,
170            authorization: self.authorization,
171            content_type: self.content_type,
172            content: self.content,
173            methodstate: std::marker::PhantomData,
174            pathstate: std::marker::PhantomData,
175            hoststate: std::marker::PhantomData,
176            authorizationstate: std::marker::PhantomData,
177            contenttypestate: std::marker::PhantomData,
178            contentstate: std::marker::PhantomData,
179        }
180    }
181}
182
183impl<'response>
184    ClientResponse<
185        'response,
186        MethodPresent,
187        PathPresent,
188        HostPresent,
189        AuthorizationPresent,
190        ContentTypeNotPresent,
191        ContentNotPresent,
192    >
193{
194    pub fn content_type(
195        mut self,
196        content_type: ContentType,
197    ) -> ClientResponse<
198        'response,
199        MethodPresent,
200        PathPresent,
201        HostPresent,
202        AuthorizationPresent,
203        ContentTypePresent,
204        ContentNotPresent,
205    > {
206        match content_type {
207            ContentType::TextPlain => self.content_type.push("text/plain"),
208            ContentType::TextHtml => self.content_type.push("text/html"),
209            ContentType::TextCss => self.content_type.push("text/css"),
210            ContentType::TextJavaScript => self.content_type.push("text/javascript"),
211            ContentType::TextXml => self.content_type.push("text/xml"),
212            ContentType::ApplicationJson => self.content_type.push("application/json"),
213            ContentType::ApplicationXml => self.content_type.push("application/xml"),
214            ContentType::ApplicationXWwwFormUrlencoded => {
215                self.content_type.push("application/x-www-form-urlencoded")
216            }
217            ContentType::ApplicationOctetStream => {
218                self.content_type.push("application/octet-stream")
219            }
220            ContentType::ApplicationPdf => self.content_type.push("application/pdf"),
221            ContentType::ApplicationZip => self.content_type.push("application/zip"),
222            ContentType::ApplicationJavaScript => self.content_type.push("application/javascript"),
223            ContentType::MultipartFormData => self.content_type.push("multipart/form-data"),
224            ContentType::MultipartAlternative => self.content_type.push("multipart/alternative"),
225            ContentType::MultipartRelated => self.content_type.push("multipart/related"),
226            ContentType::ImageJpeg => self.content_type.push("image/jpeg"),
227            ContentType::ImagePng => self.content_type.push("image/png"),
228            ContentType::ImageGif => self.content_type.push("image/gif"),
229            ContentType::ImageSvgXml => self.content_type.push("image/svg+xml"),
230            ContentType::AudioMpeg => self.content_type.push("audio/mpeg"),
231            ContentType::AudioOgg => self.content_type.push("audio/ogg"),
232            ContentType::VideoMp4 => self.content_type.push("video/mp4"),
233            ContentType::VideoOgg => self.content_type.push("video/ogg"),
234            ContentType::FontWoff => self.content_type.push("font/woff"),
235            ContentType::FontWoff2 => self.content_type.push("font/woff2"),
236            ContentType::ApplicationFontWoff => self.content_type.push("application/font-woff"),
237            ContentType::ApplicationMsExcel => self.content_type.push("application/vnd.ms-excel"),
238            ContentType::ApplicationMsWord => self.content_type.push("application/msword"),
239            ContentType::ApplicationRtf => self.content_type.push("application/rtf"),
240        }
241        ClientResponse {
242            method: self.method,
243            path: self.path,
244            host: self.host,
245            authorization: self.authorization,
246            content_type: self.content_type,
247            content: self.content,
248            methodstate: std::marker::PhantomData,
249            pathstate: std::marker::PhantomData,
250            hoststate: std::marker::PhantomData,
251            authorizationstate: std::marker::PhantomData,
252            contenttypestate: std::marker::PhantomData,
253            contentstate: std::marker::PhantomData,
254        }
255    }
256}
257
258impl<'response>
259    ClientResponse<
260        'response,
261        MethodPresent,
262        PathPresent,
263        HostPresent,
264        AuthorizationPresent,
265        ContentTypePresent,
266        ContentNotPresent,
267    >
268{
269    pub fn content(
270        mut self,
271        content: &'response str,
272    ) -> ClientResponse<
273        'response,
274        MethodPresent,
275        PathPresent,
276        HostPresent,
277        AuthorizationPresent,
278        ContentTypePresent,
279        ContentPresent,
280    > {
281        self.content.push(content);
282        ClientResponse {
283            method: self.method,
284            path: self.path,
285            host: self.host,
286            authorization: self.authorization,
287            content_type: self.content_type,
288            content: self.content,
289            methodstate: std::marker::PhantomData,
290            pathstate: std::marker::PhantomData,
291            hoststate: std::marker::PhantomData,
292            authorizationstate: std::marker::PhantomData,
293            contenttypestate: std::marker::PhantomData,
294            contentstate: std::marker::PhantomData,
295        }
296    }
297}
298
299impl<'response>
300    ClientResponse<
301        'response,
302        MethodPresent,
303        PathPresent,
304        HostPresent,
305        AuthorizationPresent,
306        ContentTypePresent,
307        ContentPresent,
308    >
309{
310    pub fn build(self) -> String {
311        let mut request = String::new();
312        for (id, method) in self.method.iter().enumerate() {
313            request.push_str(&format!("{} ", method));
314            request.push_str(&format!("{} ", self.path[id]));
315            request.push_str(&format!("HTTP/1.1\r\n"));
316            request.push_str(&format!("Host: {}\r\n", self.host[id]));
317            if let Some(authorization) = self.authorization[id] {
318                request.push_str(&format!("Authorization: {}\r\n", authorization));
319            }
320            request.push_str(&format!("Content-Type: {}\r\n", self.content_type[id]));
321            request.push_str(&format!("Content-Length: {}\r\n\r\n", self.content.len()));
322            request.push_str(&format!("{}", self.content[id]));
323            // let requestt = format!("{} {} HTTP/1.1\r\nHost: {}\r\nAuthorization: {}\r\nContent-Type: {}\r\nContent-Length: {}\r\n\r\n{}",method,self.path[id],self.host[id],self.authorization[id],self.content_type[id],self.content[id].len(),self.content[id]);
324            // request.push_str(&requestt);
325        }
326        request
327    }
328}