provenant/utils/
language.rs1use content_inspector::{ContentType, inspect};
2use std::path::Path;
3
4fn is_utf8_text(content_type: ContentType) -> bool {
5 content_type == ContentType::UTF_8 || content_type == ContentType::UTF_8_BOM
6}
7
8pub fn detect_language(path: &Path, content: &[u8]) -> String {
9 if content.len() > 32 && !is_utf8_text(inspect(content)) {
10 return "Binary".to_string();
11 }
12
13 if content.len() > 2 && content[0] == b'#' && content[1] == b'!' {
15 let shebang_end = content
16 .iter()
17 .position(|&b| b == b'\n')
18 .unwrap_or(content.len());
19 let shebang = String::from_utf8_lossy(&content[0..shebang_end]);
20
21 if shebang.contains("python") {
22 return "Python".to_string();
23 } else if shebang.contains("node") {
24 return "JavaScript".to_string();
25 } else if shebang.contains("ruby") {
26 return "Ruby".to_string();
27 } else if shebang.contains("perl") {
28 return "Perl".to_string();
29 } else if shebang.contains("php") {
30 return "PHP".to_string();
31 } else if shebang.contains("bash") || shebang.contains("sh") {
32 return "Shell".to_string();
33 }
34 }
35
36 if let Some(extension) = path.extension().and_then(|e| e.to_str()) {
38 match extension.to_lowercase().as_str() {
39 "rs" => return "Rust".to_string(),
40 "py" => return "Python".to_string(),
41 "js" => return "JavaScript".to_string(),
42 "ts" => return "TypeScript".to_string(),
43 "html" | "htm" => return "HTML".to_string(),
44 "css" => return "CSS".to_string(),
45 "c" => return "C".to_string(),
46 "cpp" | "cc" | "cxx" => return "C++".to_string(),
47 "h" => return "C".to_string(),
48 "hpp" => return "C++".to_string(),
49 "s" => return "GAS".to_string(),
50 "java" => return "Java".to_string(),
51 "go" => return "Go".to_string(),
52 "rb" => return "Ruby".to_string(),
53 "php" => return "PHP".to_string(),
54 "pl" => return "Perl".to_string(),
55 "swift" => return "Swift".to_string(),
56 "md" | "markdown" => return "Markdown".to_string(),
57 "json" => return "JSON".to_string(),
58 "xml" => return "XML".to_string(),
59 "yml" | "yaml" => return "YAML".to_string(),
60 "sql" => return "SQL".to_string(),
61 "sh" | "bash" => return "Shell".to_string(),
62 "kt" | "kts" => return "Kotlin".to_string(),
63 "dart" => return "Dart".to_string(),
64 "scala" => return "Scala".to_string(),
65 "cs" => return "C#".to_string(),
66 "fs" => return "F#".to_string(),
67 "r" => return "R".to_string(),
68 "lua" => return "Lua".to_string(),
69 "jl" => return "Julia".to_string(),
70 "ex" | "exs" => return "Elixir".to_string(),
71 "clj" => return "Clojure".to_string(),
72 "hs" => return "Haskell".to_string(),
73 "erl" => return "Erlang".to_string(),
74 "sc" => return "SuperCollider".to_string(),
75 "tex" => return "TeX".to_string(),
76 _ => {}
77 }
78 }
79
80 let file_name = path
82 .file_name()
83 .and_then(|n| n.to_str())
84 .map(|s| s.to_lowercase())
85 .unwrap_or_default();
86
87 if matches!(
88 file_name.as_str(),
89 "dockerfile" | "containerfile" | "containerfile.core"
90 ) {
91 return "Dockerfile".to_string();
92 } else if file_name == "makefile" {
93 return "Makefile".to_string();
94 } else if file_name == "gemfile" || file_name == "rakefile" {
95 return "Ruby".to_string();
96 }
97
98 if is_utf8_text(inspect(content)) {
99 let text_sample = String::from_utf8_lossy(&content[..std::cmp::min(content.len(), 1000)]);
100
101 if text_sample.contains("<?php") {
102 return "PHP".to_string();
103 } else if text_sample.contains("<html") || text_sample.contains("<!DOCTYPE html") {
104 return "HTML".to_string();
105 } else if text_sample.contains("import React") || text_sample.contains("import {") {
106 return "JavaScript/TypeScript".to_string();
107 } else if text_sample.contains("def ") && text_sample.contains(":") {
108 return "Python".to_string();
109 } else if text_sample.contains("package ")
110 && text_sample.contains("import ")
111 && text_sample.contains("{")
112 {
113 return "Go".to_string();
114 }
115
116 return "Text".to_string();
117 }
118
119 "Unknown".to_string()
120}
121
122#[cfg(test)]
123mod tests {
124 use super::detect_language;
125 use std::path::Path;
126
127 #[test]
128 fn detect_language_supports_containerfile_names() {
129 assert_eq!(
130 detect_language(Path::new("Containerfile"), b"FROM scratch\n"),
131 "Dockerfile"
132 );
133 assert_eq!(
134 detect_language(Path::new("containerfile.core"), b"FROM scratch\n"),
135 "Dockerfile"
136 );
137 }
138
139 #[test]
140 fn detect_language_maps_c_headers_to_c() {
141 assert_eq!(detect_language(Path::new("zlib.h"), b"/* header */\n"), "C");
142 }
143
144 #[test]
145 fn detect_language_maps_uppercase_s_to_gas() {
146 assert_eq!(detect_language(Path::new("gvmat64.S"), b"; asm\n"), "GAS");
147 }
148}