pub struct EditSession<T, C> { /* private fields */ }Expand description
A single-use edit. It never writes to a document or records history itself.
This type intentionally does not implement Clone. finish consumes it, so the same
session cannot accidentally emit two commits. A failed finish also consumes the draft;
call check first if invalid input should remain editable in the UI.
Implementations§
Source§impl<T: Clone, C> EditSession<T, C>
impl<T: Clone, C> EditSession<T, C>
Sourcepub fn new(original: T, context: C) -> Self
pub fn new(original: T, context: C) -> Self
Begin from a value already validated by the application.
Cloning original must produce an independently editable snapshot. Shared mutable
pointers such as Rc<RefCell<_>> can let edits change the original or live document
and defeat cancellation/conflict checks. Use owned fields or immutable shared data.
The context must also remain a stable snapshot until the session ends.
Source§impl<T, C> EditSession<T, C>
impl<T, C> EditSession<T, C>
Sourcepub fn original(&self) -> &T
pub fn original(&self) -> &T
The validated value captured when editing began.
Examples found in repository?
92 fn confirm(&mut self, host: &mut Host) -> Result<Outcome, ConfirmError> {
93 // This whole method runs on the host's serialized edit path, without an await
94 // or a callback that can change the target between check and command dispatch.
95 let draft = self.draft.as_ref().ok_or(ConfirmError::Closed)?;
96 draft
97 .check(&host.value, &host.context)
98 .map_err(ConfirmError::Draft)?;
99 let outcome = if draft.preview() == draft.original() {
100 Outcome::Unchanged // Do not dispatch a command or clear Redo for a no-op.
101 } else {
102 // Borrow the proposal: finish() would consume it before this fallible call.
103 host.apply(draft.preview())?;
104 Outcome::Applied
105 };
106 self.cancel(); // Close only after a successful command or a checked no-op.
107 Ok(outcome)
108 }Sourcepub fn preview(&self) -> &T
pub fn preview(&self) -> &T
Last valid preview, even when a later incomplete/invalid input is present.
Examples found in repository?
92 fn confirm(&mut self, host: &mut Host) -> Result<Outcome, ConfirmError> {
93 // This whole method runs on the host's serialized edit path, without an await
94 // or a callback that can change the target between check and command dispatch.
95 let draft = self.draft.as_ref().ok_or(ConfirmError::Closed)?;
96 draft
97 .check(&host.value, &host.context)
98 .map_err(ConfirmError::Draft)?;
99 let outcome = if draft.preview() == draft.original() {
100 Outcome::Unchanged // Do not dispatch a command or clear Redo for a no-op.
101 } else {
102 // Borrow the proposal: finish() would consume it before this fallible call.
103 host.apply(draft.preview())?;
104 Outcome::Applied
105 };
106 self.cancel(); // Close only after a successful command or a checked no-op.
107 Ok(outcome)
108 }Sourcepub fn context(&self) -> &C
pub fn context(&self) -> &C
The host context captured when editing began; never changed by this session.
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Whether the latest proposal was accepted. This does not check live host state.
Sourcepub fn propose<E>(&mut self, proposal: Result<T, E>) -> Result<(), E>
pub fn propose<E>(&mut self, proposal: Result<T, E>) -> Result<(), E>
Accept a host-validated proposal. Errors invalidate commit without replacing the last valid preview. Units, mixed values, finite checks and bounds belong to the host.
Source§impl<T: PartialEq, C: PartialEq> EditSession<T, C>
impl<T: PartialEq, C: PartialEq> EditSession<T, C>
Sourcepub fn check_live(&self, live: &T, context: &C) -> Result<(), FinishError>
pub fn check_live(&self, live: &T, context: &C) -> Result<(), FinishError>
Check whether the draft still belongs to the current host state, independently of invalid input. Useful when deciding whether an open editor must be dismissed.
This never returns InvalidProposal and does not authorize a commit. Use check
or finish immediately before applying on the host’s serialized command path.
Context conflicts take precedence over value conflicts. No lock is acquired.
Sourcepub fn check(&self, live: &T, context: &C) -> Result<(), FinishError>
pub fn check(&self, live: &T, context: &C) -> Result<(), FinishError>
Check live host state and proposal validity immediately before applying on the host’s serialized command path. Live conflicts take precedence over invalid input. This comparison is not a lock and is not a distributed transaction.
Examples found in repository?
92 fn confirm(&mut self, host: &mut Host) -> Result<Outcome, ConfirmError> {
93 // This whole method runs on the host's serialized edit path, without an await
94 // or a callback that can change the target between check and command dispatch.
95 let draft = self.draft.as_ref().ok_or(ConfirmError::Closed)?;
96 draft
97 .check(&host.value, &host.context)
98 .map_err(ConfirmError::Draft)?;
99 let outcome = if draft.preview() == draft.original() {
100 Outcome::Unchanged // Do not dispatch a command or clear Redo for a no-op.
101 } else {
102 // Borrow the proposal: finish() would consume it before this fallible call.
103 host.apply(draft.preview())?;
104 Outcome::Applied
105 };
106 self.cancel(); // Close only after a successful command or a checked no-op.
107 Ok(outcome)
108 }