use std::path::Path;
pub const PERL_SOURCE_EXTENSIONS: [&str; 4] = ["pl", "pm", "t", "psgi"];
#[must_use]
pub fn is_perl_source_extension(extension: &str) -> bool {
let ext = extension.strip_prefix('.').unwrap_or(extension);
PERL_SOURCE_EXTENSIONS.iter().any(|candidate| candidate.eq_ignore_ascii_case(ext))
}
#[must_use]
pub fn is_perl_source_path(path: &Path) -> bool {
path.extension().and_then(|ext| ext.to_str()).is_some_and(is_perl_source_extension)
}
#[must_use]
pub fn is_perl_source_uri(uri: &str) -> bool {
let without_fragment = uri.split('#').next().unwrap_or(uri);
let without_query = without_fragment.split('?').next().unwrap_or(without_fragment);
is_perl_source_path(Path::new(without_query))
}
#[cfg(test)]
mod tests {
use super::{
PERL_SOURCE_EXTENSIONS, is_perl_source_extension, is_perl_source_path, is_perl_source_uri,
};
use std::path::Path;
#[test]
fn exposes_expected_extension_set() {
assert_eq!(PERL_SOURCE_EXTENSIONS, ["pl", "pm", "t", "psgi"]);
}
#[test]
fn classifies_extensions_case_insensitively() {
assert!(is_perl_source_extension("pl"));
assert!(is_perl_source_extension(".pm"));
assert!(is_perl_source_extension("T"));
assert!(is_perl_source_extension("PsGi"));
assert!(!is_perl_source_extension("txt"));
}
#[test]
fn classifies_filesystem_paths() {
assert!(is_perl_source_path(Path::new("/workspace/script.pl")));
assert!(is_perl_source_path(Path::new("/workspace/lib/Foo/Bar.PM")));
assert!(is_perl_source_path(Path::new("/workspace/app.psgi")));
assert!(!is_perl_source_path(Path::new("/workspace/README.md")));
assert!(!is_perl_source_path(Path::new("/workspace/no_extension")));
}
#[test]
fn classifies_uri_like_inputs() {
assert!(is_perl_source_uri("file:///workspace/script.pl"));
assert!(is_perl_source_uri("file:///workspace/lib/Foo/Bar.pm"));
assert!(is_perl_source_uri("file:///workspace/app.psgi"));
assert!(is_perl_source_uri("file:///workspace/app.psgi?version=1#section"));
assert!(!is_perl_source_uri("file:///workspace/README.md"));
}
#[test]
fn supports_windows_style_paths() {
assert!(is_perl_source_uri(r"C:\workspace\script.pl"));
assert!(is_perl_source_uri(r"file:///C:/workspace/lib/Foo.pm"));
assert!(!is_perl_source_uri(r"C:\workspace\README.txt"));
}
}