Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data-encoding = { workspace = true }
either = { workspace = true }
indexmap = { workspace = true }
once_cell = { workspace = true }
patricia_tree = "0.5.5"
patricia_tree = { version = "0.10.1", features = ["serde"] }
petgraph = { workspace = true, features = ["serde-1"] }
roaring = { workspace = true, features = ["serde"] }
ref-cast = "1.0.20"
Expand Down
71 changes: 5 additions & 66 deletions turbopack/crates/turbopack-core/src/resolve/alias_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ use std::{

use anyhow::Result;
use patricia_tree::PatriciaMap;
use serde::{
Deserialize, Deserializer, Serialize, Serializer,
de::{MapAccess, Visitor},
ser::SerializeMap,
};
use serde_bytes::{ByteBuf, Bytes};
use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr;
use turbo_tasks::{
NonLocalValue,
Expand All @@ -29,7 +24,8 @@ use super::pattern::Pattern;
///
/// If the pattern does not have a wildcard character, it will only match the
/// exact string, and return the template as-is.
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct AliasMap<T> {
map: PatriciaMap<BTreeMap<AliasKey, T>>,
}
Expand All @@ -55,63 +51,6 @@ where

impl<T> Eq for AliasMap<T> where T: Eq {}

impl<T> Serialize for AliasMap<T>
where
T: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(self.map.len()))?;
for (prefix, value) in self.map.iter() {
let key = ByteBuf::from(prefix);
map.serialize_entry(&key, value)?;
}
map.end()
}
}

struct AliasMapVisitor<T> {
marker: std::marker::PhantomData<T>,
}

impl<'de, T> Visitor<'de> for AliasMapVisitor<T>
where
T: Deserialize<'de>,
{
type Value = AliasMap<T>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a map of alias patterns to templates")
}

fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let mut map = AliasMap::new();
while let Some((key, value)) = access.next_entry::<&Bytes, _>()? {
map.map.insert(key, value);
}
Ok(map)
}
}

impl<'a, T> Deserialize<'a> for AliasMap<T>
where
T: Deserialize<'a>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'a>,
{
deserializer.deserialize_map(AliasMapVisitor {
marker: std::marker::PhantomData,
})
}
}

impl<T> TraceRawVcs for AliasMap<T>
where
T: TraceRawVcs,
Expand Down Expand Up @@ -392,7 +331,7 @@ impl<'a, T> IntoIterator for &'a AliasMap<T> {
///
/// [PATTERN_KEY_COMPARE]: https://nodejs.org/api/esm.html#resolver-algorithm-specification
pub struct AliasMapIntoIter<T> {
iter: patricia_tree::map::IntoIter<BTreeMap<AliasKey, T>>,
iter: patricia_tree::map::IntoIter<Vec<u8>, BTreeMap<AliasKey, T>>,
current_prefix_iterator: Option<AliasMapIntoIterItem<T>>,
}

Expand Down Expand Up @@ -457,7 +396,7 @@ impl<T> Iterator for AliasMapIntoIter<T> {
///
/// [PATTERN_KEY_COMPARE]: https://nodejs.org/api/esm.html#resolver-algorithm-specification
pub struct AliasMapIter<'a, T> {
iter: patricia_tree::map::Iter<'a, BTreeMap<AliasKey, T>>,
iter: patricia_tree::map::Iter<'a, Vec<u8>, BTreeMap<AliasKey, T>>,
current_prefix_iterator: Option<AliasMapIterItem<'a, T>>,
}

Expand Down
Loading