cans 1.5.0

An elegant and lightweight Rust-based literal template engine for managing web content, enhanced with a world module for streamlined regional and city information, as well as robust MIME type management.
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use std::collections::HashMap;

/// ### set_mime_types
///
/// Initializes a HashMap with common file extensions and their corresponding MIME types.
///
/// The `set_mime_types` function initializes a collection of MIME types and their
/// associated file extensions, returning this mapping as a HashMap. It serves as
/// the starting point for managing MIME types in applications, providing a comprehensive
/// list of common file extensions and their recognized types.
///
/// ### Returns
/// 
/// - Returns a `HashMap<String, String>` containing common file extensions as keys
///   and their corresponding MIME types as values.
///
/// Examples
///
/// ```rust
/// use cans::mime::{display_mime_types, set_mime_types};
///
/// fn main() {
///    // Create a new HashMap and initialize it with default MIME types
///    let mime_types = set_mime_types();
///    
///    println!("Initial MIME types:");
///    display_mime_types(&mime_types);
/// }
/// ```
///
/// ### Output
///
/// ```terminal
/// Initial MIME types:
/// Extension: application - MIME Type: application/octet-stream
/// Extension: mkv - MIME Type: video/x-matroska
/// Extension: mpg - MIME Type: video/mpeg
/// Extension: mov - MIME Type: video/quicktime
/// Extension: gz - MIME Type: application/gzip
/// Extension: ics - MIME Type: text/calendar
/// Extension: txt - MIME Type: text/plain
/// ...
/// ```
///
/// ### Usage Context
/// 
/// This function is typically called at the start of an application to establish
/// default MIME type mappings, ensuring that subsequent operations on file types
/// can utilize a reliable set of MIME definitions.
///
/// <small>End Fun Doc</small>
pub fn set_mime_types() -> HashMap<String, String> {
    let mime_types: HashMap<String, String> = [
        ("html", "text/html"),
        ("ejs", "text/html"),
        ("css", "text/css"),
        ("scss", "text/scss"),
        ("less", "text/css"),
        ("csv", "text/csv"),
        ("rs", "text/plain"),
        ("plain", "text/plain"),
        ("txt", "text/plain"),
        ("markdown", "text/markdown"),
        ("md", "text/markdown"),
        ("xml", "text/xml"),
        ("yaml", "text/yaml"),
        ("wat", "text/wat"),
        ("py", "text/x-python"),
        ("log", "text/plain"),
        ("ics", "text/calendar"),
        ("vcard", "text/vcard"),
        ("php", "text/x-php"),
        ("conf", "text/plain"),
        ("init", "text/plain"),
        ("png", "image/png"),
        ("jpg", "image/jpeg"),
        ("jpeg", "image/jpeg"),
        ("gif", "image/gif"),
        ("svg", "image/svg+xml"),
        ("svgz", "image/svg+xml"),
        ("bmp", "image/bmp"),
        ("tiff", "image/tiff"),
        ("ico", "image/x-icon"),
        ("webp", "image/webp"),
        ("woff", "font/woff"),
        ("woff2", "font/woff2"),
        ("ttf", "font/ttf"),
        ("otf", "font/otf"),
        ("eot", "application/vnd.ms-fontobject"),
        ("js", "application/javascript"),
        ("ts", "application/typescript"),
        ("xml", "application/xml"),
        ("json", "application/json"),
        ("geojson", "application/geo+json"),
        ("map", "application/json-patch+json"),
        ("jsonld", "application/ld+json"),
        ("binary", "application/octet-stream"),
        ("tar", "application/x-tar"),
        ("so", "application/octet-stream"),
        ("so", "application/x-sharedlib"),
        ("deb", "application/vnd.debian.binary-package"),
        ("bash", "application/x-shellscript"),
        ("php", "application/x-httpd-php"),
        ("db", "application/x-database"),
        ("sql", "application/sql"),
        ("pdf", "application/pdf"),
        ("zip", "application/zip"),
        ("gz", "application/gzip"),
        ("epub", "application/epub+zip"),
        ("toml", "application/toml"),
        ("xhtml", "application/xhtml+xml"),
        ("rss", "application/rss+xml"),
        ("atom", "application/atom+xml"),
        ("graphql", "application/graphql"),
        ("form", "application/x-www-form-urlencoded"),
        ("srt", "application/x-subrip"),
        ("rtf", "application/rtf"),
        ("kml", "application/vnd.google-earth.kml+xml"),
        ("kmz", "application/vnd.google-earth.kmz"),
        ("sh", "application/x-sh"),
        ("bin", "application/octet-stream"),
        ("exe", "application/octet-stream"),
        ("dll", "application/octet-stream"),
        ("class", "application/java-vm"),
        ("jar", "application/java-archive"),
        ("wasm", "application/wasm"),
        ("wat", "application/wasm"),
        ("conf", "application/x-configuration"),
        ("init", "application/x-initialization"),
        ("pem", "application/x-x509-ca-cert"),
        ("p12", "application/x-pkcs12"),
        ("p7b", "application/x-pkcs7-certificates"),
        ("p7c", "application/pkcs7-mime"),
        ("pot", "application/vnd.ms-powerpoint"),
        ("doc", "application/msword"),
        (
            "docx",
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
        ),
        ("xls", "application/vnd.ms-excel"),
        (
            "xlsx",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        ),
        ("ppt", "application/vnd.ms-powerpoint"),
        (
            "pptx",
            "application/vnd.openxmlformats-officedocument.presentationml.presentation",
        ),
        (
            "dotx",
            "application/vnd.openxmlformats-officedocument.wordtemplate",
        ),
        ("xla", "application/vnd.ms-excel"),
        ("xlt", "application/vnd.ms-excel"),
        (
            "xltx",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
        ),
        ("bat", "application/bat"),
        ("pub", "application/x-mspublisher"),
        ("xps", "application/vnd.ms-xpsdocument"),
        ("msg", "application/vnd.ms-outlook"),
        ("dot", "application/msword"),
        ("wps", "application/vnd.ms-works"),
        ("odt", "application/vnd.oasis.opendocument.text"),
        ("ods", "application/vnd.oasis.opendocument.spreadsheet"),
        ("apk", "application/vnd.android.package-archive"),
        ("iot", "application/x-iot-file"),
        ("iot", "application/x-iot-data"),
        ("application", "application/octet-stream"),
        ("wav", "audio/wav"),
        ("mp3", "audio/mpeg"),
        ("aac", "audio/aac"),
        ("ogg", "audio/ogg"),
        ("m4a", "audio/mp4a-latm"),
        ("flac", "audio/flac"),
        ("midi", "audio/midi"),
        ("mid", "audio/midi"),
        ("mp4", "video/mp4"),
        ("webm", "video/webm"),
        ("mov", "video/quicktime"),
        ("avi", "video/x-msvideo"),
        ("flv", "video/x-flv"),
        ("mkv", "video/x-matroska"),
        ("mpg", "video/mpeg"),
        ("mpeg", "video/mpeg"),
        ("mpa", "video/x-mpeg"),
    ]
    .iter()
    .map(|&(ext, mime)| (ext.to_string(), mime.to_string()))
    .collect();

    mime_types
}

/// ### manage_mime_types
///
/// A helper function to manage MIME types by converting an array of tuples into a HashMap.
///
/// This function takes a slice of tuples containing file extensions and their
/// corresponding MIME types as input and returns a HashMap. It facilitates
/// the creation of various MIME type mappings in a consistent manner, making it
/// easier to insert or remove MIME type mappings as needed.
///
/// ### Parameters
/// 
/// - `mime_types`: A slice of tuples, where each tuple contains a file extension
///   and its respective MIME type.
///
/// ### Returns
/// 
/// - Returns a `HashMap<String, String>` containing the provided file extensions
///   as keys and their corresponding MIME types as values.
///
/// <small>End Fun Doc</small>
pub fn manage_mime_types(mime_types: &[(&str, &str)]) -> HashMap<String, String> {
    mime_types
        .iter()
        .map(|&(ext, mime)| (ext.to_string(), mime.to_string()))
        .collect()
}

/// ### insert_mime_type
///
/// Inserts a single MIME type into an existing HashMap for the given file extension.
///
/// The `insert_mime_type` function allows users to extend the existing list of MIME
/// types dynamically. By providing a file extension and its corresponding MIME type,
/// this function can be used to add new entries as needed.
///
/// ### Parameters
/// 
/// - `mime_types`: A mutable reference to a `HashMap<String, String>` of MIME types.
/// - `extension`: A string slice (`&str`) representing the file extension to be added.
/// - `mime_type`: A string slice (`&str`) representing the corresponding MIME type.
///
/// ### Examples
/// 
/// ```rust
/// use cans::mime::{
///     display_mime_types,
///     insert_mime_type,
///     set_mime_types,
/// };
///
/// fn main() {
///    // Create a new HashMap and initialize it with default MIME types
///    let mut mime_types = set_mime_types();
///
///    // Insert a new MIME type
///    insert_mime_type(&mut mime_types, "txt", "text/plain");
///    println!("\nAfter inserting .txt MIME type:");
///    display_mime_types(&mime_types);
/// }
/// ```
///
/// ### Output
///
/// ```terminal
/// After inserting .txt MIME type:
/// Extension: log - MIME Type: text/plain
/// ...
/// Extension: txt - MIME Type: text/plain
/// ...
/// ```
///
/// ### Usage Context
/// 
/// This function can be useful when dealing with custom file types or when extending
/// the MIME type definitions beyond the defaults provided in `set_mime_types`.
///
/// <small>End Fun Doc</small>
pub fn insert_mime_type(
    mime_types: &mut HashMap<String, String>,
    extension: &str,
    mime_type: &str,
) {
    mime_types.insert(extension.to_string(), mime_type.to_string());
}

/// ### remove_mime_type
///
/// Removes a MIME type for a specified file extension from the HashMap.
///
/// The `remove_mime_type` function is utilized to delete existing MIME definitions
/// based on file extensions. This helps maintain an accurate and relevant collection
/// of MIME type definitions as the application evolves.
///
/// ### Parameters
/// 
/// - `mime_types`: A mutable reference to a `HashMap<String, String>` of MIME types.
/// - `extension`: A string slice (`&str`) representing the file extension to remove.
///
/// ### Examples
/// 
/// ```rust
/// use cans::mime::{
///     remove_mime_type,
///     set_mime_types,
/// };
///
/// let mut mime_types = set_mime_types();
/// remove_mime_type(&mut mime_types, "html");
/// assert_eq!(mime_types.get("html"), None);
/// ```
/// 
/// ```rust
/// use cans::mime::{
///     display_mime_types,
///     remove_mime_type,
///     set_mime_types,
/// };
///
/// fn main() {
///    // Create a new HashMap and initialize it with default MIME types
///    let mut mime_types = set_mime_types();
///
///    // Remove an existing MIME type
///    remove_mime_type(&mut mime_types, "jpg");
///    println!("\nAfter removing .jpg MIME type:");
///    display_mime_types(&mime_types);
/// }
/// ```
/// 
/// ### Usage Context
/// 
/// This function is essential for scenarios where certain MIME types need to be
/// deprecated or removed from the active listings, ensuring that the application
/// stays up-to-date with the current file typing needs.
///
/// <small>End Fun Doc</small>
pub fn remove_mime_type(mime_types: &mut HashMap<String, String>, extension: &str) {
    mime_types.remove(extension);
}

/// ### insert_mime_types
///
/// Inserts entries from a given HashMap of MIME types into an existing HashMap.
///
/// The `insert_mime_types` function takes another `HashMap` containing MIME types
/// and merges its entries into the provided existing `HashMap`. This allows for
/// extensibility and flexibility when managing MIME types, facilitating the
/// addition of multiple entries at once.
///
/// ### Parameters
///
/// - `mime_types`: A mutable reference to an existing `HashMap<String, String>`
///   that will receive new MIME type entries.
/// - `mime_types_map`: A `HashMap<String, String>` containing MIME type entries
///   to be inserted into the existing `mime_types` HashMap.
/// 
/// ```rust
/// use cans::mime::{
///     display_mime_types,
///     insert_mime_types,
///     manage_mime_types,
///     set_mime_types,
/// };
/// 
/// fn main() {
///      // Create a new HashMap and initialize it with default MIME types
///      let mut mime_types = set_mime_types();
/// 
///      // Display the default count of MIME types
///      println!("The default length of MIME types: {}", mime_types.len()); // Example output: 113
/// 
///      // Define an array of tuples for both already included and new MIME types
///      let included_mime_types = manage_mime_types(&[
///          // Already included MIME types
///          ("html", "text/html"),
///          ("css", "text/css"),
///          ("js", "application/javascript"),
///          ("svg", "image/svg+xml"),
///          ("gif", "image/gif"),
///          ("jpg", "image/jpeg"),
///          ("txt", "text/plain"),
///          ("json", "application/json"),
///          // New MIME type to include
///          ("some_ext", "some_mime"),
///      ]);
/// 
///      // Insert new MIME types into the existing HashMap
///      insert_mime_types(&mut mime_types, included_mime_types);
/// 
///      // Display the length after insertion
///      println!(
///          "The length after inserting MIME types: {}",
///          mime_types.len() // Example output: 114
///      );
/// 
///     // Display the updated MIME types
///     println!("\nAfter inserting MIME types:");
///     display_mime_types(&mime_types);
/// }
/// ```
///
/// ### Usage Context
/// 
/// This function is particularly useful when there is a need to bulk-add MIME types
/// from a predefined list or configuration. It simplifies the process of extending
/// the supported MIME types within an application, improving maintainability and
/// organization.
///
/// <small>End Fun Doc</small>
pub fn insert_mime_types(
    mime_types: &mut HashMap<String, String>,
    mime_types_map: HashMap<String, String>,
) {
    for (extension, mime_type) in mime_types_map {
        mime_types.insert(extension, mime_type);
    }
}

/// ### remove_mime_types
///
/// Removes multiple MIME types based on specified file extensions from the HashMap.
///
/// The `remove_mime_types` function allows for the bulk removal of existing MIME
/// definitions based on their associated file extensions. This facilitates effective
/// management of MIME type definitions, enabling applications to remain efficient
/// and relevant as they evolve.
///
/// ### Parameters
/// 
/// - `mime_types`: A mutable reference to a `HashMap<String, String>` containing
///   MIME types, from which entries will be removed.
/// - `mime_types_map`: A `HashMap<String, String>` where the keys represent
///   the file extensions to be removed from the existing `mime_types` HashMap.
///
/// ### Examples
/// 
/// ```rust
/// use cans::mime::{
///     display_mime_types,
///     manage_mime_types,
///     remove_mime_types,
///     set_mime_types,
/// };
///
/// fn main() {
///     // Create a new HashMap and initialize it with default MIME types
///     let mut mime_types = set_mime_types();
///
///     // Display the default count of MIME types
///     println!("The default length of MIME types: {}", mime_types.len()); // Example output: 113
///
///     // Define an array of tuples for both pre-existing and new MIME types
///     let excluded_mime_types = manage_mime_types(&[
///         // Pre-existing MIME types by default
///         ("html", "text/html"),
///         ("css", "text/css"),
///         ("js", "application/javascript"),
///         ("svg", "image/svg+xml"),
///         ("gif", "image/gif"),
///         ("jpg", "image/jpeg"),
///         ("txt", "text/plain"),
///         ("json", "application/json"),
///         // New MIME type that is not part of the default list
///         ("some_ext", "some_mime"),
///     ]);
///
///     // Attempt to remove MIME types from the existing HashMap
///     remove_mime_types(&mut mime_types, excluded_mime_types);
///
///     // Display the length after removal
///     println!(
///         "The length after removing MIME types: {}",
///         mime_types.len() // Example output: 105
///     );
///
///     // Display the updated MIME types
///     println!("\nAfter removing MIME types:");
///     display_mime_types(&mime_types);
/// }
/// ```
///
/// ### Usage Context
/// 
/// This function is particularly useful in scenarios where multiple MIME types
/// need to be deprecated or removed in a single operation. It helps maintain an
/// organized and relevant collection of MIME type definitions, ensuring application
/// consistency and efficiency.
///
/// <small>End Fun Doc</small>
pub fn remove_mime_types(
    mime_types: &mut HashMap<String, String>,
    mime_types_map: HashMap<String, String>,
) {
    for extension in mime_types_map.keys() {
        mime_types.remove(extension);
    }
}

/// ### display_mime_types
///
/// Prints all the current MIME types stored in a `HashMap` to the console.
///
/// This function iterates over the provided `HashMap` containing file extensions as keys
/// and their corresponding MIME types as values. For each key-value pair, it prints the
/// extension and the associated MIME type in a human-readable format.
///
/// ### Parameters
/// 
/// - `mime_types`: A reference to a `HashMap<String, String>`, which contains
///   mappings of file extensions to their corresponding MIME types.
///
/// ### Examples
///
/// ```rust
/// use cans::mime::{display_mime_types, set_mime_types};
///
/// fn main() {
///     // Initialize MIME types
///     let mime_types = set_mime_types();
///
///     // Display all MIME types
///     display_mime_types(&mime_types);
///     // Output:
///     // Extension: html - MIME Type: text/html
///     // Extension: css - MIME Type: text/css
///     // Extension: js - MIME Type: application/javascript
///     // ...
/// }
/// ```
///
/// ### Notes
/// 
/// - Ensure that the `mime_types` HashMap is not empty to see any output.
/// - This function is intended for display purposes and does not modify the input HashMap.
///
/// <small>End Fun Doc</small>
pub fn display_mime_types(mime_types: &HashMap<String, String>) {
    for (extension, mime_type) in mime_types {
        println!("Extension: {} - MIME Type: {}", extension, mime_type);
    }
}

/// ### default_mime_types
///
/// Displays the default set of MIME types.
///
/// This function retrieves the default MIME types by calling the `set_mime_types()` function,
/// which returns a HashMap containing extensions and their corresponding MIME types.
/// It then prints each extension alongside its MIME type in a formatted manner.
///
/// ### Example
///
/// ```rust
/// use cans::mime::default_mime_types;
///
/// default_mime_types();
/// ```
///
/// ### Sample Output
///
/// ```terminal
/// Default MIME types:
/// Extension: html - MIME Type: text/html
/// Extension: css - MIME Type: text/css
/// Extension: js - MIME Type: application/javascript
/// Extension: svg - MIME Type: image/svg+xml
/// ...
/// ```
///
/// ### Notes
///
/// - The function does not take any parameters and does not return any values.
/// - It is primarily intended for debugging and informational purposes, helping users
///   understand the default MIME types recognized by the application.
///
/// <small>End Fun Doc</small>
pub fn default_mime_types() {
    println!("\nDefault MIME types:");
    let default_mime_types = set_mime_types();
    for (extension, mime_type) in &default_mime_types {
        println!("Extension: {} - MIME Type: {}", extension, mime_type);
    }
}