Anki/rslib/src/config/undo.rs
RumovZ c521753057
Refactor error handling (#2136)
* Add crate snafu

* Replace all inline structs in AnkiError

* Derive Snafu on AnkiError

* Use snafu for card type errors

* Use snafu whatever error for InvalidInput

* Use snafu for NotFoundError and improve message

* Use snafu for FileIoError to attach context

Remove IoError.
Add some context-attaching helpers to replace code returning bare
io::Errors.

* Add more context-attaching io helpers

* Add message, context and backtrace to new snafus

* Utilize error context and backtrace on frontend

* Rename LocalizedError -> BackendError.
* Remove DocumentedError.
* Have all backend exceptions inherit BackendError.

* Rename localized(_description) -> message

* Remove accidentally committed experimental trait

* invalid_input_context -> ok_or_invalid

* ensure_valid_input! -> require!

* Always return `Err` from `invalid_input!`

Instead of a Result to unwrap, the macro accepts a source error now.

* new_tempfile_in_parent -> new_tempfile_in_parent_of

* ok_or_not_found -> or_not_found

* ok_or_invalid -> or_invalid

* Add crate convert_case

* Use unqualified lowercase type name

* Remove uses of snafu::ensure

* Allow public construction of InvalidInputErrors (dae)

Needed to port the AnkiDroid changes.

* Make into_protobuf() public (dae)

Also required for AnkiDroid. Not sure why it worked previously - possible
bug in older Rust version?
2022-10-21 18:02:12 +10:00

117 lines
3.6 KiB
Rust

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use super::ConfigEntry;
use crate::prelude::*;
#[derive(Debug)]
pub(crate) enum UndoableConfigChange {
Added(Box<ConfigEntry>),
Updated(Box<ConfigEntry>),
Removed(Box<ConfigEntry>),
}
impl Collection {
pub(crate) fn undo_config_change(&mut self, change: UndoableConfigChange) -> Result<()> {
match change {
UndoableConfigChange::Added(entry) => self.remove_config_undoable(&entry.key),
UndoableConfigChange::Updated(entry) => {
let current = self
.storage
.get_config_entry(&entry.key)?
.or_invalid("config disappeared")?;
self.update_config_entry_undoable(entry, current)
.map(|_| ())
}
UndoableConfigChange::Removed(entry) => self.add_config_entry_undoable(entry),
}
}
/// True if added, or value changed.
pub(super) fn set_config_undoable(&mut self, entry: Box<ConfigEntry>) -> Result<bool> {
if let Some(original) = self.storage.get_config_entry(&entry.key)? {
self.update_config_entry_undoable(entry, original)
} else {
self.add_config_entry_undoable(entry)?;
Ok(true)
}
}
pub(super) fn remove_config_undoable(&mut self, key: &str) -> Result<()> {
if let Some(current) = self.storage.get_config_entry(key)? {
self.save_undo(UndoableConfigChange::Removed(current));
self.storage.remove_config(key)?;
}
Ok(())
}
fn add_config_entry_undoable(&mut self, entry: Box<ConfigEntry>) -> Result<()> {
self.storage.set_config_entry(&entry)?;
self.save_undo(UndoableConfigChange::Added(entry));
Ok(())
}
/// True if new value differed.
fn update_config_entry_undoable(
&mut self,
entry: Box<ConfigEntry>,
original: Box<ConfigEntry>,
) -> Result<bool> {
if entry.value != original.value {
self.save_undo(UndoableConfigChange::Updated(original));
self.storage.set_config_entry(&entry)?;
Ok(true)
} else {
Ok(false)
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::collection::open_test_collection;
#[test]
fn undo() -> Result<()> {
let mut col = open_test_collection();
// the op kind doesn't matter, we just need undo enabled
let op = Op::Bury;
// test key
let key = BoolKey::NormalizeNoteText;
// not set by default, but defaults to true
assert!(col.get_config_bool(key));
// first set adds the key
col.transact(op.clone(), |col| col.set_config_bool_inner(key, false))?;
assert!(!col.get_config_bool(key));
// mutate it twice
col.transact(op.clone(), |col| col.set_config_bool_inner(key, true))?;
assert!(col.get_config_bool(key));
col.transact(op.clone(), |col| col.set_config_bool_inner(key, false))?;
assert!(!col.get_config_bool(key));
// when we remove it, it goes back to its default
col.transact(op, |col| col.remove_config_inner(key))?;
assert!(col.get_config_bool(key));
// undo the removal
col.undo()?;
assert!(!col.get_config_bool(key));
// undo the mutations
col.undo()?;
assert!(col.get_config_bool(key));
col.undo()?;
assert!(!col.get_config_bool(key));
// and undo the initial add
col.undo()?;
assert!(col.get_config_bool(key));
Ok(())
}
}