Repository navigation
Expand file tree
/
Copy pathretry_command.rs
More file actions
268 lines (246 loc) · 8.63 KB
/
Copy pathretry_command.rs
File metadata and controls
268 lines (246 loc) · 8.63 KB
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
//! A synchronous host-owned command boundary: retain the draft until the command succeeds.
//! This small model demonstrates recovery; it is not a PhotoCraft/PdfCraft UI regression.
use edit_session::{EditSession, FinishError};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Context {
document: u64,
target: u64,
revision: u64,
}
#[derive(Debug, PartialEq, Eq)]
enum ConfirmError {
Closed,
Draft(FinishError),
PermissionDenied,
RevisionExhausted,
}
#[derive(Debug, PartialEq, Eq)]
enum Outcome {
Applied,
Unchanged,
}
struct Host {
value: String,
context: Context,
allowed: bool,
undo: Vec<String>,
redo: Vec<String>,
}
impl Host {
fn new() -> Self {
Self {
value: "original".into(),
context: Context {
document: 7,
target: 2,
revision: 42,
},
allowed: true,
undo: Vec::new(),
redo: Vec::new(),
}
}
fn apply(&mut self, proposed: &str) -> Result<(), ConfirmError> {
// All fallible host checks precede domain/history writes. Real command engines
// must provide the same atomic rejection guarantee, or their own rollback.
if !self.allowed {
return Err(ConfirmError::PermissionDenied);
}
let next_revision = self
.context
.revision
.checked_add(1)
.ok_or(ConfirmError::RevisionExhausted)?;
self.undo.push(self.value.clone());
self.value = proposed.to_owned();
self.redo.clear();
self.context.revision = next_revision;
Ok(())
}
}
struct Dialog {
// Keep the raw field too: an invalid proposal retains only the last valid preview.
text: String,
draft: Option<EditSession<String, Context>>,
}
impl Dialog {
fn open(host: &Host) -> Self {
Self {
text: host.value.clone(),
draft: Some(EditSession::new(host.value.clone(), host.context)),
}
}
fn set_text(&mut self, text: &str) -> Result<(), &'static str> {
let draft = self.draft.as_mut().ok_or("dialog is closed")?;
self.text = text.to_owned();
draft.propose(if text.trim().is_empty() {
Err("enter a title")
} else {
Ok(self.text.clone())
})
}
fn confirm(&mut self, host: &mut Host) -> Result<Outcome, ConfirmError> {
// This whole method runs on the host's serialized edit path, without an await
// or a callback that can change the target between check and command dispatch.
let draft = self.draft.as_ref().ok_or(ConfirmError::Closed)?;
draft
.check(&host.value, &host.context)
.map_err(ConfirmError::Draft)?;
let outcome = if draft.preview() == draft.original() {
Outcome::Unchanged // Do not dispatch a command or clear Redo for a no-op.
} else {
// Borrow the proposal: finish() would consume it before this fallible call.
host.apply(draft.preview())?;
Outcome::Applied
};
self.cancel(); // Close only after a successful command or a checked no-op.
Ok(outcome)
}
fn cancel(&mut self) {
if let Some(draft) = self.draft.take() {
draft.cancel();
}
}
}
fn main() -> Result<(), ConfirmError> {
let mut host = Host::new();
let mut dialog = Dialog::open(&host);
assert_eq!(dialog.set_text("edited title"), Ok(()));
host.allowed = false;
assert_eq!(
dialog.confirm(&mut host),
Err(ConfirmError::PermissionDenied)
);
assert_eq!(dialog.text, "edited title");
assert!(dialog.draft.is_some());
host.allowed = true;
assert_eq!(dialog.confirm(&mut host)?, Outcome::Applied);
assert_eq!(host.value, "edited title");
assert_eq!(host.undo, ["original"]);
assert_eq!(dialog.confirm(&mut host), Err(ConfirmError::Closed));
println!("Denied command retained the draft; retry applied once and closed the dialog.");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn denied_edit() -> (Host, Dialog) {
let mut host = Host::new();
host.redo.push("future".into());
let mut dialog = Dialog::open(&host);
assert_eq!(dialog.set_text("edited"), Ok(()));
host.allowed = false;
assert_eq!(
dialog.confirm(&mut host),
Err(ConfirmError::PermissionDenied)
);
(host, dialog)
}
#[test]
fn rejection_preserves_fields_document_revision_and_history_then_retry_commits_once() {
let (mut host, mut dialog) = denied_edit();
assert_eq!(host.value, "original");
assert_eq!(host.context.revision, 42);
assert!(host.undo.is_empty());
assert_eq!(host.redo, ["future"]);
assert_eq!(dialog.text, "edited");
assert!(dialog.draft.is_some());
host.allowed = true;
assert_eq!(dialog.confirm(&mut host), Ok(Outcome::Applied));
assert_eq!(host.value, "edited");
assert_eq!(host.context.revision, 43);
assert_eq!(host.undo, ["original"]);
assert!(host.redo.is_empty());
assert!(dialog.draft.is_none());
}
#[test]
fn repeated_confirm_cannot_duplicate_the_command() {
let (mut host, mut dialog) = denied_edit();
host.allowed = true;
assert_eq!(dialog.confirm(&mut host), Ok(Outcome::Applied));
assert_eq!(dialog.confirm(&mut host), Err(ConfirmError::Closed));
assert_eq!(host.undo, ["original"]);
assert_eq!(host.context.revision, 43);
}
#[test]
fn invalid_correction_keeps_raw_input_and_does_not_apply_last_valid_preview() {
let (mut host, mut dialog) = denied_edit();
host.allowed = true;
assert!(dialog.set_text(" ").is_err());
assert_eq!(
dialog.confirm(&mut host),
Err(ConfirmError::Draft(FinishError::InvalidProposal))
);
assert_eq!(dialog.text, " ");
assert_eq!(host.value, "original");
assert!(host.undo.is_empty());
assert_eq!(dialog.set_text("corrected"), Ok(()));
assert_eq!(dialog.confirm(&mut host), Ok(Outcome::Applied));
assert_eq!(host.value, "corrected");
}
#[test]
fn retry_never_retargets_a_different_document_or_target() {
for change_document in [true, false] {
let (mut host, mut dialog) = denied_edit();
host.allowed = true;
if change_document {
host.context.document += 1;
} else {
host.context.target += 1;
}
assert_eq!(
dialog.confirm(&mut host),
Err(ConfirmError::Draft(FinishError::ContextChanged))
);
assert_eq!(host.value, "original");
assert!(host.undo.is_empty());
assert_eq!(host.redo, ["future"]);
assert_eq!(dialog.text, "edited");
}
}
#[test]
fn edit_then_undo_before_retry_is_stale_even_at_the_original_value() {
let (mut host, mut dialog) = denied_edit();
host.allowed = true;
host.context.revision += 2; // A host edit and Undo, back to "original".
assert_eq!(
dialog.confirm(&mut host),
Err(ConfirmError::Draft(FinishError::ContextChanged))
);
assert_eq!(host.value, "original");
assert!(host.undo.is_empty());
assert_eq!(host.redo, ["future"]);
}
#[test]
fn unversioned_host_value_change_still_blocks_retry() {
let (mut host, mut dialog) = denied_edit();
host.allowed = true;
host.value = "newer value".into();
assert_eq!(
dialog.confirm(&mut host),
Err(ConfirmError::Draft(FinishError::ValueChanged))
);
assert_eq!(host.value, "newer value");
assert!(host.undo.is_empty());
}
#[test]
fn noop_skips_the_denied_command_and_preserves_redo() {
let (mut host, mut dialog) = denied_edit();
assert_eq!(dialog.set_text("original"), Ok(()));
assert_eq!(dialog.confirm(&mut host), Ok(Outcome::Unchanged));
assert!(dialog.draft.is_none());
assert_eq!(host.context.revision, 42);
assert!(host.undo.is_empty());
assert_eq!(host.redo, ["future"]);
}
#[test]
fn cancel_after_rejection_discards_only_the_local_draft() {
let (mut host, mut dialog) = denied_edit();
dialog.cancel();
assert_eq!(dialog.confirm(&mut host), Err(ConfirmError::Closed));
assert_eq!(host.value, "original");
assert_eq!(host.context.revision, 42);
assert!(host.undo.is_empty());
assert_eq!(host.redo, ["future"]);
}
}