@@ -29,8 +29,8 @@ mod test;
2929use anyhow:: { bail, Context , Result } ;
3030use schemars:: JsonSchema ;
3131use serde:: Deserialize ;
32- use source:: Source ;
33- use std:: path:: PathBuf ;
32+ use source:: { workspace :: WorkspaceConfig , Source } ;
33+ use std:: path:: { Path , PathBuf } ;
3434use tracing:: log;
3535
3636/// Common configuration model functionality
@@ -114,6 +114,59 @@ impl ConfigModel for Configuration {
114114 }
115115}
116116
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" ) ;
122+ if cargo_toml. exists ( ) {
123+ if let Ok ( workspace) = WorkspaceConfig :: new ( & 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+ }
164+ }
165+ }
166+ }
167+ Ok ( None )
168+ }
169+
117170/// Locate and load the configuration, given an optional file or directory. Falling back to the
118171/// current directory.
119172pub async fn load ( path : Option < PathBuf > ) -> Result < ( Configuration , PathBuf ) > {
0 commit comments