gem_rs/
utils.rs

1//! Utility functions for the Gem-rs library.
2//!
3//! This module contains various utility functions used internally by the Gem-rs library,
4//! primarily for handling MIME types of different file formats.
5
6use std::path::Path;
7
8/// Determines the MIME type of a file based on its extension.
9///
10/// This function takes a file path and attempts to determine its MIME type
11/// by examining the file extension. It supports various file formats including
12/// documents, images, audio, and video files.
13///
14/// # Arguments
15///
16/// * `file_path` - A reference to a `Path` representing the file path.
17///
18/// # Returns
19///
20/// An `Option<String>` containing the MIME type if recognized, or `None` if the
21/// file type is not supported or recognized.
22///
23/// # Examples
24///
25/// ```
26/// use std::path::Path;
27/// use gem_rs::utils::get_mime_type;
28///
29/// let path = Path::new("example.pdf");
30/// assert_eq!(get_mime_type(path), Some("application/pdf".to_string()));
31///
32/// let path = Path::new("image.png");
33/// assert_eq!(get_mime_type(path), Some("image/png".to_string()));
34///
35/// let path = Path::new("unknown.xyz");
36/// assert_eq!(get_mime_type(path), None);
37/// ```
38///
39/// # Notes
40///
41/// - Currently, all document types are treated as PDF. This may change in future updates.
42/// - GIF files are currently not supported, as there's a TODO to implement GIF to video conversion.
43/// - All text types (including code files) are currently treated as plain text.
44pub fn get_mime_type(file_path: &Path) -> Option<String> {
45    match file_path.extension().and_then(|ext| ext.to_str()) {
46        //TODO: Convert all document types to PDF
47        Some("pdf") => Some("application/pdf".to_string()),
48
49        Some("png") => Some("image/png".to_string()),
50        Some("jpg") | Some("jpeg") => Some("image/jpeg".to_string()),
51
52        Some("mp3") => Some("audio/mpeg".to_string()),
53        Some("wav") => Some("audio/wav".to_string()),
54
55        Some("mp4") => Some("video/mp4".to_string()),
56        Some("mov") => Some("video/quicktime".to_string()),
57        Some("mpeg") => Some("video/mpeg".to_string()),
58        Some("mpg") => Some("video/mpeg".to_string()),
59        Some("avi") => Some("video/x-msvideo".to_string()),
60        Some("wmv") => Some("video/x-ms-wmv".to_string()),
61        Some("mpegps") => Some("video/mpeg".to_string()),
62        Some("flv") => Some("video/x-flv".to_string()),
63        // Some("gif") => Some("image/gif".to_string()), TODO: implement gif to video conversion
64
65        //TODO: Convert all text types (including codes) to plain text
66        Some("txt") => Some("text/plain".to_string()),
67        _ => None,
68    }
69}