use url::Url;
#[cfg(all(not(target_arch = "wasm32"), windows))]
use percent_encoding::percent_decode_str;
#[cfg(not(target_arch = "wasm32"))]
pub fn uri_to_fs_path(uri: &str) -> Option<std::path::PathBuf> {
let url = Url::parse(uri).ok()?;
if url.scheme() != "file" {
return None;
}
url.to_file_path().ok().or_else(|| windows_rooted_file_uri_to_path(&url))
}
#[cfg(not(target_arch = "wasm32"))]
pub fn fs_path_to_uri<P: AsRef<std::path::Path>>(path: P) -> Result<String, String> {
let path = normalize_filesystem_path(path.as_ref());
let abs_path = if path.is_absolute() {
path.to_path_buf()
} else {
std::env::current_dir()
.map_err(|e| format!("Failed to get current directory: {}", e))?
.join(path)
};
Url::from_file_path(&abs_path)
.map(|url| url.to_string())
.map_err(|_| format!("Failed to convert path to URI: {}", abs_path.display()))
}
#[cfg(not(target_arch = "wasm32"))]
fn normalize_filesystem_path(path: &std::path::Path) -> std::path::PathBuf {
#[cfg(windows)]
{
if let Some(path_str) = path.to_str() {
if let Some(stripped) = path_str.strip_prefix(r"\\?\UNC\") {
return std::path::PathBuf::from(format!(r"\\{}", stripped));
}
if let Some(stripped) = path_str.strip_prefix(r"\\?\") {
return std::path::PathBuf::from(stripped);
}
}
}
path.to_path_buf()
}
#[cfg(all(not(target_arch = "wasm32"), windows))]
fn windows_rooted_file_uri_to_path(url: &Url) -> Option<std::path::PathBuf> {
match url.host_str() {
None | Some("localhost") => {}
Some(_) => return None,
}
let decoded = percent_decode_str(url.path()).decode_utf8().ok()?;
if decoded.is_empty() {
return None;
}
let native = if decoded.len() > 3
&& decoded.starts_with('/')
&& decoded.as_bytes()[2] == b':'
&& decoded.as_bytes()[1].is_ascii_alphabetic()
{
decoded[1..].replace('/', "\\")
} else {
decoded.replace('/', "\\")
};
Some(std::path::PathBuf::from(native))
}
#[cfg(all(not(target_arch = "wasm32"), not(windows)))]
fn windows_rooted_file_uri_to_path(_url: &Url) -> Option<std::path::PathBuf> {
None
}
#[cfg(not(target_arch = "wasm32"))]
pub fn normalize_uri(uri: &str) -> String {
let path = std::path::Path::new(uri);
if path.is_absolute()
&& let Ok(uri_string) = fs_path_to_uri(path)
{
return uri_string;
}
if let Ok(url) = Url::parse(uri) {
return url.to_string();
}
if let Ok(uri_string) = fs_path_to_uri(path) {
return uri_string;
}
if uri.starts_with("file://")
&& let Some(fs_path) = uri_to_fs_path(uri)
&& let Ok(normalized) = fs_path_to_uri(&fs_path)
{
return normalized;
}
uri.to_string()
}
#[cfg(target_arch = "wasm32")]
pub fn normalize_uri(uri: &str) -> String {
if let Ok(url) = Url::parse(uri) { url.to_string() } else { uri.to_string() }
}
pub use perl_uri_classify::{is_file_uri, is_special_scheme, uri_extension, uri_key};
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::*;
#[test]
fn test_uri_key_basic() {
assert_eq!(uri_key("file:///tmp/test.pl"), "file:///tmp/test.pl");
}
#[test]
fn test_uri_key_windows_drive() {
assert_eq!(uri_key("file:///C:/Users/test.pl"), "file:///c:/Users/test.pl");
assert_eq!(uri_key("file:///D:/foo/bar.pm"), "file:///d:/foo/bar.pm");
}
#[test]
fn test_uri_key_invalid() {
assert_eq!(uri_key("not-a-uri"), "not-a-uri");
}
#[test]
fn test_is_file_uri() {
assert!(is_file_uri("file:///tmp/test.pl"));
assert!(!is_file_uri("https://example.com"));
assert!(!is_file_uri("untitled:Untitled-1"));
}
#[test]
fn test_is_special_scheme() {
assert!(is_special_scheme("untitled:Untitled-1"));
assert!(!is_special_scheme("file:///tmp/test.pl"));
}
#[test]
fn test_uri_extension() {
assert_eq!(uri_extension("file:///tmp/test.pl"), Some("pl"));
assert_eq!(uri_extension("file:///tmp/Module.pm"), Some("pm"));
assert_eq!(uri_extension("file:///tmp/script.t"), Some("t"));
assert_eq!(uri_extension("file:///tmp/no-extension"), None);
assert_eq!(uri_extension("file:///tmp/file.pl?query=1"), Some("pl"));
}
#[cfg(not(target_arch = "wasm32"))]
mod filesystem_tests {
use super::*;
use perl_tdd_support::{must, must_some};
#[test]
fn test_uri_to_fs_path_basic() {
let path = uri_to_fs_path("file:///tmp/test.pl");
assert!(path.is_some());
let path = must_some(path);
assert!(path.ends_with("test.pl"));
}
#[test]
fn test_uri_to_fs_path_non_file() {
assert!(uri_to_fs_path("https://example.com").is_none());
assert!(uri_to_fs_path("untitled:Untitled-1").is_none());
}
#[test]
fn test_uri_to_fs_path_with_spaces() {
let path = uri_to_fs_path("file:///tmp/path%20with%20spaces/test.pl");
assert!(path.is_some());
let path = must_some(path);
let path_str = path.to_string_lossy();
assert!(path_str.contains("path with spaces"));
}
#[test]
fn test_fs_path_to_uri_basic() {
let uri = must(fs_path_to_uri("/tmp/test.pl"));
assert!(uri.starts_with("file:///"));
assert!(uri.contains("test.pl"));
}
#[test]
fn test_fs_path_to_uri_with_spaces() {
let uri = must(fs_path_to_uri("/tmp/path with spaces/test.pl"));
assert!(uri.contains("%20") || uri.contains("path with spaces"));
}
#[test]
fn test_normalize_uri_valid() {
let uri = normalize_uri("file:///tmp/test.pl");
assert_eq!(uri, "file:///tmp/test.pl");
}
#[test]
fn test_normalize_uri_special() {
let uri = normalize_uri("untitled:Untitled-1");
assert_eq!(uri, "untitled:Untitled-1");
}
#[test]
fn test_normalize_uri_absolute_path() {
let path = std::env::temp_dir().join("normalize-uri-absolute.pl");
let raw_path = path.to_string_lossy();
let expected = must(fs_path_to_uri(&path));
assert_eq!(normalize_uri(raw_path.as_ref()), expected);
}
#[test]
fn test_roundtrip() {
let original = "/tmp/roundtrip-test.pl";
let uri = must(fs_path_to_uri(original));
let path = must_some(uri_to_fs_path(&uri));
assert!(path.ends_with("roundtrip-test.pl"));
}
}
}