1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
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::{
file_status::{FromRelativePathBuf, ToRelativePathBuf},
vault_modified::sign_vault_modified,
},
sheet::SheetName,
};
use crate::{
actions::{
auth_member, check_connection_instance, get_current_sheet_name, try_get_local_workspace,
try_get_vault,
},
write_and_return,
};
#[derive(Default, Serialize, Deserialize)]
pub enum MakeSheetActionResult {
Success,
SuccessRestore,
// Fail
AuthorizeFailed(String),
SheetAlreadyExists,
SheetCreationFailed(String),
#[default]
Unknown,
}
/// Build a sheet with context
#[action_gen]
pub async fn make_sheet_action(
ctx: ActionContext,
sheet_name: SheetName,
) -> Result<MakeSheetActionResult, 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(MakeSheetActionResult::AuthorizeFailed(e.to_string())),
};
if ctx.is_proc_on_remote() {
let vault = try_get_vault(&ctx)?;
// Check if the sheet already exists
if let Ok(mut sheet) = vault.sheet(&sheet_name).await {
// If the sheet has no holder, assign it to the current member (restore operation)
if sheet.holder().is_none() {
sheet.set_holder(member_id);
match sheet.persist().await {
Ok(_) => {
write_and_return!(instance, MakeSheetActionResult::SuccessRestore);
}
Err(e) => {
write_and_return!(
instance,
MakeSheetActionResult::SheetCreationFailed(e.to_string())
);
}
}
} else {
write_and_return!(instance, MakeSheetActionResult::SheetAlreadyExists);
}
} else {
// Create the sheet
match vault.create_sheet(&sheet_name, &member_id).await {
Ok(_) => {
write_and_return!(instance, MakeSheetActionResult::Success);
}
Err(e) => {
write_and_return!(
instance,
MakeSheetActionResult::SheetCreationFailed(e.to_string())
);
}
}
}
}
if ctx.is_proc_on_local() {
let result = instance
.lock()
.await
.read::<MakeSheetActionResult>()
.await?;
if matches!(result, MakeSheetActionResult::Success) {
sign_vault_modified(true).await;
}
return Ok(result);
}
Err(TcpTargetError::NoResult("No result.".to_string()))
}
#[derive(Default, Serialize, Deserialize)]
pub enum DropSheetActionResult {
Success,
// Fail
SheetInUse,
AuthorizeFailed(String),
SheetNotExists,
SheetDropFailed(String),
NoHolder,
NotOwner,
#[default]
Unknown,
}
#[action_gen]
pub async fn drop_sheet_action(
ctx: ActionContext,
sheet_name: SheetName,
) -> Result<DropSheetActionResult, 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(DropSheetActionResult::AuthorizeFailed(e.to_string()));
}
};
// Check sheet in use on local
if ctx.is_proc_on_local() {
let local_workspace = try_get_local_workspace(&ctx)?;
if let Some(sheet) = local_workspace.config().lock().await.sheet_in_use() {
if sheet == &sheet_name {
instance.lock().await.write(false).await?;
return Ok(DropSheetActionResult::SheetInUse);
}
instance.lock().await.write(true).await?;
} else {
instance.lock().await.write(true).await?;
}
}
if ctx.is_proc_on_remote() {
// Check if client sheet is in use
let sheet_in_use = instance.lock().await.read::<bool>().await?;
if !sheet_in_use {
return Ok(DropSheetActionResult::SheetInUse);
}
let vault = try_get_vault(&ctx)?;
// Check if the sheet exists
let mut sheet = match vault.sheet(&sheet_name).await {
Ok(sheet) => sheet,
Err(e) => {
if e.kind() == ErrorKind::NotFound {
write_and_return!(instance, DropSheetActionResult::SheetNotExists);
} else {
write_and_return!(
instance,
DropSheetActionResult::SheetDropFailed(e.to_string())
);
}
}
};
// Get the sheet's holder
let Some(holder) = sheet.holder() else {
write_and_return!(instance, DropSheetActionResult::NoHolder);
};
// Verify the sheet's holder
if holder != &member_id {
write_and_return!(instance, DropSheetActionResult::NotOwner);
}
// Drop the sheet
sheet.forget_holder();
match sheet.persist().await {
Ok(_) => {
write_and_return!(instance, DropSheetActionResult::Success);
}
Err(e) => {
write_and_return!(
instance,
DropSheetActionResult::SheetDropFailed(e.to_string())
);
}
}
}
if ctx.is_proc_on_local() {
let result = instance
.lock()
.await
.read::<DropSheetActionResult>()
.await?;
if matches!(result, DropSheetActionResult::Success) {
sign_vault_modified(true).await;
}
return Ok(result);
}
Err(TcpTargetError::NoResult("No result.".to_string()))
}
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)
}
|