Skip to content

Commit 0b6c4ce

Browse files
committed
Inlines the arca dependency
1 parent 27e5bd9 commit 0b6c4ce

File tree

6 files changed

+84
-6
lines changed

6 files changed

+84
-6
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ repository = "https://github.com/yarnpkg/pnp-rs"
1010
[dependencies]
1111
arca = "^0.7"
1212
byteorder = "1"
13+
clean-path = "0.2.1"
1314
concurrent_lru = "^0.2"
1415
fancy-regex = "^0.13.0"
1516
indexmap = { version = "2.7.1", features = ["serde"] }
1617
lazy_static = "1"
1718
miniz_oxide = "^0.7"
1819
mmap-rs = { version = "^0.6", optional = true }
20+
path-slash = "0.2.1"
1921
pathdiff = "^0.2"
22+
radix_trie = "0.2.1"
2023
regex = "1"
2124
serde = { version = "1", features = ["derive"] }
2225
serde_json = "1"

src/fs.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn split_virtual(p_bytes: &[u8]) -> std::io::Result<(usize, Option<(usize, usize
225225
}
226226

227227
fn vpath(p: &Path) -> std::io::Result<VPath> {
228-
let p_str = arca::path::normalize_path(
228+
let p_str = crate::util::normalize_path(
229229
&p.as_os_str()
230230
.to_string_lossy()
231231
);
@@ -303,6 +303,8 @@ mod tests {
303303
use rstest::rstest;
304304
use std::path::PathBuf;
305305

306+
use crate::util;
307+
306308
use super::*;
307309

308310
#[test]
@@ -445,7 +447,7 @@ mod tests {
445447
fn test_path_to_pnp(#[case] input: &str, #[case] expected: Option<VPath>) {
446448
let expectation: VPath = match &expected {
447449
Some(p) => p.clone(),
448-
None => VPath::Native(PathBuf::from(arca::path::normalize_path(input))),
450+
None => VPath::Native(PathBuf::from(util::normalize_path(input))),
449451
};
450452

451453
match vpath(&PathBuf::from(input)) {

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub struct Manifest {
132132
pub manifest_path: PathBuf,
133133

134134
#[serde(skip_deserializing)]
135-
location_trie: arca::path::Trie<PackageLocator>,
135+
location_trie: util::Trie<PackageLocator>,
136136

137137
enable_top_level_fallback: bool,
138138
ignore_pattern_data: Option<RegexDef>,
@@ -265,7 +265,7 @@ pub fn init_pnp_manifest<P: AsRef<Path>>(manifest: &mut Manifest, p: P) {
265265
let package_location = manifest.manifest_dir
266266
.join(info.package_location.clone());
267267

268-
let normalized_location = arca::path::normalize_path(
268+
let normalized_location = util::normalize_path(
269269
&package_location.to_string_lossy(),
270270
);
271271

@@ -304,7 +304,7 @@ pub fn find_locator<'a, P: AsRef<Path>>(manifest: &'a Manifest, path: &P) -> Opt
304304
.expect("Assertion failed: Provided path should be absolute");
305305

306306
if let Some(regex) = &manifest.ignore_pattern_data {
307-
if regex.0.is_match(&arca::path::normalize_path(rel_path.to_string_lossy())).unwrap() {
307+
if regex.0.is_match(&util::normalize_path(rel_path.to_string_lossy())).unwrap() {
308308
return None
309309
}
310310
}

src/util.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,75 @@ use fancy_regex::Regex;
22
use serde::{de::Error, Deserialize, Deserializer};
33
use std::borrow::Cow;
44

5+
use path_slash::PathBufExt;
6+
use std::path::{PathBuf, Path};
7+
8+
#[derive(Debug, Default, Clone)]
9+
pub struct Trie<T> {
10+
inner: radix_trie::Trie<String, (PathBuf, T)>,
11+
}
12+
13+
impl<T> Trie<T> {
14+
fn key<P: AsRef<Path>>(&self, key: &P) -> String {
15+
let mut p = normalize_path(key.as_ref().to_string_lossy());
16+
17+
if !p.ends_with('/') {
18+
p.push('/');
19+
}
20+
21+
p
22+
}
23+
24+
pub fn get_ancestor_value<P: AsRef<Path>>(&self, key: &P) -> Option<&T> {
25+
self.inner.get_ancestor_value(&self.key(&key)).map(|t| &t.1)
26+
}
27+
28+
pub fn insert<P: AsRef<Path>>(&mut self, key: P, value: T) -> () {
29+
let k = self.key(&key);
30+
let p = PathBuf::from(k.clone());
31+
32+
self.inner.insert(k, (p, value)).map(|t| t.1);
33+
}
34+
}
35+
36+
pub fn normalize_path<P: AsRef<str>>(original: P) -> String {
37+
let original_str = original.as_ref();
38+
39+
let p = PathBuf::from(original_str);
40+
let mut str = clean_path::clean(p)
41+
.to_slash_lossy()
42+
.to_string();
43+
44+
if original_str.ends_with('/') && !str.ends_with('/') {
45+
str.push('/');
46+
}
47+
48+
str
49+
}
50+
51+
#[cfg(test)]
52+
mod tests {
53+
use super::*;
54+
55+
#[test]
56+
fn test_normalize_path() {
57+
assert_eq!(normalize_path(""), ".");
58+
assert_eq!(normalize_path("/"), "/");
59+
assert_eq!(normalize_path("foo"), "foo");
60+
assert_eq!(normalize_path("foo/bar"), "foo/bar");
61+
assert_eq!(normalize_path("foo//bar"), "foo/bar");
62+
assert_eq!(normalize_path("foo/./bar"), "foo/bar");
63+
assert_eq!(normalize_path("foo/../bar"), "bar");
64+
assert_eq!(normalize_path("foo/bar/.."), "foo");
65+
assert_eq!(normalize_path("foo/../../bar"), "../bar");
66+
assert_eq!(normalize_path("../foo/../../bar"), "../../bar");
67+
assert_eq!(normalize_path("./foo"), "foo");
68+
assert_eq!(normalize_path("../foo"), "../foo");
69+
assert_eq!(normalize_path("/foo/bar"), "/foo/bar");
70+
assert_eq!(normalize_path("/foo/bar/"), "/foo/bar/");
71+
}
72+
}
73+
574
fn strip_slash_escape(str: &str) -> String {
675
let mut res = String::default();
776
res.reserve_exact(str.len());

src/zip.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use byteorder::{ReadBytesExt, LittleEndian};
55
use std::io::Read;
66

77
use crate::fs::FileType;
8+
use crate::util;
89

910
#[derive(Debug)]
1011
pub enum Compression {
@@ -36,7 +37,7 @@ where T : AsRef<[u8]> {
3637
};
3738

3839
for (name, maybe_entry) in list_zip_entries(zip.storage.as_ref())? {
39-
let name = arca::path::normalize_path(name);
40+
let name = util::normalize_path(name);
4041
let segments: Vec<&str> = name.split('/').collect();
4142

4243
for t in 1..segments.len() - 1 {

0 commit comments

Comments
 (0)