use std::path::Path;
use panproto_parse::ParserRegistry;
#[must_use]
pub fn detect_language<'a>(path: &Path, registry: &'a ParserRegistry) -> Option<&'a str> {
registry.detect_language(path)
}
#[must_use]
pub fn is_binary_extension(path: &Path) -> bool {
path.extension()
.and_then(|e| e.to_str())
.is_some_and(|ext| {
matches!(
ext.to_lowercase().as_str(),
"png"
| "jpg"
| "jpeg"
| "gif"
| "webp"
| "ico"
| "bmp"
| "tiff"
| "pdf"
| "zip"
| "tar"
| "gz"
| "bz2"
| "xz"
| "7z"
| "rar"
| "wasm"
| "o"
| "so"
| "dylib"
| "dll"
| "exe"
| "bin"
| "class"
| "pyc"
| "pyo"
)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detect_core_languages() {
let registry = ParserRegistry::new();
assert_eq!(
detect_language(Path::new("lib.py"), ®istry),
Some("python")
);
assert_eq!(
detect_language(Path::new("main.rs"), ®istry),
Some("rust")
);
assert_eq!(detect_language(Path::new("main.go"), ®istry), Some("go"));
}
#[test]
fn detect_unknown_returns_none() {
let registry = ParserRegistry::new();
assert_eq!(detect_language(Path::new("LICENSE"), ®istry), None);
assert_eq!(detect_language(Path::new("Makefile"), ®istry), None);
}
#[test]
fn binary_detection() {
assert!(is_binary_extension(Path::new("photo.png")));
assert!(is_binary_extension(Path::new("app.wasm")));
assert!(is_binary_extension(Path::new("archive.zip")));
assert!(!is_binary_extension(Path::new("main.rs")));
assert!(!is_binary_extension(Path::new("README.md")));
}
}