Skip to content

Commit fd4f5d0

Browse files
committed
Moves the trie to a separate crate
1 parent e8ccae7 commit fd4f5d0

File tree

5 files changed

+33
-68
lines changed

5 files changed

+33
-68
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
[package]
22
name = "pnp"
3-
version = "0.4.5"
3+
version = "0.4.6"
44
edition = "2021"
55
license = "BSD-2-Clause"
66
description = "Resolution primitives for Yarn PnP"
77
homepage = "https://yarnpkg.com"
88
repository = "https://github.com/yarnpkg/berry/"
99

1010
[dependencies]
11-
clean-path = "0.2.1"
11+
arca = "0.1.3"
1212
fancy-regex = "0.11.0"
1313
lazy_static = "1.4.0"
1414
lru = "0.10.0"
1515
miniz_oxide = "0.7.1"
16-
path-slash = "0.2.1"
1716
pathdiff = "0.2.1"
18-
radix_trie = "0.2.1"
1917
regex = "1.7.1"
2018
serde = { version = "1.0.152", features = ["derive"] }
2119
serde_json = "1.0.93"

src/fs.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use serde::Deserialize;
55
use std::{path::{Path, PathBuf}, fs, io::{BufReader, Read}, collections::{HashSet, HashMap}, str::Utf8Error, num::NonZeroUsize};
66
use zip::{ZipArchive, result::ZipError};
77

8-
use crate::util;
9-
108
#[derive(Clone)]
119
#[derive(Debug)]
1210
#[derive(Deserialize)]
@@ -78,7 +76,7 @@ impl Zip {
7876
for i in 0..zip.archive.len() {
7977
let entry = zip.archive.by_index_raw(i)?;
8078

81-
let name = util::normalize_path(entry.name());
79+
let name = arca::path::normalize_path(entry.name());
8280
let segments: Vec<&str> = name.split('/').collect();
8381

8482
for t in 1..segments.len() - 1 {
@@ -219,7 +217,7 @@ pub fn vpath(p: &Path) -> Result<VPath, std::io::Error> {
219217
.to_string_lossy()
220218
.to_string();
221219

222-
let mut p_bytes = util::normalize_path(p_str.clone())
220+
let mut p_bytes = arca::path::normalize_path(p_str.clone())
223221
.as_bytes().to_vec();
224222

225223
if let Some(m) = VIRTUAL_RE.captures(&p_bytes) {
@@ -231,7 +229,7 @@ pub fn vpath(p: &Path) -> Result<VPath, std::io::Error> {
231229
&subpath.as_bytes(),
232230
].concat();
233231

234-
p_str = util::normalize_path(io_bytes_to_str(&bytes)?);
232+
p_str = arca::path::normalize_path(io_bytes_to_str(&bytes)?);
235233
p_bytes = p_str.as_bytes().to_vec();
236234
}
237235
}
@@ -263,7 +261,7 @@ pub fn vpath(p: &Path) -> Result<VPath, std::io::Error> {
263261
let zip_path = PathBuf::from(io_bytes_to_str(&p_bytes[0..next_char_idx])?);
264262

265263
let sub_path = if next_char_idx + 1 < p_bytes.len() {
266-
util::normalize_path(io_bytes_to_str(&p_bytes[next_char_idx + 1..])?)
264+
arca::path::normalize_path(io_bytes_to_str(&p_bytes[next_char_idx + 1..])?)
267265
} else {
268266
return Ok(VPath::Native(zip_path))
269267
};

src/lib.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ mod util;
33

44
use fancy_regex::Regex;
55
use lazy_static::lazy_static;
6-
use radix_trie::Trie;
76
use serde::Deserialize;
87
use serde_with::{serde_as, DefaultOnNull};
98
use std::{path::{Path, PathBuf}, collections::{HashSet, HashMap, hash_map::Entry}};
@@ -52,6 +51,8 @@ pub struct ResolutionConfig {
5251
}
5352

5453
#[derive(Clone)]
54+
#[derive(Debug)]
55+
#[derive(Default)]
5556
#[derive(Deserialize)]
5657
pub struct PackageLocator {
5758
name: String,
@@ -71,7 +72,7 @@ enum PackageDependency {
7172
#[derive(Deserialize)]
7273
#[serde(rename_all = "camelCase")]
7374
pub struct PackageInformation {
74-
package_location: String,
75+
package_location: PathBuf,
7576

7677
#[serde(default)]
7778
discard_from_lookup: bool,
@@ -92,7 +93,7 @@ pub struct Manifest {
9293
pub manifest_path: PathBuf,
9394

9495
#[serde(skip_deserializing)]
95-
location_trie: Trie<String, PackageLocator>,
96+
location_trie: arca::path::Trie<PackageLocator>,
9697

9798
enable_top_level_fallback: bool,
9899
ignore_pattern_data: Option<RegexDef>,
@@ -202,15 +203,17 @@ pub fn init_pnp_manifest<P: AsRef<Path>>(manifest: &mut Manifest, p: P) {
202203

203204
for (name, ranges) in manifest.package_registry_data.iter_mut() {
204205
for (reference, info) in ranges.iter_mut() {
205-
let p = manifest.manifest_dir
206+
let package_location = manifest.manifest_dir
206207
.join(info.package_location.clone());
207208

208-
info.package_location = util::normalize_path(
209-
p.to_string_lossy(),
209+
let normalized_location = arca::path::normalize_path(
210+
&package_location.to_string_lossy(),
210211
);
211212

213+
info.package_location = PathBuf::from(normalized_location);
214+
212215
if !info.discard_from_lookup {
213-
manifest.location_trie.insert(info.package_location.clone(), PackageLocator {
216+
manifest.location_trie.insert(&info.package_location, PackageLocator {
214217
name: name.clone(),
215218
reference: reference.clone(),
216219
});
@@ -238,16 +241,12 @@ pub fn find_locator<'a, P: AsRef<Path>>(manifest: &'a Manifest, path: &P) -> Opt
238241
.expect("Assertion failed: Provided path should be absolute");
239242

240243
if let Some(regex) = &manifest.ignore_pattern_data {
241-
if regex.0.is_match(&util::normalize_path(rel_path.to_string_lossy())).unwrap() {
244+
if regex.0.is_match(&arca::path::normalize_path(rel_path.to_string_lossy())).unwrap() {
242245
return None
243246
}
244247
}
245248

246-
let trie_key = util::normalize_path(
247-
path.as_ref().to_string_lossy(),
248-
);
249-
250-
manifest.location_trie.get_ancestor_value(&trie_key)
249+
manifest.location_trie.get_ancestor_value(&path)
251250
}
252251

253252
pub fn get_package<'a>(manifest: &'a Manifest, locator: &PackageLocator) -> Result<&'a PackageInformation, Error> {
@@ -301,7 +300,7 @@ pub fn resolve_to_unqualified_via_manifest<P: AsRef<Path>>(manifest: &Manifest,
301300
PackageDependency::Alias(name, reference) => get_package(&manifest, &PackageLocator { name, reference }),
302301
}?;
303302

304-
Ok(Resolution::Package(PathBuf::from(dependency_pkg.package_location.clone()), module_path.clone()))
303+
Ok(Resolution::Package(dependency_pkg.package_location.clone(), module_path.clone()))
305304
} else {
306305
return Err(Error::FailedResolution);
307306
}

src/util.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,6 @@
11
use fancy_regex::Regex;
2-
use path_slash::PathBufExt;
32
use serde::{de::Error, Deserialize, Deserializer};
4-
use std::{borrow::Cow, path::{PathBuf}};
5-
6-
pub fn normalize_path<P: AsRef<str>>(original: P) -> String {
7-
let original_str = original.as_ref();
8-
9-
let p = PathBuf::from(original_str);
10-
let mut str = clean_path::clean(p)
11-
.to_slash_lossy()
12-
.to_string();
13-
14-
if original_str.ends_with('/') && !str.ends_with('/') {
15-
str.push('/');
16-
}
17-
18-
str
19-
}
20-
21-
#[cfg(test)]
22-
mod tests {
23-
use super::*;
24-
25-
#[test]
26-
fn test_normalize_path() {
27-
assert_eq!(normalize_path(""), ".");
28-
assert_eq!(normalize_path("/"), "/");
29-
assert_eq!(normalize_path("foo"), "foo");
30-
assert_eq!(normalize_path("foo/bar"), "foo/bar");
31-
assert_eq!(normalize_path("foo//bar"), "foo/bar");
32-
assert_eq!(normalize_path("foo/./bar"), "foo/bar");
33-
assert_eq!(normalize_path("foo/../bar"), "bar");
34-
assert_eq!(normalize_path("foo/bar/.."), "foo");
35-
assert_eq!(normalize_path("foo/../../bar"), "../bar");
36-
assert_eq!(normalize_path("../foo/../../bar"), "../../bar");
37-
assert_eq!(normalize_path("./foo"), "foo");
38-
assert_eq!(normalize_path("../foo"), "../foo");
39-
assert_eq!(normalize_path("/foo/bar"), "/foo/bar");
40-
assert_eq!(normalize_path("/foo/bar/"), "/foo/bar/");
41-
}
42-
}
3+
use std::{borrow::Cow};
434

445
fn strip_slash_escape(str: &str) -> String {
456
let mut res = String::default();

0 commit comments

Comments
 (0)