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;
|
mod insert;
|
||||||
pub mod package;
|
pub mod package;
|
||||||
|
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
@ -17,59 +19,30 @@ pub enum ImportProgress {
|
||||||
|
|
||||||
/// Wrapper around a progress function, usually passed by the [crate::backend::Backend],
|
/// Wrapper around a progress function, usually passed by the [crate::backend::Backend],
|
||||||
/// to make repeated calls more ergonomic.
|
/// to make repeated calls more ergonomic.
|
||||||
pub(crate) struct IncrementableProgress<P> {
|
pub(crate) struct IncrementableProgress<P>(Box<dyn FnMut(P, bool) -> bool>);
|
||||||
progress_fn: Box<dyn FnMut(P, bool) -> bool>,
|
|
||||||
count_map: Option<Box<dyn FnMut(usize) -> P>>,
|
|
||||||
count: usize,
|
|
||||||
update_interval: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<P> IncrementableProgress<P> {
|
impl<P> IncrementableProgress<P> {
|
||||||
/// `progress_fn: (progress, throttle) -> should_continue`
|
/// `progress_fn: (progress, throttle) -> should_continue`
|
||||||
pub(crate) fn new(progress_fn: impl 'static + FnMut(P, bool) -> bool) -> Self {
|
pub(crate) fn new(progress_fn: impl 'static + FnMut(P, bool) -> bool) -> Self {
|
||||||
Self {
|
Self(Box::new(progress_fn))
|
||||||
progress_fn: Box::new(progress_fn),
|
|
||||||
count_map: None,
|
|
||||||
count: 0,
|
|
||||||
update_interval: 17,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resets the count, and defines how it should be mapped to a progress value
|
/// Returns an [Incrementor] with an `increment()` function for use in loops.
|
||||||
/// in the future.
|
pub(crate) fn incrementor<'inc, 'progress: 'inc, 'map: 'inc>(
|
||||||
pub(crate) fn set_count_map(&mut self, count_map: impl 'static + FnMut(usize) -> P) {
|
&'progress mut self,
|
||||||
self.count_map = Some(Box::new(count_map));
|
mut count_map: impl 'map + FnMut(usize) -> P,
|
||||||
self.count = 0;
|
) -> 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.
|
/// Manually triggers 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.
|
|
||||||
/// Returns [AnkiError::Interrupted] if the operation should be cancelled.
|
/// Returns [AnkiError::Interrupted] if the operation should be cancelled.
|
||||||
pub(crate) fn call(&mut self, progress: P) -> Result<()> {
|
pub(crate) fn call(&mut self, progress: P) -> Result<()> {
|
||||||
self.update(progress, false)
|
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<()> {
|
fn update(&mut self, progress: P, throttle: bool) -> Result<()> {
|
||||||
if (self.progress_fn)(progress, throttle) {
|
if (self.0)(progress, throttle) {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(AnkiError::Interrupted)
|
Err(AnkiError::Interrupted)
|
||||||
|
@ -81,18 +54,34 @@ impl<P> IncrementableProgress<P> {
|
||||||
&mut self,
|
&mut self,
|
||||||
count_map: impl 'static + Fn(usize) -> P,
|
count_map: impl 'static + Fn(usize) -> P,
|
||||||
) -> Result<impl FnMut(usize) -> bool + '_> {
|
) -> 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> {
|
pub(crate) struct Incrementor<'f, F: 'f + FnMut(usize) -> Result<()>> {
|
||||||
/// Allows incrementing without a map, if the progress is of type [usize].
|
update_fn: F,
|
||||||
pub(crate) fn increment_flat(&mut self) -> Result<()> {
|
count: usize,
|
||||||
self.count += 1;
|
update_interval: usize,
|
||||||
if self.count % 17 == 0 {
|
_phantom: PhantomData<&'f ()>,
|
||||||
self.update(self.count, true)
|
}
|
||||||
} else {
|
|
||||||
Ok(())
|
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<()> {
|
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() {
|
for entry in media_map.used_entries() {
|
||||||
self.progress.increment()?;
|
incrementor.increment()?;
|
||||||
entry.copy_from_archive(&mut self.archive, &self.target_col.media_folder)?;
|
entry.copy_from_archive(&mut self.archive, &self.target_col.media_folder)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -71,10 +71,10 @@ fn prepare_media(
|
||||||
progress: &mut IncrementableProgress<ImportProgress>,
|
progress: &mut IncrementableProgress<ImportProgress>,
|
||||||
) -> Result<MediaUseMap> {
|
) -> Result<MediaUseMap> {
|
||||||
let mut media_map = MediaUseMap::default();
|
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)? {
|
for mut entry in extract_media_entries(meta, archive)? {
|
||||||
progress.increment()?;
|
incrementor.increment()?;
|
||||||
|
|
||||||
if entry.is_static() {
|
if entry.is_static() {
|
||||||
if !existing_sha1s.contains_key(&entry.name) {
|
if !existing_sha1s.contains_key(&entry.name) {
|
||||||
|
|
|
@ -189,10 +189,10 @@ impl<'n> NoteContext<'n> {
|
||||||
notes: Vec<Note>,
|
notes: Vec<Note>,
|
||||||
progress: &mut IncrementableProgress<ImportProgress>,
|
progress: &mut IncrementableProgress<ImportProgress>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
progress.set_count_map(ImportProgress::Notes);
|
let mut incrementor = progress.incrementor(ImportProgress::Notes);
|
||||||
|
|
||||||
for mut note in notes {
|
for mut note in notes {
|
||||||
progress.increment()?;
|
incrementor.increment()?;
|
||||||
if let Some(notetype_id) = self.remapped_notetypes.get(¬e.notetype_id) {
|
if let Some(notetype_id) = self.remapped_notetypes.get(¬e.notetype_id) {
|
||||||
if self.target_guids.contains_key(¬e.guid) {
|
if self.target_guids.contains_key(¬e.guid) {
|
||||||
self.imports.log_conflicting(note);
|
self.imports.log_conflicting(note);
|
||||||
|
|
|
@ -281,8 +281,9 @@ fn write_media_files(
|
||||||
progress: &mut IncrementableProgress<usize>,
|
progress: &mut IncrementableProgress<usize>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut copier = MediaCopier::new(meta);
|
let mut copier = MediaCopier::new(meta);
|
||||||
|
let mut incrementor = progress.incrementor(|u| u);
|
||||||
for (index, res) in media.0.enumerate() {
|
for (index, res) in media.0.enumerate() {
|
||||||
progress.increment_flat()?;
|
incrementor.increment()?;
|
||||||
let path = res?;
|
let path = res?;
|
||||||
|
|
||||||
zip.start_file(index.to_string(), file_options_stored())?;
|
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 media_manager = MediaManager::new(media_folder, media_db)?;
|
||||||
let mut media_comparer = MediaComparer::new(meta, progress, &media_manager, log)?;
|
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 {
|
for mut entry in media_entries {
|
||||||
progress.increment()?;
|
incrementor.increment()?;
|
||||||
maybe_restore_media_file(meta, media_folder, archive, &mut entry, &mut media_comparer)?;
|
maybe_restore_media_file(meta, media_folder, archive, &mut entry, &mut media_comparer)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue