mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
Outsource incrementing into Incrementor
This commit is contained in:
parent
58235d3709
commit
3864857b07
5 changed files with 47 additions and 57 deletions
|
@ -5,6 +5,8 @@ mod gather;
|
|||
mod insert;
|
||||
pub mod package;
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
|
@ -17,59 +19,30 @@ pub enum ImportProgress {
|
|||
|
||||
/// Wrapper around a progress function, usually passed by the [crate::backend::Backend],
|
||||
/// to make repeated calls more ergonomic.
|
||||
pub(crate) struct IncrementableProgress<P> {
|
||||
progress_fn: Box<dyn FnMut(P, bool) -> bool>,
|
||||
count_map: Option<Box<dyn FnMut(usize) -> P>>,
|
||||
count: usize,
|
||||
update_interval: usize,
|
||||
}
|
||||
pub(crate) struct IncrementableProgress<P>(Box<dyn FnMut(P, bool) -> bool>);
|
||||
|
||||
impl<P> IncrementableProgress<P> {
|
||||
/// `progress_fn: (progress, throttle) -> should_continue`
|
||||
pub(crate) fn new(progress_fn: impl 'static + FnMut(P, bool) -> bool) -> Self {
|
||||
Self {
|
||||
progress_fn: Box::new(progress_fn),
|
||||
count_map: None,
|
||||
count: 0,
|
||||
update_interval: 17,
|
||||
}
|
||||
Self(Box::new(progress_fn))
|
||||
}
|
||||
|
||||
/// Resets the count, and defines how it should be mapped to a progress value
|
||||
/// in the future.
|
||||
pub(crate) fn set_count_map(&mut self, count_map: impl 'static + FnMut(usize) -> P) {
|
||||
self.count_map = Some(Box::new(count_map));
|
||||
self.count = 0;
|
||||
/// Returns an [Incrementor] with an `increment()` function for use in loops.
|
||||
pub(crate) fn incrementor<'inc, 'progress: 'inc, 'map: 'inc>(
|
||||
&'progress mut self,
|
||||
mut count_map: impl 'map + FnMut(usize) -> P,
|
||||
) -> Incrementor<'inc, impl FnMut(usize) -> Result<()> + 'inc> {
|
||||
Incrementor::new(move |u| self.update(count_map(u), true))
|
||||
}
|
||||
|
||||
/// Increment the progress counter, periodically triggering an update.
|
||||
/// Returns [AnkiError::Interrupted] if the operation should be cancelled.
|
||||
/// Must have called `set_count_map()` before calling this.
|
||||
pub(crate) fn increment(&mut self) -> Result<()> {
|
||||
self.count += 1;
|
||||
if self.count % self.update_interval != 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let progress = self.mapped_progress()?;
|
||||
self.update(progress, true)
|
||||
}
|
||||
|
||||
/// Manually trigger an update.
|
||||
/// Manually triggers an update.
|
||||
/// Returns [AnkiError::Interrupted] if the operation should be cancelled.
|
||||
pub(crate) fn call(&mut self, progress: P) -> Result<()> {
|
||||
self.update(progress, false)
|
||||
}
|
||||
|
||||
fn mapped_progress(&mut self) -> Result<P> {
|
||||
if let Some(count_map) = self.count_map.as_mut() {
|
||||
Ok(count_map(self.count))
|
||||
} else {
|
||||
Err(AnkiError::invalid_input("count_map not set"))
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, progress: P, throttle: bool) -> Result<()> {
|
||||
if (self.progress_fn)(progress, throttle) {
|
||||
if (self.0)(progress, throttle) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(AnkiError::Interrupted)
|
||||
|
@ -81,18 +54,34 @@ impl<P> IncrementableProgress<P> {
|
|||
&mut self,
|
||||
count_map: impl 'static + Fn(usize) -> P,
|
||||
) -> Result<impl FnMut(usize) -> bool + '_> {
|
||||
Ok(move |count| (self.progress_fn)(count_map(count), true))
|
||||
Ok(move |count| (self.0)(count_map(count), true))
|
||||
}
|
||||
}
|
||||
|
||||
impl IncrementableProgress<usize> {
|
||||
/// Allows incrementing without a map, if the progress is of type [usize].
|
||||
pub(crate) fn increment_flat(&mut self) -> Result<()> {
|
||||
self.count += 1;
|
||||
if self.count % 17 == 0 {
|
||||
self.update(self.count, true)
|
||||
} else {
|
||||
Ok(())
|
||||
pub(crate) struct Incrementor<'f, F: 'f + FnMut(usize) -> Result<()>> {
|
||||
update_fn: F,
|
||||
count: usize,
|
||||
update_interval: usize,
|
||||
_phantom: PhantomData<&'f ()>,
|
||||
}
|
||||
|
||||
impl<'f, F: 'f + FnMut(usize) -> Result<()>> Incrementor<'f, F> {
|
||||
fn new(update_fn: F) -> Self {
|
||||
Self {
|
||||
update_fn,
|
||||
count: 0,
|
||||
update_interval: 17,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Increments the progress counter, periodically triggering an update.
|
||||
/// Returns [AnkiError::Interrupted] if the operation should be cancelled.
|
||||
pub(crate) fn increment(&mut self) -> Result<()> {
|
||||
self.count += 1;
|
||||
if self.count % self.update_interval != 0 {
|
||||
return Ok(());
|
||||
}
|
||||
(self.update_fn)(self.count)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,9 +45,9 @@ impl Context<'_> {
|
|||
}
|
||||
|
||||
pub(super) fn copy_media(&mut self, media_map: &mut MediaUseMap) -> Result<()> {
|
||||
self.progress.set_count_map(ImportProgress::Media);
|
||||
let mut incrementor = self.progress.incrementor(ImportProgress::Media);
|
||||
for entry in media_map.used_entries() {
|
||||
self.progress.increment()?;
|
||||
incrementor.increment()?;
|
||||
entry.copy_from_archive(&mut self.archive, &self.target_col.media_folder)?;
|
||||
}
|
||||
Ok(())
|
||||
|
@ -71,10 +71,10 @@ fn prepare_media(
|
|||
progress: &mut IncrementableProgress<ImportProgress>,
|
||||
) -> Result<MediaUseMap> {
|
||||
let mut media_map = MediaUseMap::default();
|
||||
progress.set_count_map(ImportProgress::MediaCheck);
|
||||
let mut incrementor = progress.incrementor(ImportProgress::MediaCheck);
|
||||
|
||||
for mut entry in extract_media_entries(meta, archive)? {
|
||||
progress.increment()?;
|
||||
incrementor.increment()?;
|
||||
|
||||
if entry.is_static() {
|
||||
if !existing_sha1s.contains_key(&entry.name) {
|
||||
|
|
|
@ -189,10 +189,10 @@ impl<'n> NoteContext<'n> {
|
|||
notes: Vec<Note>,
|
||||
progress: &mut IncrementableProgress<ImportProgress>,
|
||||
) -> Result<()> {
|
||||
progress.set_count_map(ImportProgress::Notes);
|
||||
let mut incrementor = progress.incrementor(ImportProgress::Notes);
|
||||
|
||||
for mut note in notes {
|
||||
progress.increment()?;
|
||||
incrementor.increment()?;
|
||||
if let Some(notetype_id) = self.remapped_notetypes.get(¬e.notetype_id) {
|
||||
if self.target_guids.contains_key(¬e.guid) {
|
||||
self.imports.log_conflicting(note);
|
||||
|
|
|
@ -281,8 +281,9 @@ fn write_media_files(
|
|||
progress: &mut IncrementableProgress<usize>,
|
||||
) -> Result<()> {
|
||||
let mut copier = MediaCopier::new(meta);
|
||||
let mut incrementor = progress.incrementor(|u| u);
|
||||
for (index, res) in media.0.enumerate() {
|
||||
progress.increment_flat()?;
|
||||
incrementor.increment()?;
|
||||
let path = res?;
|
||||
|
||||
zip.start_file(index.to_string(), file_options_stored())?;
|
||||
|
|
|
@ -92,9 +92,9 @@ fn restore_media(
|
|||
let media_manager = MediaManager::new(media_folder, media_db)?;
|
||||
let mut media_comparer = MediaComparer::new(meta, progress, &media_manager, log)?;
|
||||
|
||||
progress.set_count_map(ImportProgress::Media);
|
||||
let mut incrementor = progress.incrementor(ImportProgress::Media);
|
||||
for mut entry in media_entries {
|
||||
progress.increment()?;
|
||||
incrementor.increment()?;
|
||||
maybe_restore_media_file(meta, media_folder, archive, &mut entry, &mut media_comparer)?;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue