pub struct Transaction { /* private fields */ }Expand description
Builder for creating a group of commands as a single transaction.
Commands added to a transaction are executed immediately. If any command fails, all previously executed commands are rolled back.
When committed, the transaction becomes a single entry in history that can be undone/redone atomically.
Implementations§
Source§impl Transaction
impl Transaction
Sourcepub fn begin(description: impl Into<String>) -> Self
pub fn begin(description: impl Into<String>) -> Self
Begin a new transaction with the given description.
Sourcepub fn execute(&mut self, cmd: Box<dyn UndoableCmd>) -> CommandResult
pub fn execute(&mut self, cmd: Box<dyn UndoableCmd>) -> CommandResult
Execute a command and add it to the transaction.
The command is executed immediately. If it fails, all previously executed commands are rolled back and the error is returned.
§Errors
Returns error if the command fails to execute.
Sourcepub fn add_executed(&mut self, cmd: Box<dyn UndoableCmd>) -> CommandResult
pub fn add_executed(&mut self, cmd: Box<dyn UndoableCmd>) -> CommandResult
Add a pre-executed command to the transaction.
Use this when the command has already been executed externally. The command will be undone on rollback and redone on redo.
§Errors
Returns error if the transaction is already finalized.
Sourcepub fn commit(self) -> Option<Box<dyn UndoableCmd>>
pub fn commit(self) -> Option<Box<dyn UndoableCmd>>
Commit the transaction, returning it as a single undoable command.
Returns None if the transaction is empty.
Sourcepub fn rollback(&mut self)
pub fn rollback(&mut self)
Roll back all executed commands in the transaction.
This undoes all commands in reverse order. After rollback, the transaction is finalized and cannot be used further.
Sourcepub fn description(&self) -> &str
pub fn description(&self) -> &str
Get the transaction description.