forked from denoland/deno_graph
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule_specifier.rs
More file actions
57 lines (49 loc) · 1.36 KB
/
module_specifier.rs
File metadata and controls
57 lines (49 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2018-2024 the Deno authors. MIT license.
pub type ModuleSpecifier = url::Url;
pub use deno_path_util::SpecifierError;
pub use deno_path_util::resolve_import;
pub fn is_fs_root_specifier(url: &ModuleSpecifier) -> bool {
if url.scheme() != "file" {
return false;
}
let path = url.path();
let path = path.trim_start_matches('/').trim_end_matches('/');
let mut parts = path.split('/');
let Some(first_part) = parts.next() else {
return true;
};
if parts.next().is_some() {
return false;
}
let mut first_part_chars = first_part.chars();
let Some(first_char) = first_part_chars.next() else {
return true;
};
let Some(second_char) = first_part_chars.next() else {
return false;
};
// Windows path: file:///C:/example
first_part_chars.next().is_none()
&& first_char.is_ascii_alphabetic()
&& second_char == ':'
}
#[cfg(test)]
mod test {
use crate::ModuleSpecifier;
use super::*;
#[test]
fn test_is_fs_root_specifier() {
let cases = [
("https://deno.land", false),
("file:///", true),
("file://", true),
("file:///C:/", true),
("file:///V:/", true),
("file:///V:/test/", false),
];
for (specifier, expected) in cases {
let url = ModuleSpecifier::parse(specifier).unwrap();
assert_eq!(is_fs_root_specifier(&url), expected, "{:?}", specifier);
}
}
}