11use std:: collections:: HashMap ;
22
3- use serde:: { Deserialize , Serialize } ;
43use miette:: { Context , IntoDiagnostic } ;
4+ use serde:: { Deserialize , Serialize } ;
55
66use std:: path:: PathBuf ;
77
8- use pixi_consts:: consts;
98use pixi_config:: pixi_home;
10-
9+ use pixi_consts :: consts ;
1110
1211/// Returns the path to the workspace registry file
1312pub fn workspace_registry_path ( ) -> Option < PathBuf > {
1413 pixi_home ( ) . map ( |d| d. join ( consts:: WORKSPACES_REGISTRY ) )
1514}
1615
17- #[ derive( Clone , Debug , Deserialize , Serialize , PartialEq , Eq ) ]
16+ #[ derive( Clone , Debug , Deserialize , Serialize , PartialEq , Eq , Default ) ]
1817#[ serde( rename_all = "kebab-case" ) ]
1918pub struct WorkspaceRegistry {
2019 /// Mapping of a named workspaces to the path of their manifest file.
@@ -23,14 +22,6 @@ pub struct WorkspaceRegistry {
2322 pub named_workspaces : HashMap < String , PathBuf > ,
2423}
2524
26- impl Default for WorkspaceRegistry {
27- fn default ( ) -> Self {
28- Self {
29- named_workspaces : HashMap :: new ( ) ,
30- }
31- }
32- }
33-
3425impl WorkspaceRegistry {
3526 /// Loads the workspace registry from disk, if it exists.
3627 pub fn load ( ) -> miette:: Result < Self > {
@@ -39,22 +30,23 @@ impl WorkspaceRegistry {
3930
4031 let contents = match fs_err:: read_to_string ( & path) {
4132 Ok ( c) => c,
42- Err ( e) if e. kind ( ) == std:: io:: ErrorKind :: NotFound || e. kind ( ) == std:: io:: ErrorKind :: NotADirectory => {
33+ Err ( e)
34+ if e. kind ( ) == std:: io:: ErrorKind :: NotFound
35+ || e. kind ( ) == std:: io:: ErrorKind :: NotADirectory =>
36+ {
4337 // File doesn't exist yet, return default
4438 return Ok ( WorkspaceRegistry :: default ( ) ) ;
4539 }
4640 Err ( e) => {
47- return Err ( e)
48- . into_diagnostic ( ) ?;
41+ return Err ( e) . into_diagnostic ( ) ?;
4942 }
5043 } ;
5144
52- let de = toml_edit:: de:: Deserializer :: parse ( & contents)
53- . into_diagnostic ( ) ?;
45+ let de = toml_edit:: de:: Deserializer :: parse ( & contents) . into_diagnostic ( ) ?;
5446
5547 // Deserialize the contents
56- let registry: WorkspaceRegistry = serde_ignored :: deserialize ( de , |_| { } )
57- . into_diagnostic ( ) ?;
48+ let registry: WorkspaceRegistry =
49+ serde_ignored :: deserialize ( de , |_| { } ) . into_diagnostic ( ) ?;
5850
5951 Ok ( registry)
6052 }
@@ -64,7 +56,6 @@ impl WorkspaceRegistry {
6456 let path = workspace_registry_path ( )
6557 . ok_or_else ( || miette:: miette!( "Unable to determine pixi home directory" ) ) ?;
6658
67-
6859 if let Some ( parent) = path. parent ( ) {
6960 fs_err:: create_dir_all ( parent)
7061 . into_diagnostic ( )
@@ -86,25 +77,15 @@ impl WorkspaceRegistry {
8677 self . named_workspaces . remove ( name) ;
8778 self . save ( ) . await ?;
8879 } else {
89- return Err (
90- miette:: diagnostic!( "Workspace '{}' is not found." , name, ) . into ( ) ,
91- ) ;
80+ return Err ( miette:: diagnostic!( "Workspace '{}' is not found." , name, ) . into ( ) ) ;
9281 }
9382 Ok ( ( ) )
9483 }
9584
9685 /// Add a workspace to the registry given the name and path association
9786 pub async fn add_workspace ( & mut self , name : String , path : PathBuf ) -> miette:: Result < ( ) > {
98- if self . named_workspaces . contains_key ( & name) {
99- return Err ( miette:: diagnostic!(
100- "Workspace with name '{}' is already registered." ,
101- name,
102- )
103- . into ( ) ) ;
104- } else {
105- self . named_workspaces . insert ( name, path) ;
106- self . save ( ) . await ?;
107- }
87+ self . named_workspaces . entry ( name) . or_insert ( path) ;
88+ self . save ( ) . await ?;
10889 Ok ( ( ) )
10990 }
11091
0 commit comments