doe 1.1.85

doe is a powerful Rust crate designed to enhance development workflow by providing an extensive collection of useful macros and utility functions. It not only simplifies common tasks but also offers convenient features for clipboard management,robust cryptographic functions,keyboard input, and mouse interaction.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/// HTTP utilities for making requests and handling headers
/// 
/// Provides functionality for:
/// - Creating HTTP clients (sync and async)
/// - Building and parsing headers
/// - Making HTTP requests with custom headers
/// 
/// # Examples
/// 
/// ```
/// use doe::httprs;
/// 
/// // Create a client with custom headers
/// let headers = httprs::headers![
///     "Content-Type" => "application/json",
///     "Authorization" => "Bearer token"
/// ];
/// 
/// // Make a GET request
/// let client = httprs::sync_client_with_headers(headers);
/// let response = client.sync_get_bytes("https://api.example.com/data");
/// ```
#[allow(warnings)]
#[cfg(feature = "httprs")]
pub mod httprs {
    use std::ptr::replace;

    pub use reqwest::blocking::Client;
    pub use reqwest::header::HeaderMap;
    pub use reqwest::header::HeaderName;
    pub use reqwest::*;

    use crate::hashmap;
    use crate::DebugPrint;
    use crate::DynError;
    use crate::Str;

    /// Macro for creating HeaderMap from key-value pairs
    /// 
    /// # Examples
    /// 
    /// ```
    /// use doe::httprs;
    /// 
    /// let headers = httprs::headers![
    ///     "Content-Type" => "application/json",
    ///     "Authorization" => "Bearer token"
    /// ];
    /// ```
    #[macro_export]
    macro_rules! headers {
        ($($key:expr => $val:expr),*) => {
            {
            let mut headers = $crate::ctf::HeaderMap::new();
            $(
                let header_name = $crate::ctf::HeaderName::from_lowercase($key.to_lowercase().as_bytes()).unwrap();
                headers.insert(header_name,$val.parse().unwrap());
            )*
            headers
            }
        };
    }
    /// Trait for converting types to HTTP headers and URLs
    /// 
    /// Provides methods for:
    /// - Extracting headers from strings
    /// - Parsing URLs from strings
    /// - Combining both operations
    /// 
    /// # Examples
    /// 
    /// ```
    /// use doe::httprs::ToHeader;
    /// 
    /// let http_request = r#"
    /// GET /api/data HTTP/1.1
    /// Host: api.example.com
    /// Authorization: Bearer token
    /// "#;
    /// 
    /// let (url, headers) = http_request.url_and_headers();
    /// ```
    pub trait ToHeader {
        /// Convert the implementing type to HTTP headers
        fn to_headers(&self) -> HeaderMap;

        /// Extract URL from the implementing type
        fn to_url(&self) -> Option<String>;

        /// Extract both URL and headers from the implementing type
        /// 
        /// Returns a tuple containing:
        /// - Option<String>: The parsed URL if available
        /// - HeaderMap: The parsed headers
        fn url_and_headers(&self) -> (Option<String>, HeaderMap);
    }
    
    impl<T: ToString> ToHeader for T {
        /// Extracts both URL and headers from the implementing type
        /// 
        /// # Returns
        /// A tuple containing:
        /// - Option<String>: The parsed URL if available
        /// - HeaderMap: The parsed headers
        /// 
        /// # Examples
        /// 
        /// ```
        /// use doe::httprs::ToHeader;
        /// 
        /// let http_request = r#"
        /// GET /api/data HTTP/1.1
        /// Host: api.example.com
        /// Authorization: Bearer token
        /// "#;
        /// 
        /// let (url, headers) = http_request.url_and_headers();
        /// ```
        fn url_and_headers(&self) -> (Option<String>, HeaderMap) {
            let header_str = self.to_string();
            (header_str.to_url(), header_str.to_headers())
        }

        /// Extracts URL from the implementing type
        /// 
        /// Parses the first line of the HTTP request to extract the URL
        /// using the Host header and request path
        /// 
        /// # Returns
        /// Option<String> containing the parsed URL if available
        /// 
        /// # Examples
        /// 
        /// ```
        /// use doe::httprs::ToHeader;
        /// 
        /// let http_request = r#"
        /// GET /api/data HTTP/1.1
        /// Host: api.example.com
        /// Authorization: Bearer token
        /// "#;
        /// 
        /// let url = http_request.to_url();
        /// ```
        fn to_url(&self) -> Option<String> {
            let header_str = self.to_string();
            let mut header_str_split = header_str.split("\n");
            let mut hash_map: std::collections::HashMap<String, String> = hashmap!();
            header_str_split
                .clone()
                .filter(|s| !s.is_empty())
                .map(|s| {
                    s.to_string()
                        .trim()
                        .split(":")
                        .filter(|s| !s.is_empty())
                        .map(|s| s.to_string())
                        .collect()
                })
                .filter(|s: &Vec<String>| s.to_owned().len() > 1)
                .map(|s| {
                    let header_name = s.first().expect("get header_name error").to_string();
                    let header_val = &s[1..].join(":").to_string().replace("\"", "'");
                    vec![header_name, header_val.to_string()]
                })
                .collect::<Vec<Vec<_>>>()
                .iter()
                .for_each(|s| {
                    let header_name = s
                        .first()
                        .expect("get header_name error")
                        .to_string()
                        .to_ascii_lowercase();
                    let header_val = s.get(1).expect("get header_val error").to_string();
                    let _ = hash_map
                        .insert(header_name, header_val);
                });

            if let Some(fist_line) = header_str_split.filter(|s|!s.is_empty()).next() {
                if let Some(host) = hash_map.get("host") {
                    let path = &fist_line.trim().split(" ").collect::<Vec<_>>()[1].trim();
                    let url = host.to_string().push_back(path.to_string());
                    Some(url.trim().to_string())
                } else {
                    None
                }
            } else {
                None
            }
        }

        /// Converts the implementing type to HTTP headers
        /// 
        /// Parses the input string to extract key-value pairs and
        /// converts them into a HeaderMap
        /// 
        /// # Returns
        /// HeaderMap containing the parsed headers
        /// 
        /// # Examples
        /// 
        /// ```
        /// use doe::httprs::ToHeader;
        /// 
        /// let http_request = r#"
        /// GET /api/data HTTP/1.1
        /// Host: api.example.com
        /// Authorization: Bearer token
        /// "#;
        /// 
        /// let headers = http_request.to_headers();
        /// ```
        fn to_headers(&self) -> HeaderMap {
            let header_str = self.to_string();
            let header_str_split = header_str.split("\n");
            let head_vec = header_str_split
                .filter(|s| !s.is_empty())
                .map(|s| {
                    s.to_string()
                        .trim()
                        .split(":")
                        .filter(|s| !s.is_empty())
                        .map(|s| s.to_string())
                        .collect()
                })
                .filter(|s: &Vec<String>| s.to_owned().len() > 1)
                .map(|s| {
                    let header_name = s.first().expect("get header_name error").to_string();
                    let header_val = &s[1..].join(":").to_string().replace("\"", "'");
                    vec![header_name, header_val.to_string()]
                })
                .collect::<Vec<Vec<_>>>();
            let mut headers = HeaderMap::new();
            for kv in head_vec.iter() {
                let mut kv = kv.iter();
                let key = kv.next().unwrap();
                let val = kv.next().unwrap();
                let header_name =
                    HeaderName::from_lowercase(key.to_lowercase().as_bytes()).unwrap();
                headers.insert(header_name, val.parse().unwrap());
            }
            headers
        }
    }

    /// Trait for asynchronous HTTP client operations
    /// 
    /// Provides methods for making asynchronous HTTP requests and receiving raw byte responses
    /// 
    /// # Examples
    /// 
    /// ```
    /// use doe::httprs::{ClientAsyncSend, async_client};
    /// 
    /// #[tokio::main]
    /// async fn main() {
    ///     let client = async_client().await;
    ///     
    ///     // Make GET request
    ///     let response = client.async_get_bytes("https://api.example.com/data").await;
    ///     
    ///     // Make POST request
    ///     let data = b"example data";
    ///     let response = client.async_post_bytes("https://api.example.com/data", data).await;
    /// }
    /// ```
    pub trait ClientAsyncSend {
        /// Make an asynchronous POST request and return the raw response bytes
        /// 
        /// # Arguments
        /// * `url` - The target URL as a string
        /// * `body` - The request body as a byte slice
        /// 
        /// # Returns
        /// Vec<u8> containing the raw response bytes
        async fn async_post_bytes(&self, url: impl ToString, body: &[u8]) -> Vec<u8>;

        /// Make an asynchronous GET request and return the raw response bytes
        /// 
        /// # Arguments
        /// * `url` - The target URL as a string
        /// 
        /// # Returns
        /// Vec<u8> containing the raw response bytes
        async fn async_get_bytes(&self, url: impl ToString) -> Vec<u8>;
    }

    impl ClientAsyncSend for reqwest::Client {
        async fn async_post_bytes(&self, url: impl ToString, body: &[u8]) -> Vec<u8> {
            let mut resp = self
                .post(url.to_string())
                .body(body.to_vec())
                .send()
                .await
                .unwrap();

            resp.bytes().await.unwrap().to_vec()
        }

        async fn async_get_bytes(&self, url: impl ToString) -> Vec<u8> {
            let mut resp = self.get(url.to_string()).send().await.unwrap();
            resp.bytes().await.unwrap().to_vec()
        }
    }

    /// Trait for synchronous HTTP client operations
    /// 
    /// Provides methods for making synchronous HTTP requests and receiving raw byte responses
    /// 
    /// # Examples
    /// 
    /// ```
    /// use doe::httprs::{ClientSyncSend, sync_client};
    /// 
    /// fn main() {
    ///     let client = sync_client();
    ///     
    ///     // Make GET request
    ///     let response = client.sync_get_bytes("https://api.example.com/data");
    ///     
    ///     // Make POST request
    ///     let data = b"example data";
    ///     let response = client.sync_post_bytes("https://api.example.com/data", data);
    /// }
    /// ```
    pub trait ClientSyncSend {
        /// Make a synchronous POST request and return the raw response bytes
        /// 
        /// # Arguments
        /// * `url` - The target URL as a string
        /// * `body` - The request body as a byte slice
        /// 
        /// # Returns
        /// Vec<u8> containing the raw response bytes
        fn sync_post_bytes(&self, url: impl ToString, body: &[u8]) -> Vec<u8>;

        /// Make a synchronous GET request and return the raw response bytes
        /// 
        /// # Arguments
        /// * `url` - The target URL as a string
        /// 
        /// # Returns
        /// Vec<u8> containing the raw response bytes
        fn sync_get_bytes(&self, url: impl ToString) -> Vec<u8>;
    }

    impl ClientSyncSend for reqwest::blocking::Client {
        fn sync_post_bytes(&self, url: impl ToString, body: &[u8]) -> Vec<u8> {
            let mut resp = self
                .post(url.to_string())
                .body(body.to_vec())
                .send()
                .unwrap();

            let mut body = vec![];
            resp.copy_to(&mut body).unwrap();
            body
        }
        fn sync_get_bytes(&self, url: impl ToString) -> Vec<u8> {
            let mut resp = self.get(url.to_string()).send().unwrap();
            let mut body = vec![];
            resp.copy_to(&mut body).unwrap();
            body
        }
    }
    /// Creates a new asynchronous HTTP client with default settings
    /// 
    /// # Returns
    /// reqwest::Client instance
    /// 
    /// # Examples
    /// 
    /// ```
    /// use doe::httprs;
    /// 
    /// #[tokio::main]
    /// async fn main() {
    ///     let client = httprs::async_client().await;
    /// }
    /// ```
    pub async fn async_client() -> reqwest::Client {
        let client_builder = reqwest::ClientBuilder::new();
        let client = client_builder.build().unwrap();
        client
    }

    /// Creates a new synchronous HTTP client with default settings
    /// 
    /// # Returns
    /// reqwest::blocking::Client instance
    /// 
    /// # Examples
    /// 
    /// ```
    /// use doe::httprs;
    /// 
    /// fn main() {
    ///     let client = httprs::sync_client();
    /// }
    /// ```
    pub fn sync_client() -> reqwest::blocking::Client {
        let client_builder = reqwest::blocking::ClientBuilder::new();
        let client = client_builder.build().unwrap();
        client
    }

    /// Creates a new asynchronous HTTP client with custom headers
    /// 
    /// # Arguments
    /// * `headers` - HeaderMap containing the custom headers
    /// 
    /// # Returns
    /// reqwest::Client instance with configured headers
    /// 
    /// # Examples
    /// 
    /// ```
    /// use doe::httprs::{async_client_with_headers, headers};
    /// 
    /// #[tokio::main]
    /// async fn main() {
    ///     let headers = headers![
    ///         "Content-Type" => "application/json",
    ///         "Authorization" => "Bearer token"
    ///     ];
    ///     let client = async_client_with_headers(headers).await;
    /// }
    /// ```
    pub async fn async_client_with_headers(headers: reqwest::header::HeaderMap) -> reqwest::Client {
        let client_builder = reqwest::ClientBuilder::new();
        let client = client_builder.default_headers(headers).build().unwrap();
        client
    }

    /// Creates a new asynchronous HTTP client builder for custom configuration
    /// 
    /// # Returns
    /// reqwest::ClientBuilder instance
    /// 
    /// # Examples
    /// 
    /// ```
    /// use doe::httprs;
    /// 
    /// #[tokio::main]
    /// async fn main() {
    ///     let client = httprs::async_client_builder()
    ///         .await
    ///         .danger_accept_invalid_certs(true)
    ///         .build()
    ///         .unwrap();
    /// }
    /// ```
    pub async fn async_client_builder() -> reqwest::ClientBuilder {
        reqwest::ClientBuilder::new()
    }

    /// Creates a new synchronous HTTP client with custom headers
    /// 
    /// # Arguments
    /// * `headers` - HeaderMap containing the custom headers
    /// 
    /// # Returns
    /// reqwest::blocking::Client instance with configured headers
    /// 
    /// # Examples
    /// 
    /// ```
    /// use doe::httprs::{sync_client_with_headers, headers};
    /// 
    /// fn main() {
    ///     let headers = headers![
    ///         "Content-Type" => "application/json",
    ///         "Authorization" => "Bearer token"
    ///     ];
    ///     let client = sync_client_with_headers(headers);
    /// }
    /// ```
    pub fn sync_client_with_headers(
        headers: reqwest::header::HeaderMap,
    ) -> reqwest::blocking::Client {
        let client_builder = reqwest::blocking::ClientBuilder::new();
        let client = client_builder.default_headers(headers).build().unwrap();
        client
    }

    /// Creates a new synchronous HTTP client builder for custom configuration
    /// 
    /// # Returns
    /// reqwest::blocking::ClientBuilder instance
    /// 
    /// # Examples
    /// 
    /// ```
    /// use doe::httprs;
    /// 
    /// fn main() {
    ///     let client = httprs::sync_client_builder()
    ///         .danger_accept_invalid_certs(true)
    ///         .build()
    ///         .unwrap();
    /// }
    /// ```
    pub fn sync_client_builder() -> reqwest::blocking::ClientBuilder {
        reqwest::blocking::ClientBuilder::new()
    }

    /// Re-export of reqwest::Method for convenience
    pub use reqwest::Method;
}





#[cfg(feature = "httprs")]
pub use httprs::*;