summaryrefslogtreecommitdiff
path: root/crates/vcs_actions/src
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-12-15 10:05:37 +0800
committer魏曹先生 <1992414357@qq.com>2025-12-15 10:05:37 +0800
commitad60d1f5e46e0fbe2a5463a511c4fccc9b5198b5 (patch)
tree1929fd1eb6a14a57b5875478e9eacea644e767d2 /crates/vcs_actions/src
parenta2906e73faa8ed45976d83e8ec88c3e6e34c24c8 (diff)
Add edit mapping action for sheet operations
Diffstat (limited to 'crates/vcs_actions/src')
-rw-r--r--crates/vcs_actions/src/actions/sheet_actions.rs162
-rw-r--r--crates/vcs_actions/src/registry/client_registry.rs5
-rw-r--r--crates/vcs_actions/src/registry/server_registry.rs5
3 files changed, 151 insertions, 21 deletions
diff --git a/crates/vcs_actions/src/actions/sheet_actions.rs b/crates/vcs_actions/src/actions/sheet_actions.rs
index ff467d9..e8d9c94 100644
--- a/crates/vcs_actions/src/actions/sheet_actions.rs
+++ b/crates/vcs_actions/src/actions/sheet_actions.rs
@@ -1,12 +1,21 @@
-use std::io::ErrorKind;
+use std::{collections::HashMap, io::ErrorKind};
use action_system::{action::ActionContext, macros::action_gen};
use serde::{Deserialize, Serialize};
use tcp_connection::error::TcpTargetError;
-use vcs_data::data::{local::vault_modified::sign_vault_modified, sheet::SheetName};
+use vcs_data::data::{
+ local::{
+ file_status::{FromRelativePathBuf, ToRelativePathBuf},
+ vault_modified::sign_vault_modified,
+ },
+ sheet::SheetName,
+};
use crate::{
- actions::{auth_member, check_connection_instance, try_get_local_workspace, try_get_vault},
+ actions::{
+ auth_member, check_connection_instance, get_current_sheet_name, try_get_local_workspace,
+ try_get_vault,
+ },
write_and_return,
};
@@ -200,19 +209,134 @@ pub async fn drop_sheet_action(
Err(TcpTargetError::NoResult("No result.".to_string()))
}
-// #[derive(Serialize, Deserialize)]
-// pub enum AlignSheetActionResult {
-// Success,
-// }
-
-// #[derive(Serialize, Deserialize)]
-// pub struct AlignSheetActionArguments {
-// pub
-// }
-
-// #[action_gen]
-// pub async fn align_sheet_action(
-// ctx: ActionContext,
-// args: AlignSheetActionArgument,
-// ) -> Result<DropSheetActionResult, TcpTargetError> {
-// }
+pub type OperationArgument = (EditMappingOperations, Option<ToRelativePathBuf>);
+
+#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
+pub enum EditMappingOperations {
+ Move,
+ Erase,
+}
+
+#[derive(Serialize, Deserialize, Default)]
+pub enum EditMappingActionResult {
+ Success,
+
+ // Fail
+ AuthorizeFailed(String),
+ MappingNotFound(FromRelativePathBuf),
+ InvalidMove(InvalidMoveReason),
+
+ #[default]
+ Unknown,
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum InvalidMoveReason {
+ MoveOperationButNoTarget(FromRelativePathBuf),
+ ContainsDuplicateMapping(ToRelativePathBuf),
+}
+
+#[derive(Serialize, Deserialize, Clone)]
+pub struct EditMappingActionArguments {
+ pub operations: HashMap<FromRelativePathBuf, OperationArgument>,
+}
+
+/// This Action only modifies Sheet Mapping and
+/// does not interfere with the actual location of local files or Local Mapping
+#[action_gen]
+pub async fn edit_mapping_action(
+ ctx: ActionContext,
+ args: EditMappingActionArguments,
+) -> Result<EditMappingActionResult, TcpTargetError> {
+ let instance = check_connection_instance(&ctx)?;
+
+ // Auth Member
+ let member_id = match auth_member(&ctx, instance).await {
+ Ok(id) => id,
+ Err(e) => {
+ return Ok(EditMappingActionResult::AuthorizeFailed(e.to_string()));
+ }
+ };
+
+ // Check sheet
+ let sheet_name = get_current_sheet_name(&ctx, instance, &member_id).await?;
+
+ if ctx.is_proc_on_remote() {
+ let vault = try_get_vault(&ctx)?;
+ let mut sheet = vault.sheet(&sheet_name).await?;
+
+ // Precheck
+ for (from_path, (operation, to_path)) in args.operations.iter() {
+ // Check mapping exists
+ if !sheet.mapping().contains_key(from_path) {
+ write_and_return!(
+ instance,
+ EditMappingActionResult::MappingNotFound(from_path.clone())
+ );
+ }
+
+ // Move check
+ if operation == &EditMappingOperations::Move {
+ // Check if target exists
+ if let Some(to_path) = to_path {
+ // Check if target is duplicate
+ if sheet.mapping().contains_key(to_path) {
+ write_and_return!(
+ instance,
+ EditMappingActionResult::InvalidMove(
+ InvalidMoveReason::ContainsDuplicateMapping(to_path.clone())
+ )
+ );
+ }
+ } else {
+ write_and_return!(
+ instance,
+ EditMappingActionResult::InvalidMove(
+ InvalidMoveReason::MoveOperationButNoTarget(from_path.clone())
+ )
+ );
+ }
+ }
+ }
+
+ // Process
+ for (from_path, (operation, to_path)) in args.operations {
+ match operation {
+ // During the Precheck phase, it has been ensured that:
+ // 1. The mapping to be edited for the From path indeed exists
+ // 2. The location of the To path is indeed empty
+ // 3. In Move mode, To path can be safely unwrapped
+ // Therefore, the following unwrap() calls are safe to execute
+ EditMappingOperations::Move => {
+ let mapping = sheet.mapping_mut().remove(&from_path).unwrap();
+ let to_path = to_path.unwrap();
+ sheet
+ .add_mapping(to_path, mapping.id, mapping.version)
+ .await?;
+ }
+ EditMappingOperations::Erase => {
+ sheet.mapping_mut().remove(&from_path).unwrap();
+ }
+ }
+ }
+
+ // Write
+ sheet.persist().await?;
+
+ write_and_return!(instance, EditMappingActionResult::Success);
+ }
+
+ if ctx.is_proc_on_local() {
+ let result = instance
+ .lock()
+ .await
+ .read::<EditMappingActionResult>()
+ .await?;
+ if matches!(result, EditMappingActionResult::Success) {
+ sign_vault_modified(true).await;
+ }
+ return Ok(result);
+ }
+
+ Ok(EditMappingActionResult::Success)
+}
diff --git a/crates/vcs_actions/src/registry/client_registry.rs b/crates/vcs_actions/src/registry/client_registry.rs
index 6e2814f..6667a74 100644
--- a/crates/vcs_actions/src/registry/client_registry.rs
+++ b/crates/vcs_actions/src/registry/client_registry.rs
@@ -13,7 +13,9 @@ use crate::{
local_actions::{
register_set_upstream_vault_action, register_update_to_latest_info_action,
},
- sheet_actions::{register_drop_sheet_action, register_make_sheet_action},
+ sheet_actions::{
+ register_drop_sheet_action, register_edit_mapping_action, register_make_sheet_action,
+ },
track_action::register_track_file_action,
user_actions::register_change_virtual_file_edit_right_action,
},
@@ -30,6 +32,7 @@ fn register_actions(pool: &mut ActionPool) {
// Sheet Actions
register_make_sheet_action(pool);
register_drop_sheet_action(pool);
+ register_edit_mapping_action(pool);
// Track Action
register_track_file_action(pool);
diff --git a/crates/vcs_actions/src/registry/server_registry.rs b/crates/vcs_actions/src/registry/server_registry.rs
index 9c12d24..404625c 100644
--- a/crates/vcs_actions/src/registry/server_registry.rs
+++ b/crates/vcs_actions/src/registry/server_registry.rs
@@ -2,7 +2,9 @@ use action_system::action_pool::ActionPool;
use crate::actions::{
local_actions::{register_set_upstream_vault_action, register_update_to_latest_info_action},
- sheet_actions::{register_drop_sheet_action, register_make_sheet_action},
+ sheet_actions::{
+ register_drop_sheet_action, register_edit_mapping_action, register_make_sheet_action,
+ },
track_action::register_track_file_action,
user_actions::register_change_virtual_file_edit_right_action,
};
@@ -17,6 +19,7 @@ pub fn server_action_pool() -> ActionPool {
// Sheet Actions
register_make_sheet_action(&mut pool);
register_drop_sheet_action(&mut pool);
+ register_edit_mapping_action(&mut pool);
// Track Action
register_track_file_action(&mut pool);