11use std:: collections:: { BTreeMap , BTreeSet } ;
22
3+ use crate :: metadata:: CargoTomlPath ;
34use anyhow:: { anyhow, bail, Context , Result } ;
4- use camino:: { Utf8Path , Utf8PathBuf } ;
5+ use camino:: Utf8Path ;
56use cargo_toml:: Manifest ;
67
78/// A description of Cargo.toml files and how they are related in workspaces.
89/// All `Utf8PathBuf` values are paths of Cargo.toml files.
910#[ derive( Debug , PartialEq ) ]
1011pub ( 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 > ,
1314}
1415
1516impl DiscoveredWorkspaces {
16- pub ( crate ) fn workspaces ( & self ) -> BTreeSet < Utf8PathBuf > {
17+ pub ( crate ) fn workspaces ( & self ) -> BTreeSet < CargoTomlPath > {
1718 self . workspaces_to_members . keys ( ) . cloned ( ) . collect ( )
1819 }
1920
20- pub ( crate ) fn all_workspaces_and_members ( & self ) -> BTreeSet < Utf8PathBuf > {
21+ pub ( crate ) fn all_workspaces_and_members ( & self ) -> BTreeSet < CargoTomlPath > {
2122 self . workspaces_to_members
2223 . keys ( )
2324 . chain ( self . workspaces_to_members . values ( ) . flatten ( ) )
@@ -27,8 +28,8 @@ impl DiscoveredWorkspaces {
2728}
2829
2930pub ( 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 > ,
3233) -> Result < DiscoveredWorkspaces > {
3334 let mut manifest_cache = ManifestCache {
3435 cache : BTreeMap :: new ( ) ,
@@ -38,7 +39,7 @@ pub(crate) fn discover_workspaces(
3839}
3940
4041fn discover_workspaces_with_cache (
41- cargo_toml_paths : BTreeSet < Utf8PathBuf > ,
42+ cargo_toml_paths : BTreeSet < CargoTomlPath > ,
4243 manifest_cache : & mut ManifestCache ,
4344) -> Result < DiscoveredWorkspaces > {
4445 let mut discovered_workspaces = DiscoveredWorkspaces {
@@ -85,7 +86,7 @@ fn discover_workspaces_with_cache(
8586 } )
8687 . transpose ( ) ?;
8788
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 ( ) )
8990 . follow_links ( false )
9091 . follow_root_links ( false )
9192 . into_iter ( )
@@ -117,6 +118,7 @@ fn discover_workspaces_with_cache(
117118 let child_path = Utf8Path :: from_path ( entry. path ( ) )
118119 . ok_or_else ( || anyhow ! ( "Failed to parse {:?} as UTF-8" , entry. path( ) ) ) ?
119120 . to_path_buf ( ) ;
121+ let child_path = CargoTomlPath :: unchecked_new ( child_path) ;
120122 if child_path == workspace_path {
121123 continue ;
122124 }
@@ -136,7 +138,7 @@ fn discover_workspaces_with_cache(
136138 )
137139 } ) ?;
138140 actual_workspace_path =
139- child_path. parent ( ) . unwrap ( ) . join ( explicit_workspace_path) ;
141+ CargoTomlPath :: new ( child_path. parent ( ) . join ( explicit_workspace_path) ) ? ;
140142 }
141143 }
142144 if !discovered_workspaces
@@ -146,10 +148,8 @@ fn discover_workspaces_with_cache(
146148 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) ;
147149 }
148150
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 ( ) ) ;
153153
154154 if let Ok ( dir_relative_to_workspace_dir) = dir_relative_to_workspace_dir {
155155 use itertools:: Itertools ;
@@ -201,11 +201,11 @@ fn discover_workspaces_with_cache(
201201}
202202
203203fn discover_workspace_parent (
204- cargo_toml_path : & Utf8PathBuf ,
204+ cargo_toml_path : & CargoTomlPath ,
205205 manifest_cache : & mut ManifestCache ,
206- ) -> Option < Utf8PathBuf > {
206+ ) -> Option < CargoTomlPath > {
207207 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 ) ;
209209 let maybe_manifest = manifest_cache. get ( & maybe_cargo_toml_path) ;
210210 if let Some ( manifest) = maybe_manifest {
211211 if manifest. workspace . is_some ( ) {
@@ -217,12 +217,12 @@ fn discover_workspace_parent(
217217}
218218
219219struct 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 > ,
222222}
223223
224224impl ManifestCache < ' _ > {
225- fn get ( & mut self , path : & Utf8PathBuf ) -> Option < Manifest > {
225+ fn get ( & mut self , path : & CargoTomlPath ) -> Option < Manifest > {
226226 if let Some ( manifest) = self . known_manifests . get ( path) {
227227 return Some ( manifest. clone ( ) ) ;
228228 }
@@ -417,7 +417,7 @@ mod test {
417417 fn ws1_discovered_workspaces ( root_dir : & Utf8Path ) -> DiscoveredWorkspaces {
418418 let mut workspaces_to_members = BTreeMap :: new ( ) ;
419419 workspaces_to_members. insert (
420- root_dir. join ( "ws1" ) . join ( "Cargo.toml" ) ,
420+ CargoTomlPath :: for_dir ( root_dir. join ( "ws1" ) ) ,
421421 BTreeSet :: from ( [
422422 root_dir. join ( "ws1" ) . join ( "ws1c1" ) . join ( "Cargo.toml" ) ,
423423 root_dir
0 commit comments