1
1
use std:: collections:: { BTreeMap , BTreeSet } ;
2
2
3
+ use crate :: metadata:: CargoTomlPath ;
3
4
use anyhow:: { anyhow, bail, Context , Result } ;
4
- use camino:: { Utf8Path , Utf8PathBuf } ;
5
+ use camino:: Utf8Path ;
5
6
use cargo_toml:: Manifest ;
6
7
7
8
/// A description of Cargo.toml files and how they are related in workspaces.
8
9
/// All `Utf8PathBuf` values are paths of Cargo.toml files.
9
10
#[ derive( Debug , PartialEq ) ]
10
11
pub ( crate ) struct DiscoveredWorkspaces {
11
- workspaces_to_members : BTreeMap < Utf8PathBuf , BTreeSet < Utf8PathBuf > > ,
12
- non_workspaces : BTreeSet < Utf8PathBuf > ,
12
+ workspaces_to_members : BTreeMap < CargoTomlPath , BTreeSet < CargoTomlPath > > ,
13
+ non_workspaces : BTreeSet < CargoTomlPath > ,
13
14
}
14
15
15
16
impl DiscoveredWorkspaces {
16
- pub ( crate ) fn workspaces ( & self ) -> BTreeSet < Utf8PathBuf > {
17
+ pub ( crate ) fn workspaces ( & self ) -> BTreeSet < CargoTomlPath > {
17
18
self . workspaces_to_members . keys ( ) . cloned ( ) . collect ( )
18
19
}
19
20
20
- pub ( crate ) fn all_workspaces_and_members ( & self ) -> BTreeSet < Utf8PathBuf > {
21
+ pub ( crate ) fn all_workspaces_and_members ( & self ) -> BTreeSet < CargoTomlPath > {
21
22
self . workspaces_to_members
22
23
. keys ( )
23
24
. chain ( self . workspaces_to_members . values ( ) . flatten ( ) )
@@ -27,8 +28,8 @@ impl DiscoveredWorkspaces {
27
28
}
28
29
29
30
pub ( crate ) fn discover_workspaces (
30
- cargo_toml_paths : BTreeSet < Utf8PathBuf > ,
31
- known_manifests : & BTreeMap < Utf8PathBuf , Manifest > ,
31
+ cargo_toml_paths : BTreeSet < CargoTomlPath > ,
32
+ known_manifests : & BTreeMap < CargoTomlPath , Manifest > ,
32
33
) -> Result < DiscoveredWorkspaces > {
33
34
let mut manifest_cache = ManifestCache {
34
35
cache : BTreeMap :: new ( ) ,
@@ -38,7 +39,7 @@ pub(crate) fn discover_workspaces(
38
39
}
39
40
40
41
fn discover_workspaces_with_cache (
41
- cargo_toml_paths : BTreeSet < Utf8PathBuf > ,
42
+ cargo_toml_paths : BTreeSet < CargoTomlPath > ,
42
43
manifest_cache : & mut ManifestCache ,
43
44
) -> Result < DiscoveredWorkspaces > {
44
45
let mut discovered_workspaces = DiscoveredWorkspaces {
@@ -85,7 +86,7 @@ fn discover_workspaces_with_cache(
85
86
} )
86
87
. transpose ( ) ?;
87
88
88
- ' per_child: for entry in walkdir:: WalkDir :: new ( workspace_path. parent ( ) . unwrap ( ) )
89
+ ' per_child: for entry in walkdir:: WalkDir :: new ( workspace_path. parent ( ) )
89
90
. follow_links ( false )
90
91
. follow_root_links ( false )
91
92
. into_iter ( )
@@ -117,6 +118,7 @@ fn discover_workspaces_with_cache(
117
118
let child_path = Utf8Path :: from_path ( entry. path ( ) )
118
119
. ok_or_else ( || anyhow ! ( "Failed to parse {:?} as UTF-8" , entry. path( ) ) ) ?
119
120
. to_path_buf ( ) ;
121
+ let child_path = CargoTomlPath :: unchecked_new ( child_path) ;
120
122
if child_path == workspace_path {
121
123
continue ;
122
124
}
@@ -136,7 +138,7 @@ fn discover_workspaces_with_cache(
136
138
)
137
139
} ) ?;
138
140
actual_workspace_path =
139
- child_path. parent ( ) . unwrap ( ) . join ( explicit_workspace_path) ;
141
+ CargoTomlPath :: new ( child_path. parent ( ) . join ( explicit_workspace_path) ) ? ;
140
142
}
141
143
}
142
144
if !discovered_workspaces
@@ -146,10 +148,8 @@ fn discover_workspaces_with_cache(
146
148
bail ! ( "Found manifest at {} which is a member of the workspace at {} which isn't included in the crates_universe" , child_path, actual_workspace_path) ;
147
149
}
148
150
149
- let dir_relative_to_workspace_dir = child_path
150
- . parent ( )
151
- . unwrap ( )
152
- . strip_prefix ( workspace_path. parent ( ) . unwrap ( ) ) ;
151
+ let dir_relative_to_workspace_dir =
152
+ child_path. parent ( ) . strip_prefix ( workspace_path. parent ( ) ) ;
153
153
154
154
if let Ok ( dir_relative_to_workspace_dir) = dir_relative_to_workspace_dir {
155
155
use itertools:: Itertools ;
@@ -201,11 +201,11 @@ fn discover_workspaces_with_cache(
201
201
}
202
202
203
203
fn discover_workspace_parent (
204
- cargo_toml_path : & Utf8PathBuf ,
204
+ cargo_toml_path : & CargoTomlPath ,
205
205
manifest_cache : & mut ManifestCache ,
206
- ) -> Option < Utf8PathBuf > {
206
+ ) -> Option < CargoTomlPath > {
207
207
for parent_dir in cargo_toml_path. ancestors ( ) . skip ( 1 ) {
208
- let maybe_cargo_toml_path = parent_dir . join ( "Cargo.toml" ) ;
208
+ let maybe_cargo_toml_path = CargoTomlPath :: for_dir ( parent_dir ) ;
209
209
let maybe_manifest = manifest_cache. get ( & maybe_cargo_toml_path) ;
210
210
if let Some ( manifest) = maybe_manifest {
211
211
if manifest. workspace . is_some ( ) {
@@ -217,12 +217,12 @@ fn discover_workspace_parent(
217
217
}
218
218
219
219
struct ManifestCache < ' a > {
220
- cache : BTreeMap < Utf8PathBuf , Option < Manifest > > ,
221
- known_manifests : & ' a BTreeMap < Utf8PathBuf , Manifest > ,
220
+ cache : BTreeMap < CargoTomlPath , Option < Manifest > > ,
221
+ known_manifests : & ' a BTreeMap < CargoTomlPath , Manifest > ,
222
222
}
223
223
224
224
impl ManifestCache < ' _ > {
225
- fn get ( & mut self , path : & Utf8PathBuf ) -> Option < Manifest > {
225
+ fn get ( & mut self , path : & CargoTomlPath ) -> Option < Manifest > {
226
226
if let Some ( manifest) = self . known_manifests . get ( path) {
227
227
return Some ( manifest. clone ( ) ) ;
228
228
}
@@ -417,7 +417,7 @@ mod test {
417
417
fn ws1_discovered_workspaces ( root_dir : & Utf8Path ) -> DiscoveredWorkspaces {
418
418
let mut workspaces_to_members = BTreeMap :: new ( ) ;
419
419
workspaces_to_members. insert (
420
- root_dir. join ( "ws1" ) . join ( "Cargo.toml" ) ,
420
+ CargoTomlPath :: for_dir ( root_dir. join ( "ws1" ) ) ,
421
421
BTreeSet :: from ( [
422
422
root_dir. join ( "ws1" ) . join ( "ws1c1" ) . join ( "Cargo.toml" ) ,
423
423
root_dir
0 commit comments