// Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html mod gather; mod insert; pub mod package; pub mod text; use std::marker::PhantomData; pub use crate::pb::import_export::import_response::{Log as NoteLog, Note as LogNote}; use crate::{ prelude::*, text::{ newlines_to_spaces, strip_html_preserving_media_filenames, truncate_to_char_boundary, CowMapping, }, }; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ImportProgress { File, Extracting, Gathering, Media(usize), MediaCheck(usize), Notes(usize), } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ExportProgress { File, Gathering, Notes(usize), Cards(usize), Media(usize), } /// Wrapper around a progress function, usually passed by the [crate::backend::Backend], /// to make repeated calls more ergonomic. pub(crate) struct IncrementableProgress
(Box IncrementableProgress {
/// `progress_fn: (progress, throttle) -> should_continue`
pub(crate) fn new(progress_fn: impl 'static + FnMut(P, bool) -> bool) -> Self {
Self(Box::new(progress_fn))
}
/// 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))
}
/// 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 update(&mut self, progress: P, throttle: bool) -> Result<()> {
if (self.0)(progress, throttle) {
Ok(())
} else {
Err(AnkiError::Interrupted)
}
}
/// Stopgap for returning a progress fn compliant with the media code.
pub(crate) fn media_db_fn(
&mut self,
count_map: impl 'static + Fn(usize) -> P,
) -> Result