diff options
| author | Glenn Smith <glenn@vector35.com> | 2023-05-30 19:53:03 -0400 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2023-06-08 17:18:30 -0400 |
| commit | 1509435163893916647427200437885127141494 (patch) | |
| tree | 769f54599dcaa07f8088f7b4a191d6c8f7196ffe /rust/src | |
| parent | 0882343191078862361937493035030128230878 (diff) | |
Undo transactions / context manager
Diffstat (limited to 'rust/src')
| -rw-r--r-- | rust/src/filemetadata.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/rust/src/filemetadata.rs b/rust/src/filemetadata.rs index b0e1c73f..1a558f3d 100644 --- a/rust/src/filemetadata.rs +++ b/rust/src/filemetadata.rs @@ -125,8 +125,26 @@ impl FileMetadata { unsafe { BNIsBackedByDatabase(self.handle, view_type.as_ref().as_ptr() as *const _) } } - pub fn begin_undo_actions(&self) -> BnString { - unsafe { BnString::from_raw(BNBeginUndoActions(self.handle)) } + pub fn run_undoable_transaction<F: FnOnce() -> Result<T, E>, T, E>( + &self, + func: F, + ) -> Result<T, E> { + let undo = self.begin_undo_actions(false); + let result = func(); + match result { + Ok(t) => { + self.commit_undo_actions(undo); + Ok(t) + } + Err(e) => { + self.revert_undo_actions(undo); + Err(e) + } + } + } + + pub fn begin_undo_actions(&self, anonymous_allowed: bool) -> BnString { + unsafe { BnString::from_raw(BNBeginUndoActions(self.handle, anonymous_allowed)) } } pub fn commit_undo_actions<S: BnStrCompatible>(&self, id: S) { |
