@@ -30,7 +30,7 @@ use anyhow::{bail, Context, Result};
3030use schemars:: JsonSchema ;
3131use serde:: Deserialize ;
3232use source:: { workspace, Source } ;
33- use std:: path:: PathBuf ;
33+ use std:: path:: { Path , PathBuf } ;
3434use tracing:: log;
3535
3636/// Common configuration model functionality
@@ -114,16 +114,53 @@ impl ConfigModel for Configuration {
114114 }
115115}
116116
117- pub async fn load_workspace_config ( path : & PathBuf ) -> Result < Option < PathBuf > > {
118- let cargo_toml = path. clone ( ) . join ( "Cargo.toml" ) ;
117+ pub async fn load_workspace_config (
118+ current_path : & Path ,
119+ workspace_name : Option < String > ,
120+ ) -> Result < Option < PathBuf > > {
121+ let cargo_toml = current_path. join ( "Cargo.toml" ) ;
119122 if cargo_toml. exists ( ) {
120- if let Ok ( workspace) = workspace:: workspace_from_manifest ( cargo_toml) . await {
121- if let Some ( workspace) = workspace. get_default_workspace ( ) {
122- // get the parent directory of the workspace
123- let workspace = workspace
124- . parent ( )
125- . context ( "unable to get parent directory of workspace" ) ?;
126- return Ok ( Some ( workspace. to_path_buf ( ) ) ) ;
123+ if let Ok ( workspace) = workspace:: workspace_from_manifest ( & cargo_toml) . await {
124+ match workspace_name {
125+ Some ( name) => {
126+ if let Some ( workspace) = workspace. get_workspace_by_name ( & name) {
127+ // get the parent directory of the workspace
128+ let workspace = match workspace. parent ( ) {
129+ Some ( parent) => parent,
130+ None => {
131+ return Err ( anyhow:: format_err!(
132+ "unable to get parent directory of workspace '{}'" ,
133+ workspace. display( )
134+ ) ) ;
135+ }
136+ } ;
137+ return Ok ( Some ( workspace. to_path_buf ( ) ) ) ;
138+ }
139+ return Err ( anyhow:: format_err!(
140+ "workspace '{}' not found in {}" ,
141+ name,
142+ cargo_toml. display( )
143+ ) ) ;
144+ }
145+ None => {
146+ if let Some ( workspace) = workspace. get_default_workspace ( ) {
147+ // get the parent directory of the workspace
148+ let workspace = match workspace. parent ( ) {
149+ Some ( parent) => parent,
150+ None => {
151+ return Err ( anyhow:: format_err!(
152+ "unable to get parent directory of workspace '{}'" ,
153+ workspace. display( )
154+ ) ) ;
155+ }
156+ } ;
157+ return Ok ( Some ( workspace. to_path_buf ( ) ) ) ;
158+ }
159+ return Err ( anyhow:: format_err!(
160+ "default workspace not found in {}" ,
161+ cargo_toml. display( )
162+ ) ) ;
163+ }
127164 }
128165 }
129166 }
@@ -152,25 +189,12 @@ pub async fn load(path: Option<PathBuf>) -> Result<(Configuration, PathBuf)> {
152189 Ok ( ( Source :: File ( path) . load ( ) . await ?, cwd) )
153190 }
154191 // if we have a directory, try finding a file and load it
155- Some ( path) if path. is_dir ( ) => {
156- let cwd = if let Some ( new_cwd) = load_workspace_config ( & path) . await ? {
157- new_cwd
158- } else {
159- path. clone ( )
160- } ;
161-
162- Ok ( ( Source :: find ( & path) ?. load ( ) . await ?, cwd) )
163- }
192+ Some ( path) if path. is_dir ( ) => Ok ( ( Source :: find ( & path) ?. load ( ) . await ?, path) ) ,
164193 // if we have something else, we can't deal with it
165194 Some ( path) => bail ! ( "{} is neither a file nor a directory" , path. display( ) ) ,
166195 // if we have nothing, try to find a file in the current directory and load it
167196 None => {
168197 let cwd = std:: env:: current_dir ( ) . context ( "unable to get current directory" ) ?;
169- let cwd = if let Some ( new_cwd) = load_workspace_config ( & cwd) . await ? {
170- new_cwd
171- } else {
172- cwd
173- } ;
174198 Ok ( ( Source :: find ( & cwd) ?. load ( ) . await ?, cwd) )
175199 }
176200 }
0 commit comments