summaryrefslogtreecommitdiff
path: root/systems/workspace/src/func.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-03-11 22:52:25 +0800
committer魏曹先生 <1992414357@qq.com>2026-03-11 22:52:25 +0800
commit085bce616f68eb6c1586f7a7e1089b5f4cdd0155 (patch)
tree5782ae03692754845958ef06b92c56805ddd7bbc /systems/workspace/src/func.rs
parent55c7ea778be2f3ce44d88440607ac8d4117e31d2 (diff)
Add vault and workspace systems with config management
Diffstat (limited to 'systems/workspace/src/func.rs')
-rw-r--r--systems/workspace/src/func.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/systems/workspace/src/func.rs b/systems/workspace/src/func.rs
new file mode 100644
index 0000000..4a1e57d
--- /dev/null
+++ b/systems/workspace/src/func.rs
@@ -0,0 +1,45 @@
+use std::{env::current_dir, path::PathBuf};
+
+use asset_system::asset::Handle;
+use framework::space::Space;
+
+use crate::workspace::{
+ Workspace, config::WorkspaceConfig, error::WorkspaceOperationError, manager::WorkspaceManager,
+};
+
+/// Create a workspace at the current location
+pub async fn create_workspace_here() -> Result<(), WorkspaceOperationError> {
+ create_workspace(current_dir()?).await
+}
+
+/// Create a workspace at the specified location
+pub async fn create_workspace(path: impl Into<PathBuf>) -> Result<(), WorkspaceOperationError> {
+ let path = path.into();
+ let mut space = Space::new(Workspace);
+ space.set_current_dir(path)?;
+ space.init_here().await?;
+
+ Ok(())
+}
+
+#[allow(unused)]
+/// Get a handle to the workspace configuration file and edit its content
+pub async fn operate_workspace_config<F>(
+ workspace_path: impl Into<PathBuf>,
+ operate: F,
+) -> Result<(), WorkspaceOperationError>
+where
+ F: AsyncFn(Handle<WorkspaceConfig>),
+{
+ // Get the workspace manager and set the context to the given workspace path
+ let mut mgr = WorkspaceManager::new();
+ mgr.get_space_mut().set_current_dir(workspace_path.into())?;
+
+ // Obtain the read-only asset of the workspace configuration file,
+ // lock it to get a writable handle, and pass it to the function for execution
+ let config = mgr.workspace_config()?;
+ let handle = config.get_handle().await?;
+ operate(handle).await;
+
+ Ok(())
+}