From e4d36e71f38d748121c2bd0f3ff2eec84aff90be Mon Sep 17 00:00:00 2001 From: RumovZ Date: Mon, 25 Apr 2022 08:28:26 +0200 Subject: [PATCH] Make apkg exporting on backend abortable --- rslib/src/backend/import_export.rs | 8 ++++++-- rslib/src/import_export/package/apkg/export.rs | 1 + rslib/src/import_export/package/apkg/tests.rs | 2 +- rslib/src/import_export/package/colpkg/export.rs | 14 +++++++------- rslib/src/import_export/package/colpkg/tests.rs | 4 ++-- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/rslib/src/backend/import_export.rs b/rslib/src/backend/import_export.rs index 6814e34ce..f9076e9f2 100644 --- a/rslib/src/backend/import_export.rs +++ b/rslib/src/backend/import_export.rs @@ -97,10 +97,14 @@ impl Backend { } } - fn export_progress_fn(&self) -> impl FnMut(usize) { + fn export_progress_fn(&self) -> impl FnMut(usize) -> Result<()> { let mut handler = self.new_progress_handler(); move |media_files| { - handler.update(Progress::Export(media_files), true); + if handler.update(Progress::Export(media_files), true) { + Ok(()) + } else { + Err(AnkiError::Interrupted) + } } } } diff --git a/rslib/src/import_export/package/apkg/export.rs b/rslib/src/import_export/package/apkg/export.rs index 079837b0b..43c5c5e32 100644 --- a/rslib/src/import_export/package/apkg/export.rs +++ b/rslib/src/import_export/package/apkg/export.rs @@ -30,6 +30,7 @@ impl Collection { with_scheduling: bool, with_media: bool, media_fn: Option) -> MediaIter>>, + progress_fn: impl FnMut(usize) -> Result<()>, progress_fn: impl FnMut(usize), ) -> Result<()> { let temp_apkg = tempfile_in_parent_of(out_path.as_ref())?; diff --git a/rslib/src/import_export/package/apkg/tests.rs b/rslib/src/import_export/package/apkg/tests.rs index 596e1c056..7997661a1 100644 --- a/rslib/src/import_export/package/apkg/tests.rs +++ b/rslib/src/import_export/package/apkg/tests.rs @@ -36,7 +36,7 @@ fn roundtrip() { true, true, None, - |_| (), + |_| Ok(()), ) .unwrap(); target_col.import_apkg(&apkg_path, &mut |_| Ok(())).unwrap(); diff --git a/rslib/src/import_export/package/colpkg/export.rs b/rslib/src/import_export/package/colpkg/export.rs index 45cd7a251..be1ba3048 100644 --- a/rslib/src/import_export/package/colpkg/export.rs +++ b/rslib/src/import_export/package/colpkg/export.rs @@ -39,7 +39,7 @@ impl Collection { out_path: impl AsRef, include_media: bool, legacy: bool, - progress_fn: impl FnMut(usize), + progress_fn: impl FnMut(usize) -> Result<()>, ) -> Result<()> { let colpkg_name = out_path.as_ref(); let temp_colpkg = tempfile_in_parent_of(colpkg_name)?; @@ -103,7 +103,7 @@ fn export_collection_file( media_dir: Option, legacy: bool, tr: &I18n, - progress_fn: impl FnMut(usize), + progress_fn: impl FnMut(usize) -> Result<()>, ) -> Result<()> { let meta = if legacy { Meta::new_legacy() @@ -143,7 +143,7 @@ pub(crate) fn export_colpkg_from_data( col_size, MediaIter::empty(), tr, - |_| (), + |_| Ok(()), ) } @@ -154,7 +154,7 @@ pub(crate) fn export_collection( col_size: usize, media: MediaIter, tr: &I18n, - progress_fn: impl FnMut(usize), + progress_fn: impl FnMut(usize) -> Result<()>, ) -> Result<()> { let out_file = File::create(&out_path)?; let mut zip = ZipWriter::new(out_file); @@ -240,7 +240,7 @@ fn write_media( meta: &Meta, zip: &mut ZipWriter, media: MediaIter, - progress_fn: impl FnMut(usize), + progress_fn: impl FnMut(usize) -> Result<()>, ) -> Result<()> { let mut media_entries = vec![]; write_media_files(meta, zip, media, &mut media_entries, progress_fn)?; @@ -284,12 +284,12 @@ fn write_media_files( zip: &mut ZipWriter, media: MediaIter, media_entries: &mut Vec, - mut progress_fn: impl FnMut(usize), + mut progress_fn: impl FnMut(usize) -> Result<()>, ) -> Result<()> { let mut copier = MediaCopier::new(meta); for (index, res) in media.0.enumerate() { let path = res?; - progress_fn(index); + progress_fn(index)?; zip.start_file(index.to_string(), file_options_stored())?; diff --git a/rslib/src/import_export/package/colpkg/tests.rs b/rslib/src/import_export/package/colpkg/tests.rs index 08c84012f..c901677d8 100644 --- a/rslib/src/import_export/package/colpkg/tests.rs +++ b/rslib/src/import_export/package/colpkg/tests.rs @@ -41,7 +41,7 @@ fn roundtrip() -> Result<()> { // export to a file let col = collection_with_media(dir, name)?; let colpkg_name = dir.join(format!("{name}.colpkg")); - col.export_colpkg(&colpkg_name, true, legacy, |_| ())?; + col.export_colpkg(&colpkg_name, true, legacy, |_| Ok(()))?; // import into a new collection let anki2_name = dir .join(format!("{name}.anki2")) @@ -82,7 +82,7 @@ fn normalization_check_on_export() -> Result<()> { // manually write a file in the wrong encoding. std::fs::write(col.media_folder.join("ぱぱ.jpg"), "nfd encoding")?; assert_eq!( - col.export_colpkg(&colpkg_name, true, false, |_| ()) + col.export_colpkg(&colpkg_name, true, false, |_| Ok(())) .unwrap_err(), AnkiError::MediaCheckRequired );