M11: Optimize runs when a category it discards holds nothing

PDF Optimizer's Discard Objects ticks pass their categories to
remove_hidden, which refuses a run that removes nothing. Ticking "Discard
bookmarks" on a file without bookmarks, or doc_optimize with such a
discard, ended the whole optimization with "there are no redaction marks
to apply". An empty category is now nothing discarded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AmsdeUCdTaD72jkDw2pJKY
This commit is contained in:
Grzegorz Martens
2026-11-02 10:16:26 +01:00
co-authored by Claude Opus 5.5
parent d3384340a6
commit 278a588bf2
2 changed files with 20 additions and 1 deletions
+10 -1
View File
@@ -92,7 +92,16 @@ impl OptimizeJob {
pdfcraft_xfa::clear_password_values(&mut cos).map_err(|e| EditError::Write(e.to_string()))?;
let mut step = |stage| if progress(stage) { Ok(()) } else { Err(EditError::Cancelled) };
step(OptimizeStage::Discarding)?;
let discarded = if discard.is_empty() { Vec::new() } else { pdfcraft_redact::sanitize::remove_hidden(&mut cos, &discard)? };
let discarded = if discard.is_empty() {
Vec::new()
} else {
// `remove_hidden` refuses a run that removes nothing. Ticked categories that hold
// nothing are nothing to discard, not a reason to end the whole optimization.
match pdfcraft_redact::sanitize::remove_hidden(&mut cos, &discard) {
Err(pdfcraft_redact::RedactError::NothingToApply) => Vec::new(),
r => r?,
}
};
let report = optimize::optimize_with_progress(&mut cos, &settings, &mut |s| {
step(match s {
optimize::Stage::Fragments => OptimizeStage::Fragments,
+10
View File
@@ -3610,6 +3610,16 @@ fn create_and_reduce() {
assert_eq!(page_texts(&s3, r), ["Page 1", "Page 1"]);
}
/// Discarding a category that holds nothing discards nothing; it used to end the whole
/// optimization with an error about redaction marks.
#[test]
fn optimize_discarding_an_empty_category_still_runs() {
let (s, id) = session_with(2);
let (bytes, report) = s.optimized_bytes(id, &optimize::Settings::default(), &[crate::Hidden::Bookmarks, crate::Hidden::Comments]).unwrap();
assert!(bytes.starts_with(b"%PDF-"));
assert!(report.discarded.is_empty(), "{:?}", report.discarded);
}
#[test]
fn an_optimize_job_reports_its_stages_and_can_be_cancelled() {
use crate::optimizer::OptimizeStage;