From 1509435163893916647427200437885127141494 Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Tue, 30 May 2023 19:53:03 -0400 Subject: Undo transactions / context manager --- rust/src/filemetadata.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'rust/src') 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 Result, T, E>( + &self, + func: F, + ) -> Result { + 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(&self, id: S) { -- cgit v1.3.1