Skip to content

Commit f6bd821

Browse files
committed
fmt
1 parent 853309d commit f6bd821

4 files changed

Lines changed: 25 additions & 39 deletions

File tree

crates/pixi_api/src/workspace/registry/mod.rs

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
use std::collections::HashMap;
22

3-
use serde::{Deserialize, Serialize};
43
use miette::{Context, IntoDiagnostic};
4+
use serde::{Deserialize, Serialize};
55

66
use std::path::PathBuf;
77

8-
use pixi_consts::consts;
98
use pixi_config::pixi_home;
10-
9+
use pixi_consts::consts;
1110

1211
/// Returns the path to the workspace registry file
1312
pub 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")]
1918
pub 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-
3425
impl 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

crates/pixi_cli/src/cli_config.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ impl WorkspaceConfig {
4848
if let Some(manifest_path) = &self.manifest_path {
4949
DiscoveryStart::ExplicitManifest(manifest_path.clone())
5050
} else if let Some(workspace) = &self.workspace {
51-
let workspace_registry = WorkspaceRegistry::load().expect("Unable to load workspace registry");
52-
let path = workspace_registry.named_workspace(&workspace).expect("Unable to find workspace");
51+
let workspace_registry =
52+
WorkspaceRegistry::load().expect("Unable to load workspace registry");
53+
let path = workspace_registry
54+
.named_workspace(workspace)
55+
.expect("Unable to find workspace");
5356
DiscoveryStart::ExplicitManifest(path)
5457
} else {
5558
DiscoveryStart::CurrentDir

crates/pixi_cli/src/workspace/register.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ pub enum Command {
5656
// Prune(PruneArgs),
5757
}
5858

59-
6059
pub async fn execute(args: Args) -> miette::Result<()> {
6160
match args.command {
6261
Some(Command::List(args)) => {
@@ -78,7 +77,9 @@ pub async fn execute(args: Args) -> miette::Result<()> {
7877
}
7978
Some(Command::Remove(remove_args)) => {
8079
let mut workspace_registry = WorkspaceRegistry::load()?;
81-
workspace_registry.remove_workspace(&remove_args.name).await?;
80+
workspace_registry
81+
.remove_workspace(&remove_args.name)
82+
.await?;
8283

8384
eprintln!(
8485
"{} Workspace '{}' has been removed from the registry successfully.",
@@ -116,7 +117,9 @@ pub async fn execute(args: Args) -> miette::Result<()> {
116117
let target_path = args.path.unwrap_or_else(|| workspace.root().to_path_buf());
117118

118119
let mut workspace_registry = WorkspaceRegistry::load()?;
119-
workspace_registry.add_workspace(target_name, target_path).await?;
120+
workspace_registry
121+
.add_workspace(target_name, target_path)
122+
.await?;
120123

121124
eprintln!(
122125
"{} {}",

crates/pixi_consts/src/consts.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ pub const CACHED_SOURCE_BUILDS: &str = "pkgs";
4949
pub const DEFAULT_GLOBAL_WORKSPACE_DIR: &str = "workspaces";
5050
pub const WORKSPACES_REGISTRY: &str = "workspaces.toml";
5151

52-
5352
/// The directory relative to the .pixi folder that stores build related caches.
5453
pub const WORKSPACE_CACHE_DIR: &str = "build";
5554

0 commit comments

Comments
 (0)