Skip to content

Commit bf13296

Browse files
committed
move logic from PackageSourceMap to PackageSources
1 parent 3acc7ab commit bf13296

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

crates/wit-parser/src/resolve/fs.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
//! Filesystem operations for [`Resolve`].
22
3-
use alloc::collections::BTreeMap;
43
use alloc::format;
5-
use alloc::string::ToString;
6-
use alloc::vec;
74
use std::path::Path;
85
use std::vec::Vec;
96

107
use anyhow::{Context, Result, bail};
118

129
use super::{PackageSources, Resolve};
13-
use crate::{IndexSet, UnresolvedPackageGroup};
10+
use crate::UnresolvedPackageGroup;
1411

1512
/// All the sources used during resolving a directory or path.
1613
#[derive(Clone, Debug)]
@@ -24,10 +21,7 @@ impl PackageSourceMap {
2421
.to_str()
2522
.ok_or_else(|| anyhow::anyhow!("path is not valid utf-8: {:?}", source))?;
2623
Ok(Self {
27-
inner: PackageSources {
28-
sources: vec![vec![path_str.to_string()]],
29-
package_id_to_source_map_idx: BTreeMap::from([(package_id, 0)]),
30-
},
24+
inner: PackageSources::from_single_source(package_id, path_str),
3125
})
3226
}
3327

@@ -37,24 +31,14 @@ impl PackageSourceMap {
3731

3832
/// All unique source paths.
3933
pub fn paths(&self) -> impl Iterator<Item = &Path> {
40-
// Usually any two source map should not have duplicated source paths,
41-
// but it can happen, e.g. with using [`Resolve::push_str`] directly.
42-
// To be sure we use a set for deduplication here.
43-
self.inner
44-
.sources
45-
.iter()
46-
.flatten()
47-
.map(|s| Path::new(s))
48-
.collect::<IndexSet<&Path>>()
49-
.into_iter()
34+
self.inner.source_names().map(Path::new)
5035
}
5136

5237
/// Source paths for package
5338
pub fn package_paths(&self, id: super::PackageId) -> Option<impl Iterator<Item = &Path>> {
5439
self.inner
55-
.package_id_to_source_map_idx
56-
.get(&id)
57-
.map(|&idx| self.inner.sources[idx].iter().map(|s| Path::new(s)))
40+
.package_source_names(id)
41+
.map(|iter| iter.map(Path::new))
5842
}
5943
}
6044

crates/wit-parser/src/resolve/mod.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,19 @@ pub type PackageId = Id<Package>;
131131
/// Source name mappings for resolved packages (no_std compatible).
132132
#[derive(Clone, Debug)]
133133
pub struct PackageSources {
134-
pub(crate) sources: Vec<Vec<String>>,
135-
pub(crate) package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
134+
sources: Vec<Vec<String>>,
135+
package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
136136
}
137137

138138
impl PackageSources {
139+
#[cfg(feature = "std")]
140+
fn from_single_source(package_id: PackageId, source: &str) -> Self {
141+
Self {
142+
sources: vec![vec![source.to_owned()]],
143+
package_id_to_source_map_idx: BTreeMap::from([(package_id, 0)]),
144+
}
145+
}
146+
139147
fn from_source_maps(
140148
source_maps: Vec<SourceMap>,
141149
package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
@@ -159,6 +167,23 @@ impl PackageSources {
159167
package_id_to_source_map_idx,
160168
}
161169
}
170+
171+
/// All unique source names.
172+
pub fn source_names(&self) -> impl Iterator<Item = &str> {
173+
self.sources
174+
.iter()
175+
.flatten()
176+
.map(|s| s.as_str())
177+
.collect::<IndexSet<&str>>()
178+
.into_iter()
179+
}
180+
181+
/// Source names for a specific package.
182+
pub fn package_source_names(&self, id: PackageId) -> Option<impl Iterator<Item = &str>> {
183+
self.package_id_to_source_map_idx
184+
.get(&id)
185+
.map(|&idx| self.sources[idx].iter().map(|s| s.as_str()))
186+
}
162187
}
163188

164189
/// Visitor helper for performing topological sort on a group of packages.

0 commit comments

Comments
 (0)