mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 06:52:21 -04:00

* 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?
69 lines
2.3 KiB
Rust
69 lines
2.3 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use crate::prelude::*;
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) enum UndoableDeckConfigChange {
|
|
Added(Box<DeckConfig>),
|
|
Updated(Box<DeckConfig>),
|
|
Removed(Box<DeckConfig>),
|
|
}
|
|
|
|
impl Collection {
|
|
pub(crate) fn undo_deck_config_change(
|
|
&mut self,
|
|
change: UndoableDeckConfigChange,
|
|
) -> Result<()> {
|
|
match change {
|
|
UndoableDeckConfigChange::Added(config) => self.remove_deck_config_undoable(*config),
|
|
UndoableDeckConfigChange::Updated(config) => {
|
|
let current = self
|
|
.storage
|
|
.get_deck_config(config.id)?
|
|
.or_invalid("deck config disappeared")?;
|
|
self.update_deck_config_undoable(&config, current)
|
|
}
|
|
UndoableDeckConfigChange::Removed(config) => self.restore_deleted_deck_config(*config),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn remove_deck_config_undoable(&mut self, config: DeckConfig) -> Result<()> {
|
|
self.storage.remove_deck_conf(config.id)?;
|
|
self.save_undo(UndoableDeckConfigChange::Removed(Box::new(config)));
|
|
Ok(())
|
|
}
|
|
|
|
pub(super) fn add_deck_config_undoable(
|
|
&mut self,
|
|
config: &mut DeckConfig,
|
|
) -> Result<(), AnkiError> {
|
|
self.storage.add_deck_conf(config)?;
|
|
self.save_undo(UndoableDeckConfigChange::Added(Box::new(config.clone())));
|
|
Ok(())
|
|
}
|
|
|
|
pub(crate) fn add_deck_config_if_unique_undoable(&mut self, config: &DeckConfig) -> Result<()> {
|
|
if self.storage.add_deck_conf_if_unique(config)? {
|
|
self.save_undo(UndoableDeckConfigChange::Added(Box::new(config.clone())));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub(super) fn update_deck_config_undoable(
|
|
&mut self,
|
|
config: &DeckConfig,
|
|
original: DeckConfig,
|
|
) -> Result<()> {
|
|
self.save_undo(UndoableDeckConfigChange::Updated(Box::new(original)));
|
|
self.storage.update_deck_conf(config)
|
|
}
|
|
|
|
fn restore_deleted_deck_config(&mut self, config: DeckConfig) -> Result<()> {
|
|
self.storage
|
|
.add_or_update_deck_config_with_existing_id(&config)?;
|
|
self.save_undo(UndoableDeckConfigChange::Added(Box::new(config)));
|
|
Ok(())
|
|
}
|
|
}
|