oxidize-pdf 2.5.1

A pure Rust PDF generation and manipulation library with zero external dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! PDF error recovery and repair functionality
//!
//! This module provides tools for handling corrupted or malformed PDF files,
//! attempting to recover as much content as possible.
//!
//! # Features
//!
//! - **Structure Recovery**: Rebuild cross-reference tables and object references
//! - **Content Recovery**: Extract readable content from damaged streams
//! - **Page Recovery**: Salvage individual pages from corrupted documents
//! - **Metadata Recovery**: Recover document information when possible
//! - **Partial Parsing**: Continue parsing despite errors
//! - **Repair Strategies**: Multiple approaches for different corruption types
//!
//! # Example
//!
//! ```rust,no_run
//! use oxidize_pdf::recovery::{RecoveryOptions, PdfRecovery};
//! use std::path::Path;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let options = RecoveryOptions::default()
//!     .with_aggressive_recovery(true)
//!     .with_partial_content(true);
//!
//! let mut recovery = PdfRecovery::new(options);
//!
//! match recovery.recover_document("corrupted.pdf") {
//!     Ok(mut doc) => {
//!         println!("Recovered {} pages", doc.page_count());
//!         doc.save("recovered.pdf")?;
//!     }
//!     Err(e) => {
//!         println!("Recovery failed: {}", e);
//!         
//!         // Try partial recovery
//!         if let Ok(partial) = recovery.recover_partial("corrupted.pdf") {
//!             println!("Partial recovery: {} pages", partial.recovered_pages.len());
//!         }
//!     }
//! }
//! # Ok(())
//! # }
//! ```

use crate::error::{PdfError, Result};
use crate::parser::PdfReader;
use crate::Document;
use std::collections::HashMap;
use std::io::{Read, Seek};
use std::path::Path;

pub mod corruption;
pub mod repair;
pub mod scanner;
pub mod validator;
pub mod xref_recovery;

// Re-export main types
pub use corruption::{detect_corruption, CorruptionReport, CorruptionType};
pub use repair::{repair_document, RepairResult, RepairStrategy};
pub use scanner::{ObjectScanner, ScanResult};
pub use validator::{validate_pdf, ValidationError, ValidationResult};
pub use xref_recovery::{needs_xref_recovery, recover_xref, XRefRecovery};

/// Options for PDF recovery
#[derive(Debug, Clone)]
pub struct RecoveryOptions {
    /// Enable aggressive recovery attempts
    pub aggressive_recovery: bool,
    /// Allow partial content recovery
    pub partial_content: bool,
    /// Maximum errors before giving up
    pub max_errors: usize,
    /// Attempt to rebuild cross-references
    pub rebuild_xref: bool,
    /// Try to recover embedded files
    pub recover_embedded: bool,
    /// Skip validation of recovered content
    pub skip_validation: bool,
    /// Memory limit for recovery operations
    pub memory_limit: usize,
}

impl Default for RecoveryOptions {
    fn default() -> Self {
        Self {
            aggressive_recovery: false,
            partial_content: true,
            max_errors: 100,
            rebuild_xref: true,
            recover_embedded: false,
            skip_validation: false,
            memory_limit: 500 * 1024 * 1024, // 500MB
        }
    }
}

impl RecoveryOptions {
    /// Enable aggressive recovery
    pub fn with_aggressive_recovery(mut self, aggressive: bool) -> Self {
        self.aggressive_recovery = aggressive;
        self
    }

    /// Enable partial content recovery
    pub fn with_partial_content(mut self, partial: bool) -> Self {
        self.partial_content = partial;
        self
    }

    /// Set maximum error threshold
    pub fn with_max_errors(mut self, max: usize) -> Self {
        self.max_errors = max;
        self
    }

    /// Set memory limit
    pub fn with_memory_limit(mut self, limit: usize) -> Self {
        self.memory_limit = limit;
        self
    }
}

/// PDF recovery engine
pub struct PdfRecovery {
    options: RecoveryOptions,
    error_count: usize,
    warnings: Vec<String>,
}

impl PdfRecovery {
    /// Create a new recovery engine
    pub fn new(options: RecoveryOptions) -> Self {
        Self {
            options,
            error_count: 0,
            warnings: Vec::new(),
        }
    }

    /// Attempt to recover a corrupted PDF document
    pub fn recover_document<P: AsRef<Path>>(&mut self, path: P) -> Result<Document> {
        let path = path.as_ref();

        // First, try standard parsing
        match PdfReader::open_document(path) {
            Ok(doc) => {
                self.warnings
                    .push("Document opened normally, no recovery needed".to_string());
                return self.convert_to_document(doc);
            }
            Err(e) => {
                self.warnings.push(format!("Standard parsing failed: {e}"));
            }
        }

        // Detect corruption type
        let corruption = detect_corruption(path)?;
        self.warnings.push(format!(
            "Detected corruption: {:?}",
            corruption.corruption_type
        ));

        // Apply repair strategy
        let strategy = RepairStrategy::for_corruption(&corruption.corruption_type);
        let repair_result = repair_document(path, strategy, &self.options)?;

        if let Some(doc) = repair_result.recovered_document {
            Ok(doc)
        } else {
            Err(PdfError::InvalidStructure(
                "Failed to recover document".to_string(),
            ))
        }
    }

    /// Attempt partial recovery of a corrupted PDF
    pub fn recover_partial<P: AsRef<Path>>(&mut self, path: P) -> Result<PartialRecovery> {
        let path = path.as_ref();
        let mut partial = PartialRecovery::default();

        // Scan for valid objects
        let mut scanner = ObjectScanner::new();
        let scan_result = scanner.scan_file(path)?;

        partial.total_objects = scan_result.total_objects;
        partial.recovered_objects = scan_result.valid_objects;

        // Try to recover individual pages
        for page_num in 0..scan_result.estimated_pages {
            if let Ok(page_content) = self.recover_page(path, page_num) {
                partial.recovered_pages.push(RecoveredPage {
                    page_number: page_num,
                    content: page_content,
                    has_text: true,
                    has_images: false,
                });
            }
        }

        // Recover metadata if possible
        if let Ok(metadata) = self.recover_metadata(path) {
            partial.metadata = Some(metadata);
        }

        partial.recovery_warnings = self.warnings.clone();

        Ok(partial)
    }

    /// Get recovery warnings
    pub fn warnings(&self) -> &[String] {
        &self.warnings
    }

    /// Clear warnings
    pub fn clear_warnings(&mut self) {
        self.warnings.clear();
        self.error_count = 0;
    }

    fn convert_to_document<R: Read + Seek>(
        &self,
        pdf_doc: crate::parser::PdfDocument<R>,
    ) -> Result<Document> {
        let mut doc = Document::new();

        // Convert pages
        let page_count = pdf_doc
            .page_count()
            .map_err(|e| PdfError::InvalidStructure(e.to_string()))?;
        for i in 0..page_count {
            if let Ok(page) = pdf_doc.get_page(i) {
                // Simple conversion - would need proper implementation
                let new_page = crate::Page::new(page.width(), page.height());
                doc.add_page(new_page);
            }
        }

        Ok(doc)
    }

    fn recover_page<P: AsRef<Path>>(&mut self, _path: P, _page_num: u32) -> Result<String> {
        // Simplified page recovery
        Ok(format!("Recovered content for page {_page_num}"))
    }

    fn recover_metadata<P: AsRef<Path>>(&mut self, _path: P) -> Result<HashMap<String, String>> {
        let mut metadata = HashMap::new();
        metadata.insert("Title".to_string(), "Recovered Document".to_string());
        metadata.insert("RecoveryDate".to_string(), chrono::Utc::now().to_string());
        Ok(metadata)
    }
}

/// Result of partial recovery
#[derive(Debug, Default)]
pub struct PartialRecovery {
    /// Successfully recovered pages
    pub recovered_pages: Vec<RecoveredPage>,
    /// Total objects found
    pub total_objects: usize,
    /// Successfully recovered objects
    pub recovered_objects: usize,
    /// Recovered metadata
    pub metadata: Option<HashMap<String, String>>,
    /// Warnings during recovery
    pub recovery_warnings: Vec<String>,
}

/// A recovered page
#[derive(Debug)]
pub struct RecoveredPage {
    /// Page number (0-based)
    pub page_number: u32,
    /// Recovered content as text
    pub content: String,
    /// Whether text was recovered
    pub has_text: bool,
    /// Whether images were recovered
    pub has_images: bool,
}

/// Quick recovery function for simple cases
pub fn quick_recover<P: AsRef<Path>>(path: P) -> Result<Document> {
    let mut recovery = PdfRecovery::new(RecoveryOptions::default());
    recovery.recover_document(path)
}

/// Analyze a PDF file for corruption
pub fn analyze_corruption<P: AsRef<Path>>(path: P) -> Result<CorruptionReport> {
    detect_corruption(path)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_recovery_options() {
        let options = RecoveryOptions::default();
        assert!(!options.aggressive_recovery);
        assert!(options.partial_content);
        assert_eq!(options.max_errors, 100);

        let options = options.with_aggressive_recovery(true).with_max_errors(50);
        assert!(options.aggressive_recovery);
        assert_eq!(options.max_errors, 50);
    }

    #[test]
    fn test_pdf_recovery_creation() {
        let recovery = PdfRecovery::new(RecoveryOptions::default());
        assert_eq!(recovery.error_count, 0);
        assert!(recovery.warnings.is_empty());
    }

    #[test]
    fn test_partial_recovery_default() {
        let partial = PartialRecovery::default();
        assert!(partial.recovered_pages.is_empty());
        assert_eq!(partial.total_objects, 0);
        assert_eq!(partial.recovered_objects, 0);
        assert!(partial.metadata.is_none());
    }

    #[test]
    fn test_recovery_options_all_setters() {
        let options = RecoveryOptions::default()
            .with_aggressive_recovery(true)
            .with_partial_content(false)
            .with_max_errors(200)
            .with_memory_limit(1024 * 1024 * 1024);

        assert!(options.aggressive_recovery);
        assert!(!options.partial_content);
        assert_eq!(options.max_errors, 200);
        assert_eq!(options.memory_limit, 1024 * 1024 * 1024);
        assert!(options.rebuild_xref);
        assert!(!options.recover_embedded);
        assert!(!options.skip_validation);
    }

    #[test]
    fn test_recovery_options_clone() {
        let options1 = RecoveryOptions::default()
            .with_aggressive_recovery(true)
            .with_max_errors(50);

        let options2 = options1.clone();
        assert_eq!(options1.aggressive_recovery, options2.aggressive_recovery);
        assert_eq!(options1.max_errors, options2.max_errors);
        assert_eq!(options1.memory_limit, options2.memory_limit);
    }

    #[test]
    fn test_recovery_options_debug() {
        let options = RecoveryOptions::default();
        let debug_str = format!("{options:?}");
        assert!(debug_str.contains("RecoveryOptions"));
        assert!(debug_str.contains("aggressive_recovery"));
        assert!(debug_str.contains("max_errors"));
    }

    #[test]
    fn test_pdf_recovery_clear_warnings() {
        let mut recovery = PdfRecovery::new(RecoveryOptions::default());

        // Add some warnings
        recovery.warnings.push("Warning 1".to_string());
        recovery.warnings.push("Warning 2".to_string());
        recovery.error_count = 5;

        assert_eq!(recovery.warnings.len(), 2);
        assert_eq!(recovery.error_count, 5);

        // Clear warnings
        recovery.clear_warnings();

        assert!(recovery.warnings.is_empty());
        assert_eq!(recovery.error_count, 0);
    }

    #[test]
    fn test_recovered_page_creation() {
        let page = RecoveredPage {
            page_number: 0,
            content: "Test content".to_string(),
            has_text: true,
            has_images: false,
        };

        assert_eq!(page.page_number, 0);
        assert_eq!(page.content, "Test content");
        assert!(page.has_text);
        assert!(!page.has_images);
    }

    #[test]
    fn test_recovered_page_debug() {
        let page = RecoveredPage {
            page_number: 5,
            content: "Page content".to_string(),
            has_text: true,
            has_images: true,
        };

        let debug_str = format!("{page:?}");
        assert!(debug_str.contains("RecoveredPage"));
        assert!(debug_str.contains("page_number: 5"));
    }

    #[test]
    fn test_partial_recovery_with_data() {
        let mut partial = PartialRecovery::default();

        // Add recovered pages
        partial.recovered_pages.push(RecoveredPage {
            page_number: 0,
            content: "Page 1".to_string(),
            has_text: true,
            has_images: false,
        });

        partial.recovered_pages.push(RecoveredPage {
            page_number: 1,
            content: "Page 2".to_string(),
            has_text: true,
            has_images: true,
        });

        // Set object counts
        partial.total_objects = 100;
        partial.recovered_objects = 85;

        // Add metadata
        let mut metadata = HashMap::new();
        metadata.insert("Title".to_string(), "Test Document".to_string());
        metadata.insert("Author".to_string(), "Test Author".to_string());
        partial.metadata = Some(metadata);

        // Add warnings
        partial.recovery_warnings.push("Warning 1".to_string());
        partial.recovery_warnings.push("Warning 2".to_string());

        // Verify
        assert_eq!(partial.recovered_pages.len(), 2);
        assert_eq!(partial.total_objects, 100);
        assert_eq!(partial.recovered_objects, 85);
        assert!(partial.metadata.is_some());
        assert_eq!(partial.recovery_warnings.len(), 2);
    }

    #[test]
    fn test_partial_recovery_debug() {
        let partial = PartialRecovery {
            recovered_pages: vec![],
            total_objects: 50,
            recovered_objects: 45,
            metadata: None,
            recovery_warnings: vec!["Test warning".to_string()],
        };

        let debug_str = format!("{partial:?}");
        assert!(debug_str.contains("PartialRecovery"));
        assert!(debug_str.contains("total_objects: 50"));
        assert!(debug_str.contains("recovered_objects: 45"));
    }

    #[test]
    fn test_recovery_with_memory_limit() {
        let options = RecoveryOptions::default().with_memory_limit(1024 * 1024); // 1MB limit

        let recovery = PdfRecovery::new(options);
        assert_eq!(recovery.options.memory_limit, 1024 * 1024);
    }

    #[test]
    fn test_recovery_warnings_accumulation() {
        let mut recovery = PdfRecovery::new(RecoveryOptions::default());

        // Add warnings
        recovery.warnings.push("First warning".to_string());
        recovery.warnings.push("Second warning".to_string());

        let warnings = recovery.warnings();
        assert_eq!(warnings.len(), 2);
        assert_eq!(warnings[0], "First warning");
        assert_eq!(warnings[1], "Second warning");
    }

    #[test]
    fn test_recovery_options_with_all_flags() {
        let options = RecoveryOptions {
            aggressive_recovery: true,
            partial_content: true,
            max_errors: 200,
            rebuild_xref: true,
            recover_embedded: true,
            skip_validation: true,
            memory_limit: 1024 * 1024 * 1024,
        };

        assert!(options.aggressive_recovery);
        assert!(options.partial_content);
        assert_eq!(options.max_errors, 200);
        assert!(options.rebuild_xref);
        assert!(options.recover_embedded);
        assert!(options.skip_validation);
        assert_eq!(options.memory_limit, 1024 * 1024 * 1024);
    }

    #[test]
    fn test_quick_recover_function() {
        // Test the quick_recover function exists and returns appropriate error
        // since we don't have a valid PDF file
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("nonexistent.pdf");

        let result = quick_recover(&path);
        assert!(result.is_err());
    }

    #[test]
    fn test_analyze_corruption_function() {
        // Test the analyze_corruption function exists
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("test.pdf");

        // Create a minimal file
        fs::write(&path, b"Not a PDF").unwrap();

        let result = analyze_corruption(&path);
        // Should either succeed or fail appropriately
        match result {
            Ok(_report) => {
                // If it succeeds, it's a valid report
            }
            Err(_) => {
                // Failure is also acceptable for invalid file
            }
        }
    }

    #[test]
    fn test_recovery_error_count() {
        let mut recovery = PdfRecovery::new(RecoveryOptions::default());
        assert_eq!(recovery.error_count, 0);

        // Simulate error counting
        recovery.error_count += 1;
        assert_eq!(recovery.error_count, 1);

        recovery.error_count += 5;
        assert_eq!(recovery.error_count, 6);

        // Clear should reset error count
        recovery.clear_warnings();
        assert_eq!(recovery.error_count, 0);
    }

    #[test]
    fn test_recovery_metadata_extraction() {
        let mut recovery = PdfRecovery::new(RecoveryOptions::default());

        // Test metadata extraction (private method, so test indirectly)
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("test.pdf");
        fs::write(&path, b"test").unwrap();

        let metadata = recovery.recover_metadata(&path).unwrap();
        assert!(metadata.contains_key("Title"));
        assert!(metadata.contains_key("RecoveryDate"));
        assert_eq!(metadata.get("Title").unwrap(), "Recovered Document");
    }

    #[test]
    fn test_recovery_page_extraction() {
        let mut recovery = PdfRecovery::new(RecoveryOptions::default());

        // Test page recovery (private method, so test indirectly)
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("test.pdf");
        fs::write(&path, b"test").unwrap();

        let content = recovery.recover_page(&path, 0).unwrap();
        assert_eq!(content, "Recovered content for page 0");

        let content2 = recovery.recover_page(&path, 5).unwrap();
        assert_eq!(content2, "Recovered content for page 5");
    }

    #[test]
    fn test_recovery_options_defaults() {
        let options = RecoveryOptions::default();

        // Verify all default values
        assert!(!options.aggressive_recovery);
        assert!(options.partial_content);
        assert_eq!(options.max_errors, 100);
        assert!(options.rebuild_xref);
        assert!(!options.recover_embedded);
        assert!(!options.skip_validation);
        assert_eq!(options.memory_limit, 500 * 1024 * 1024); // 500MB
    }

    #[test]
    fn test_recovery_with_skip_validation() {
        let options = RecoveryOptions {
            skip_validation: true,
            ..Default::default()
        };

        let recovery = PdfRecovery::new(options);
        assert!(recovery.options.skip_validation);
    }

    #[test]
    fn test_recovery_with_embedded_files() {
        let options = RecoveryOptions {
            recover_embedded: true,
            ..Default::default()
        };

        let recovery = PdfRecovery::new(options);
        assert!(recovery.options.recover_embedded);
    }

    #[test]
    fn test_partial_recovery_empty_warnings() {
        let partial = PartialRecovery {
            recovered_pages: vec![],
            total_objects: 0,
            recovered_objects: 0,
            metadata: None,
            recovery_warnings: vec![],
        };

        assert!(partial.recovery_warnings.is_empty());
    }

    #[test]
    fn test_recovery_options_chaining() {
        // Test method chaining works correctly
        let options = RecoveryOptions::default()
            .with_aggressive_recovery(true)
            .with_partial_content(false)
            .with_max_errors(50)
            .with_memory_limit(256 * 1024 * 1024)
            .with_aggressive_recovery(false); // Override previous

        assert!(!options.aggressive_recovery); // Should be false
        assert!(!options.partial_content);
        assert_eq!(options.max_errors, 50);
        assert_eq!(options.memory_limit, 256 * 1024 * 1024);
    }

    #[test]
    fn test_recovery_metadata_with_dates() {
        let mut recovery = PdfRecovery::new(RecoveryOptions::default());

        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path().join("test.pdf");
        fs::write(&path, b"test").unwrap();

        let metadata = recovery.recover_metadata(&path).unwrap();

        // Check RecoveryDate is properly formatted
        let recovery_date = metadata.get("RecoveryDate").unwrap();
        assert!(recovery_date.contains("20")); // Year
        assert!(recovery_date.contains("T")); // Time separator
    }

    #[test]
    fn test_partial_recovery_page_ordering() {
        let mut partial = PartialRecovery::default();

        // Add pages in non-sequential order
        partial.recovered_pages.push(RecoveredPage {
            page_number: 2,
            content: "Page 3".to_string(),
            has_text: true,
            has_images: false,
        });

        partial.recovered_pages.push(RecoveredPage {
            page_number: 0,
            content: "Page 1".to_string(),
            has_text: true,
            has_images: false,
        });

        partial.recovered_pages.push(RecoveredPage {
            page_number: 1,
            content: "Page 2".to_string(),
            has_text: false,
            has_images: true,
        });

        // Verify pages are stored as added (not automatically sorted)
        assert_eq!(partial.recovered_pages[0].page_number, 2);
        assert_eq!(partial.recovered_pages[1].page_number, 0);
        assert_eq!(partial.recovered_pages[2].page_number, 1);
    }

    #[test]
    fn test_recovery_with_max_errors_limit() {
        let options = RecoveryOptions::default().with_max_errors(1); // Very low limit

        let mut recovery = PdfRecovery::new(options);

        // Simulate hitting error limit
        recovery.error_count = 2;

        // In real implementation, recovery would stop when error_count > max_errors
        assert!(recovery.error_count > recovery.options.max_errors);
    }

    #[test]
    fn test_recovered_page_mixed_content() {
        let page = RecoveredPage {
            page_number: 10,
            content: "Mixed content with text and images".to_string(),
            has_text: true,
            has_images: true,
        };

        assert!(page.has_text);
        assert!(page.has_images);
        assert_eq!(page.page_number, 10);
    }

    #[test]
    fn test_recovery_warnings_immutable_access() {
        let mut recovery = PdfRecovery::new(RecoveryOptions::default());
        recovery.warnings.push("Test warning".to_string());

        // Test that warnings() returns immutable reference
        let warnings_ref = recovery.warnings();
        assert_eq!(warnings_ref.len(), 1);
        assert_eq!(warnings_ref[0], "Test warning");

        // Original can still be modified
        recovery.warnings.push("Another warning".to_string());
        assert_eq!(recovery.warnings.len(), 2);
    }

    #[test]
    fn test_partial_recovery_statistics() {
        let partial = PartialRecovery {
            recovered_pages: vec![
                RecoveredPage {
                    page_number: 0,
                    content: "Page 1".to_string(),
                    has_text: true,
                    has_images: false,
                },
                RecoveredPage {
                    page_number: 1,
                    content: "Page 2".to_string(),
                    has_text: true,
                    has_images: true,
                },
            ],
            total_objects: 150,
            recovered_objects: 142,
            metadata: Some(HashMap::new()),
            recovery_warnings: vec!["Minor issue".to_string()],
        };

        // Calculate recovery percentage
        let recovery_percentage =
            (partial.recovered_objects as f64 / partial.total_objects as f64) * 100.0;
        assert!(recovery_percentage > 94.0 && recovery_percentage < 95.0);

        // Count pages with different content types
        let text_only_pages = partial
            .recovered_pages
            .iter()
            .filter(|p| p.has_text && !p.has_images)
            .count();
        assert_eq!(text_only_pages, 1);

        let mixed_content_pages = partial
            .recovered_pages
            .iter()
            .filter(|p| p.has_text && p.has_images)
            .count();
        assert_eq!(mixed_content_pages, 1);
    }
}