Skip to content

Commit 03c974f

Browse files
committed
PackageSourceMap is now implemented with a no_std compat PackageSources which uses String instead of PathBuf
1 parent 923019a commit 03c974f

File tree

2 files changed

+52
-61
lines changed

2 files changed

+52
-61
lines changed

crates/wit-parser/src/ast.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,6 @@ impl SourceMap {
19071907
return msg;
19081908
}
19091909

1910-
#[cfg(feature = "std")]
19111910
pub(crate) fn render_location(&self, span: Span) -> String {
19121911
let src = self.source_for_offset(span.start);
19131912
let start = src.to_relative_offset(span.start);

crates/wit-parser/src/resolve.rs

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use alloc::borrow::ToOwned;
2-
#[cfg(feature = "std")]
32
use alloc::collections::BTreeMap;
43
use alloc::string::{String, ToString};
54
use alloc::vec::Vec;
@@ -8,7 +7,7 @@ use core::cmp::Ordering;
87
use core::fmt;
98
use core::mem;
109
#[cfg(feature = "std")]
11-
use std::path::{Path, PathBuf};
10+
use std::path::Path;
1211

1312
use crate::*;
1413
use anyhow::{Context, Result, anyhow, bail};
@@ -126,27 +125,18 @@ pub struct Package {
126125

127126
pub type PackageId = Id<Package>;
128127

129-
/// All the sources used during resolving a directory or path.
130-
#[cfg(feature = "std")]
128+
/// Source name mappings for resolved packages (no_std compatible).
131129
#[derive(Clone, Debug)]
132-
pub struct PackageSourceMap {
133-
sources: Vec<Vec<PathBuf>>,
130+
pub struct PackageSources {
131+
sources: Vec<Vec<String>>,
134132
package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
135133
}
136134

137-
#[cfg(feature = "std")]
138-
impl PackageSourceMap {
139-
fn from_single_source(package_id: PackageId, source: &Path) -> Self {
140-
Self {
141-
sources: vec![vec![source.to_path_buf()]],
142-
package_id_to_source_map_idx: BTreeMap::from([(package_id, 0)]),
143-
}
144-
}
145-
135+
impl PackageSources {
146136
fn from_source_maps(
147137
source_maps: Vec<SourceMap>,
148138
package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
149-
) -> PackageSourceMap {
139+
) -> PackageSources {
150140
for (package_id, idx) in &package_id_to_source_map_idx {
151141
if *idx >= source_maps.len() {
152142
panic!(
@@ -161,35 +151,55 @@ impl PackageSourceMap {
161151
Self {
162152
sources: source_maps
163153
.into_iter()
164-
.map(|source_map| {
165-
source_map
166-
.source_files()
167-
.map(|path| path.to_path_buf())
168-
.collect()
169-
})
154+
.map(|source_map| source_map.source_names().map(|s| s.to_owned()).collect())
170155
.collect(),
171156
package_id_to_source_map_idx,
172157
}
173158
}
159+
}
160+
161+
/// All the sources used during resolving a directory or path.
162+
#[cfg(feature = "std")]
163+
#[derive(Clone, Debug)]
164+
pub struct PackageSourceMap {
165+
inner: PackageSources,
166+
}
167+
168+
#[cfg(feature = "std")]
169+
impl PackageSourceMap {
170+
fn from_single_source(package_id: PackageId, source: &Path) -> Self {
171+
Self {
172+
inner: PackageSources {
173+
sources: vec![vec![source.display().to_string()]],
174+
package_id_to_source_map_idx: BTreeMap::from([(package_id, 0)]),
175+
},
176+
}
177+
}
178+
179+
fn from_inner(inner: PackageSources) -> Self {
180+
Self { inner }
181+
}
174182

175183
/// All unique source paths.
176184
pub fn paths(&self) -> impl Iterator<Item = &Path> {
177185
// Usually any two source map should not have duplicated source paths,
178186
// but it can happen, e.g. with using [`Resolve::push_str`] directly.
179187
// To be sure we use a set for deduplication here.
180-
self.sources
188+
self.inner
189+
.sources
181190
.iter()
182191
.flatten()
183-
.map(|path_buf| path_buf.as_ref())
192+
.map(|s| Path::new(s))
184193
.collect::<IndexSet<&Path>>()
185194
.into_iter()
186195
}
187196

188197
/// Source paths for package
189198
pub fn package_paths(&self, id: PackageId) -> Option<impl Iterator<Item = &Path>> {
190-
self.package_id_to_source_map_idx
199+
self.inner
200+
.package_id_to_source_map_idx
191201
.get(&id)
192-
.map(|&idx| self.sources[idx].iter().map(|path_buf| path_buf.as_ref()))
202+
.map(|&idx| self.inner.sources[idx].iter().map(|s| Path::new(s)))
193203
}
194204
}
195205

@@ -201,7 +211,6 @@ enum ParsedFile {
201211
}
202212

203213
/// Visitor helper for performing topological sort on a group of packages.
204-
#[cfg(feature = "std")]
205214
fn visit<'a>(
206215
pkg: &'a UnresolvedPackage,
207216
pkg_details_map: &'a BTreeMap<PackageName, (UnresolvedPackage, usize)>,
@@ -289,12 +298,11 @@ impl Resolve {
289298
}
290299
}
291300

292-
#[cfg(feature = "std")]
293301
fn sort_unresolved_packages(
294302
&mut self,
295303
main: UnresolvedPackageGroup,
296304
deps: Vec<UnresolvedPackageGroup>,
297-
) -> Result<(PackageId, PackageSourceMap)> {
305+
) -> Result<(PackageId, PackageSources)> {
298306
let mut pkg_details_map = BTreeMap::new();
299307
let mut source_maps = Vec::new();
300308

@@ -337,16 +345,18 @@ package {name} is defined in two different locations:\n\
337345
// and otherwise determine the order that packages must be added to
338346
// this `Resolve`.
339347
let mut order = IndexSet::default();
340-
let mut visiting = HashSet::new();
341-
for pkg_details in pkg_details_map.values() {
342-
let (pkg, _) = pkg_details;
343-
visit(
344-
pkg,
345-
&pkg_details_map,
346-
&mut order,
347-
&mut visiting,
348-
&source_maps,
349-
)?;
348+
{
349+
let mut visiting = HashSet::new();
350+
for pkg_details in pkg_details_map.values() {
351+
let (pkg, _) = pkg_details;
352+
visit(
353+
pkg,
354+
&pkg_details_map,
355+
&mut order,
356+
&mut visiting,
357+
&source_maps,
358+
)?;
359+
}
350360
}
351361

352362
// Ensure that the final output is topologically sorted. Use a set to ensure that we render
@@ -368,7 +378,7 @@ package {name} is defined in two different locations:\n\
368378

369379
Ok((
370380
main_pkg_id.unwrap(),
371-
PackageSourceMap::from_source_maps(source_maps, package_id_to_source_map_idx),
381+
PackageSources::from_source_maps(source_maps, package_id_to_source_map_idx),
372382
))
373383
}
374384

@@ -419,7 +429,8 @@ package {name} is defined in two different locations:\n\
419429
.parse_deps_dir(&deps)
420430
.with_context(|| format!("failed to parse dependency directory: {}", deps.display()))?;
421431

422-
self.sort_unresolved_packages(top_pkg, deps)
432+
let (pkg_id, inner) = self.sort_unresolved_packages(top_pkg, deps)?;
433+
Ok((pkg_id, PackageSourceMap::from_inner(inner)))
423434
}
424435

425436
#[cfg(feature = "std")]
@@ -563,30 +574,11 @@ package {name} is defined in two different locations:\n\
563574
/// which corresponds to the package that was just inserted.
564575
///
565576
/// The returned [`PackageId`]s are listed in topologically sorted order.
566-
#[cfg(feature = "std")]
567577
pub fn push_group(&mut self, unresolved_group: UnresolvedPackageGroup) -> Result<PackageId> {
568578
let (pkg_id, _) = self.sort_unresolved_packages(unresolved_group, Vec::new())?;
569579
Ok(pkg_id)
570580
}
571581

572-
/// The returned [`PackageId`]s are listed in topologically sorted order.
573-
#[cfg(not(feature = "std"))]
574-
pub fn push_group(&mut self, unresolved_group: UnresolvedPackageGroup) -> Result<PackageId> {
575-
let UnresolvedPackageGroup {
576-
main,
577-
nested,
578-
source_map,
579-
} = unresolved_group;
580-
581-
// Add nested packages first
582-
for pkg in nested {
583-
self.push(pkg, &source_map)?;
584-
}
585-
586-
// Add the main package
587-
self.push(main, &source_map)
588-
}
589-
590582
/// Convenience method for combining [`UnresolvedPackageGroup::parse`] and
591583
/// [`Resolve::push_group`].
592584
///

0 commit comments

Comments
 (0)