rust_filesearch/models/
category.rs

1use serde::{Deserialize, Serialize};
2
3/// Smart file categorization based on heuristics
4#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(tag = "type", rename_all = "lowercase")]
6pub enum FileCategory {
7    /// Source code files
8    Source { language: String },
9    /// Build artifacts and compiled binaries
10    Build,
11    /// Configuration files
12    Config { format: String },
13    /// Documentation files
14    Documentation,
15    /// Media files (images, videos, audio)
16    Media { media_type: MediaType },
17    /// Data files (CSV, JSON, XML, databases)
18    Data { format: String },
19    /// Compressed archives
20    Archive,
21    /// Executable binaries
22    Executable,
23    /// Unknown or uncategorized
24    Unknown,
25}
26
27/// Media file types
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
29#[serde(rename_all = "lowercase")]
30pub enum MediaType {
31    Image,
32    Video,
33    Audio,
34}
35
36impl FileCategory {
37    /// Categorize a file based on its extension
38    pub fn from_extension(ext: &str) -> Self {
39        match ext.to_lowercase().as_str() {
40            // Source code
41            "rs" => FileCategory::Source {
42                language: "rust".to_string(),
43            },
44            "go" => FileCategory::Source {
45                language: "go".to_string(),
46            },
47            "py" => FileCategory::Source {
48                language: "python".to_string(),
49            },
50            "js" | "jsx" | "ts" | "tsx" => FileCategory::Source {
51                language: "javascript".to_string(),
52            },
53            "java" => FileCategory::Source {
54                language: "java".to_string(),
55            },
56            "c" | "h" => FileCategory::Source {
57                language: "c".to_string(),
58            },
59            "cpp" | "cc" | "cxx" | "hpp" => FileCategory::Source {
60                language: "cpp".to_string(),
61            },
62            "rb" => FileCategory::Source {
63                language: "ruby".to_string(),
64            },
65            "php" => FileCategory::Source {
66                language: "php".to_string(),
67            },
68            "swift" => FileCategory::Source {
69                language: "swift".to_string(),
70            },
71            "kt" | "kts" => FileCategory::Source {
72                language: "kotlin".to_string(),
73            },
74
75            // Build artifacts
76            "o" | "so" | "dylib" | "dll" | "a" | "lib" => FileCategory::Build,
77
78            // Config
79            "toml" => FileCategory::Config {
80                format: "toml".to_string(),
81            },
82            "yaml" | "yml" => FileCategory::Config {
83                format: "yaml".to_string(),
84            },
85            "json" => FileCategory::Config {
86                format: "json".to_string(),
87            },
88            "ini" | "conf" | "cfg" => FileCategory::Config {
89                format: "ini".to_string(),
90            },
91
92            // Documentation
93            "md" | "markdown" => FileCategory::Documentation,
94            "txt" | "rst" | "adoc" => FileCategory::Documentation,
95            "pdf" | "tex" => FileCategory::Documentation,
96
97            // Media - Images
98            "png" | "jpg" | "jpeg" | "gif" | "bmp" | "webp" | "svg" | "ico" => {
99                FileCategory::Media {
100                    media_type: MediaType::Image,
101                }
102            }
103
104            // Media - Video
105            "mp4" | "mkv" | "avi" | "mov" | "wmv" | "flv" | "webm" => FileCategory::Media {
106                media_type: MediaType::Video,
107            },
108
109            // Media - Audio
110            "mp3" | "wav" | "flac" | "aac" | "ogg" | "m4a" | "wma" => FileCategory::Media {
111                media_type: MediaType::Audio,
112            },
113
114            // Data
115            "csv" | "tsv" => FileCategory::Data {
116                format: "csv".to_string(),
117            },
118            "xml" => FileCategory::Data {
119                format: "xml".to_string(),
120            },
121            "db" | "sqlite" | "sqlite3" => FileCategory::Data {
122                format: "sqlite".to_string(),
123            },
124            "parquet" => FileCategory::Data {
125                format: "parquet".to_string(),
126            },
127
128            // Archives
129            "zip" | "tar" | "gz" | "bz2" | "xz" | "7z" | "rar" => FileCategory::Archive,
130
131            // Executables (Unix)
132            "sh" | "bash" | "zsh" | "fish" => FileCategory::Executable,
133
134            _ => FileCategory::Unknown,
135        }
136    }
137}