ppt-rs 0.2.14

Create, read, and update PowerPoint 2007+ (.pptx) files with rich formatting, bullet styles, themes, and templates.
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
//! PPTX Repair Module
//!
//! Provides functionality to detect and repair common issues in PPTX files:
//! - Missing required parts (Content_Types, relationships, etc.)
//! - Invalid or malformed XML
//! - Broken relationships
//! - Missing slide references
//! - Corrupted package structure

use crate::exc::{PptxError, Result};
use crate::opc::Package;
use std::collections::HashSet;
use std::path::Path;

/// Types of issues that can be detected in a PPTX file
#[derive(Debug, Clone, PartialEq)]
pub enum RepairIssue {
    /// Missing required part
    MissingPart { path: String, description: String },
    /// Invalid XML content
    InvalidXml { path: String, error: String },
    /// Broken relationship reference
    BrokenRelationship {
        source: String,
        target: String,
        rel_id: String,
    },
    /// Missing slide in presentation
    MissingSlideReference { slide_path: String },
    /// Orphan slide (not referenced in presentation)
    OrphanSlide { slide_path: String },
    /// Invalid content type
    InvalidContentType { path: String },
    /// Corrupted ZIP entry
    CorruptedEntry { path: String, error: String },
    /// Missing namespace declaration
    MissingNamespace { path: String, namespace: String },
    /// Empty required element
    EmptyRequiredElement { path: String, element: String },
}

impl RepairIssue {
    /// Get severity level (1-3, where 3 is critical)
    pub fn severity(&self) -> u8 {
        match self {
            RepairIssue::MissingPart { .. } => 3,
            RepairIssue::InvalidXml { .. } => 3,
            RepairIssue::BrokenRelationship { .. } => 2,
            RepairIssue::MissingSlideReference { .. } => 2,
            RepairIssue::OrphanSlide { .. } => 1,
            RepairIssue::InvalidContentType { .. } => 2,
            RepairIssue::CorruptedEntry { .. } => 3,
            RepairIssue::MissingNamespace { .. } => 2,
            RepairIssue::EmptyRequiredElement { .. } => 1,
        }
    }

    /// Check if this issue is auto-repairable
    pub fn is_repairable(&self) -> bool {
        match self {
            RepairIssue::MissingPart { .. } => true,
            RepairIssue::InvalidXml { .. } => true,
            RepairIssue::BrokenRelationship { .. } => true,
            RepairIssue::MissingSlideReference { .. } => true,
            RepairIssue::OrphanSlide { .. } => true,
            RepairIssue::InvalidContentType { .. } => true,
            RepairIssue::CorruptedEntry { .. } => false,
            RepairIssue::MissingNamespace { .. } => true,
            RepairIssue::EmptyRequiredElement { .. } => true,
        }
    }

    /// Get human-readable description
    pub fn description(&self) -> String {
        match self {
            RepairIssue::MissingPart { path, description } => {
                format!("Missing required part '{}': {}", path, description)
            }
            RepairIssue::InvalidXml { path, error } => {
                format!("Invalid XML in '{}': {}", path, error)
            }
            RepairIssue::BrokenRelationship {
                source,
                target,
                rel_id,
            } => {
                format!(
                    "Broken relationship in '{}': {} -> {} ({})",
                    source, rel_id, target, rel_id
                )
            }
            RepairIssue::MissingSlideReference { slide_path } => {
                format!(
                    "Slide '{}' exists but not referenced in presentation",
                    slide_path
                )
            }
            RepairIssue::OrphanSlide { slide_path } => {
                format!("Orphan slide reference: '{}' does not exist", slide_path)
            }
            RepairIssue::InvalidContentType { path } => {
                format!("Invalid or missing content type for '{}'", path)
            }
            RepairIssue::CorruptedEntry { path, error } => {
                format!("Corrupted ZIP entry '{}': {}", path, error)
            }
            RepairIssue::MissingNamespace { path, namespace } => {
                format!("Missing namespace '{}' in '{}'", namespace, path)
            }
            RepairIssue::EmptyRequiredElement { path, element } => {
                format!("Empty required element '{}' in '{}'", element, path)
            }
        }
    }
}

/// Result of a repair operation
#[derive(Debug, Clone)]
pub struct RepairResult {
    /// Issues found during validation
    pub issues_found: Vec<RepairIssue>,
    /// Issues that were successfully repaired
    pub issues_repaired: Vec<RepairIssue>,
    /// Issues that could not be repaired
    pub issues_unrepaired: Vec<RepairIssue>,
    /// Whether the file is now valid
    pub is_valid: bool,
}

impl RepairResult {
    fn new() -> Self {
        RepairResult {
            issues_found: Vec::new(),
            issues_repaired: Vec::new(),
            issues_unrepaired: Vec::new(),
            is_valid: true,
        }
    }

    /// Get total number of issues found
    pub fn total_issues(&self) -> usize {
        self.issues_found.len()
    }

    /// Get number of critical issues
    pub fn critical_issues(&self) -> usize {
        self.issues_found
            .iter()
            .filter(|i| i.severity() == 3)
            .count()
    }

    /// Check if all issues were repaired
    pub fn fully_repaired(&self) -> bool {
        self.issues_unrepaired.is_empty()
    }
}

/// PPTX Repair utility
pub struct PptxRepair {
    package: Package,
    issues: Vec<RepairIssue>,
}

impl PptxRepair {
    /// Required parts in a valid PPTX file
    const REQUIRED_PARTS: &'static [(&'static str, &'static str)] = &[
        ("[Content_Types].xml", "Content types definition"),
        ("_rels/.rels", "Package relationships"),
        ("ppt/presentation.xml", "Presentation document"),
        (
            "ppt/_rels/presentation.xml.rels",
            "Presentation relationships",
        ),
    ];

    /// Open a PPTX file for repair
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let package = Package::open(path)?;
        Ok(PptxRepair {
            package,
            issues: Vec::new(),
        })
    }

    /// Open from bytes
    pub fn from_bytes(data: &[u8]) -> Result<Self> {
        let cursor = std::io::Cursor::new(data);
        let package = Package::open_reader(cursor)?;
        Ok(PptxRepair {
            package,
            issues: Vec::new(),
        })
    }

    /// Validate the PPTX file and return found issues
    pub fn validate(&mut self) -> Vec<RepairIssue> {
        self.issues.clear();

        // Check required parts
        self.check_required_parts();

        // Check XML validity
        self.check_xml_validity();

        // Check relationships
        self.check_relationships();

        // Check slide references
        self.check_slide_references();

        // Check content types
        self.check_content_types();

        self.issues.clone()
    }

    /// Repair all detected issues
    pub fn repair(&mut self) -> RepairResult {
        let mut result = RepairResult::new();

        // First validate to find issues
        result.issues_found = self.validate();

        if result.issues_found.is_empty() {
            result.is_valid = true;
            return result;
        }

        // Attempt to repair each issue
        for issue in &result.issues_found.clone() {
            if issue.is_repairable() {
                match self.repair_issue(issue) {
                    Ok(()) => result.issues_repaired.push(issue.clone()),
                    Err(_) => result.issues_unrepaired.push(issue.clone()),
                }
            } else {
                result.issues_unrepaired.push(issue.clone());
            }
        }

        // Re-validate to check if repairs were successful
        let remaining_issues = self.validate();
        result.is_valid = remaining_issues.is_empty();

        result
    }

    /// Save the repaired PPTX to a file
    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        self.package.save(path)
    }

    /// Save to bytes
    pub fn to_bytes(&self) -> Result<Vec<u8>> {
        let mut cursor = std::io::Cursor::new(Vec::new());
        self.package.save_writer(&mut cursor)?;
        Ok(cursor.into_inner())
    }

    /// Get the underlying package
    pub fn package(&self) -> &Package {
        &self.package
    }

    /// Get mutable reference to package
    pub fn package_mut(&mut self) -> &mut Package {
        &mut self.package
    }

    // Validation methods

    fn check_required_parts(&mut self) {
        for (path, description) in Self::REQUIRED_PARTS {
            if !self.package.has_part(path) {
                self.issues.push(RepairIssue::MissingPart {
                    path: path.to_string(),
                    description: description.to_string(),
                });
            }
        }
    }

    fn check_xml_validity(&mut self) {
        let xml_parts: Vec<String> = self
            .package
            .part_paths()
            .iter()
            .filter(|p| p.ends_with(".xml") || p.ends_with(".rels"))
            .map(|s| s.to_string())
            .collect();

        for path in xml_parts {
            if let Some(content) = self.package.get_part(&path) {
                let xml_str = String::from_utf8_lossy(content);
                if let Err(e) = self.validate_xml(&xml_str) {
                    self.issues.push(RepairIssue::InvalidXml { path, error: e });
                }
            }
        }
    }

    fn validate_xml(&self, xml: &str) -> std::result::Result<(), String> {
        // Check for XML declaration
        let trimmed = xml.trim();
        if trimmed.is_empty() {
            return Err("Empty XML content".to_string());
        }

        // Basic well-formedness check
        let depth = 0i32;
        let mut in_tag = false;
        let mut in_string = false;
        let mut string_char = '"';

        for ch in trimmed.chars() {
            match ch {
                '"' | '\'' if in_tag && !in_string => {
                    in_string = true;
                    string_char = ch;
                }
                c if in_string && c == string_char => {
                    in_string = false;
                }
                '<' if !in_string => {
                    in_tag = true;
                }
                '>' if !in_string => {
                    in_tag = false;
                }
                '/' if in_tag && !in_string => {
                    // Self-closing or end tag
                }
                _ => {}
            }
        }

        // Try to use anyrepair for JSON-like content repair
        // For XML, we do basic validation
        if depth < 0 {
            return Err("Unbalanced tags".to_string());
        }

        Ok(())
    }

    fn check_relationships(&mut self) {
        // Check _rels/.rels
        self.check_rels_file("_rels/.rels");

        // Check ppt/_rels/presentation.xml.rels
        self.check_rels_file("ppt/_rels/presentation.xml.rels");

        // Check slide relationship files
        let slide_rels: Vec<String> = self
            .package
            .part_paths()
            .iter()
            .filter(|p| p.starts_with("ppt/slides/_rels/") && p.ends_with(".xml.rels"))
            .map(|s| s.to_string())
            .collect();

        for rels_path in slide_rels {
            self.check_rels_file(&rels_path);
        }
    }

    fn check_rels_file(&mut self, rels_path: &str) {
        if let Some(content) = self.package.get_part(rels_path) {
            let xml_str = String::from_utf8_lossy(content);

            // Parse relationships and check targets exist
            for line in xml_str.lines() {
                if line.contains("Relationship") && line.contains("Target=") {
                    if let Some(target) = self.extract_attribute(line, "Target") {
                        let rel_id = self.extract_attribute(line, "Id").unwrap_or_default();

                        // Skip external relationships
                        if target.starts_with("http://") || target.starts_with("https://") {
                            continue;
                        }

                        // Resolve relative path
                        let full_path = self.resolve_path(rels_path, &target);

                        if !self.package.has_part(&full_path) && !target.contains("..") {
                            self.issues.push(RepairIssue::BrokenRelationship {
                                source: rels_path.to_string(),
                                target: full_path,
                                rel_id,
                            });
                        }
                    }
                }
            }
        }
    }

    fn extract_attribute(&self, line: &str, attr: &str) -> Option<String> {
        let pattern = format!("{}=\"", attr);
        if let Some(start) = line.find(&pattern) {
            let value_start = start + pattern.len();
            if let Some(end) = line[value_start..].find('"') {
                return Some(line[value_start..value_start + end].to_string());
            }
        }
        None
    }

    fn resolve_path(&self, rels_path: &str, target: &str) -> String {
        if target.starts_with('/') {
            return target[1..].to_string();
        }

        // Get directory of rels file
        let rels_dir = rels_path.rsplit_once('/').map(|(d, _)| d).unwrap_or("");
        let base_dir = rels_dir
            .replace("_rels", "")
            .trim_end_matches('/')
            .to_string();

        if target.starts_with("../") {
            // Go up one directory
            let parent = base_dir.rsplit_once('/').map(|(d, _)| d).unwrap_or("");
            format!("{}/{}", parent, target.trim_start_matches("../"))
        } else {
            if base_dir.is_empty() {
                target.to_string()
            } else {
                format!("{}/{}", base_dir, target)
            }
        }
    }

    fn check_slide_references(&mut self) {
        // Get slides from presentation.xml.rels
        let mut referenced_slides: HashSet<String> = HashSet::new();

        if let Some(rels_content) = self.package.get_part("ppt/_rels/presentation.xml.rels") {
            let xml_str = String::from_utf8_lossy(rels_content);
            for line in xml_str.lines() {
                if line.contains("slide") && line.contains("Target=") {
                    if let Some(target) = self.extract_attribute(line, "Target") {
                        let full_path = if target.starts_with('/') {
                            target[1..].to_string()
                        } else {
                            format!("ppt/{}", target)
                        };
                        referenced_slides.insert(full_path);
                    }
                }
            }
        }

        // Get actual slide files
        let actual_slides: HashSet<String> = self
            .package
            .part_paths()
            .iter()
            .filter(|p| {
                p.starts_with("ppt/slides/slide") && p.ends_with(".xml") && !p.contains("_rels")
            })
            .map(|s| s.to_string())
            .collect();

        // Check for orphan references (referenced but don't exist)
        for slide in &referenced_slides {
            if !actual_slides.contains(slide) {
                self.issues.push(RepairIssue::OrphanSlide {
                    slide_path: slide.clone(),
                });
            }
        }

        // Check for unreferenced slides
        for slide in &actual_slides {
            if !referenced_slides.contains(slide) {
                self.issues.push(RepairIssue::MissingSlideReference {
                    slide_path: slide.clone(),
                });
            }
        }
    }

    fn check_content_types(&mut self) {
        if let Some(content) = self.package.get_part("[Content_Types].xml") {
            let xml_str = String::from_utf8_lossy(content);

            // Check that all parts have content types
            let parts = self.package.part_paths();
            for part in parts {
                if part == "[Content_Types].xml" {
                    continue;
                }

                // Check if part has Override or matches Default
                let has_override = xml_str.contains(&format!("PartName=\"/{}\"", part));
                let extension = part.rsplit('.').next().unwrap_or("");
                let has_default = xml_str.contains(&format!("Extension=\"{}\"", extension));

                if !has_override && !has_default && !part.ends_with(".rels") {
                    self.issues.push(RepairIssue::InvalidContentType {
                        path: part.to_string(),
                    });
                }
            }
        }
    }

    // Repair methods

    fn repair_issue(&mut self, issue: &RepairIssue) -> Result<()> {
        match issue {
            RepairIssue::MissingPart { path, .. } => self.repair_missing_part(path),
            RepairIssue::InvalidXml { path, .. } => self.repair_invalid_xml(path),
            RepairIssue::BrokenRelationship {
                source,
                target,
                rel_id,
            } => self.repair_broken_relationship(source, target, rel_id),
            RepairIssue::MissingSlideReference { slide_path } => {
                self.repair_missing_slide_reference(slide_path)
            }
            RepairIssue::OrphanSlide { slide_path } => self.repair_orphan_slide(slide_path),
            RepairIssue::InvalidContentType { path } => self.repair_invalid_content_type(path),
            RepairIssue::MissingNamespace { path, namespace } => {
                self.repair_missing_namespace(path, namespace)
            }
            RepairIssue::EmptyRequiredElement { path, element } => {
                self.repair_empty_element(path, element)
            }
            RepairIssue::CorruptedEntry { .. } => Err(PptxError::Generic(
                "Cannot repair corrupted entry".to_string(),
            )),
        }
    }

    fn repair_missing_part(&mut self, path: &str) -> Result<()> {
        let content = match path {
            "[Content_Types].xml" => self.generate_content_types(),
            "_rels/.rels" => self.generate_package_rels(),
            "ppt/presentation.xml" => self.generate_presentation_xml(),
            "ppt/_rels/presentation.xml.rels" => self.generate_presentation_rels(),
            _ => {
                return Err(PptxError::Generic(format!(
                    "Cannot generate part: {}",
                    path
                )))
            }
        };

        self.package
            .add_part(path.to_string(), content.into_bytes());
        Ok(())
    }

    fn repair_invalid_xml(&mut self, path: &str) -> Result<()> {
        if let Some(content) = self.package.get_part(path) {
            let xml_str = String::from_utf8_lossy(content).to_string();

            // Try to repair using anyrepair for JSON-like structures
            // For XML, we try basic fixes
            let repaired = self.attempt_xml_repair(&xml_str);

            self.package
                .add_part(path.to_string(), repaired.into_bytes());
        }
        Ok(())
    }

    fn attempt_xml_repair(&self, xml: &str) -> String {
        let mut repaired = xml.to_string();

        // Ensure XML declaration
        if !repaired.trim().starts_with("<?xml") {
            repaired = format!(
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n{}",
                repaired
            );
        }

        // Fix common issues
        // Replace bare & with &amp; (but not existing entities)
        // Since Rust regex doesn't support look-ahead, we use a different approach
        let mut result = String::new();
        let mut chars = repaired.chars().peekable();
        while let Some(ch) = chars.next() {
            if ch == '&' {
                // Check if this is already an entity
                let mut entity = String::from("&");
                let mut is_entity = false;
                let mut temp_chars: Vec<char> = Vec::new();

                while let Some(&next) = chars.peek() {
                    if next == ';' {
                        entity.push(chars.next().unwrap());
                        is_entity = entity == "&amp;"
                            || entity == "&lt;"
                            || entity == "&gt;"
                            || entity == "&quot;"
                            || entity == "&apos;"
                            || entity.starts_with("&#");
                        break;
                    } else if next.is_alphanumeric() || next == '#' || next == 'x' {
                        temp_chars.push(chars.next().unwrap());
                        entity.push(*temp_chars.last().unwrap());
                    } else {
                        break;
                    }
                    if entity.len() > 10 {
                        break;
                    }
                }

                if is_entity {
                    result.push_str(&entity);
                } else {
                    result.push_str("&amp;");
                    for c in temp_chars {
                        result.push(c);
                    }
                }
            } else {
                result.push(ch);
            }
        }
        repaired = result;

        repaired
    }

    fn repair_broken_relationship(
        &mut self,
        source: &str,
        _target: &str,
        rel_id: &str,
    ) -> Result<()> {
        // Remove the broken relationship from the rels file
        if let Some(content) = self.package.get_part(source) {
            let xml_str = String::from_utf8_lossy(content).to_string();

            // Remove the relationship line with this ID
            let pattern = format!("Id=\"{}\"", rel_id);
            let lines: Vec<&str> = xml_str
                .lines()
                .filter(|line| !line.contains(&pattern))
                .collect();

            let repaired = lines.join("\n");
            self.package
                .add_part(source.to_string(), repaired.into_bytes());
        }
        Ok(())
    }

    fn repair_missing_slide_reference(&mut self, slide_path: &str) -> Result<()> {
        // Add the slide to presentation.xml.rels
        if let Some(content) = self.package.get_part("ppt/_rels/presentation.xml.rels") {
            let xml_str = String::from_utf8_lossy(content).to_string();

            // Find the highest rId
            let max_id = self.find_max_rel_id(&xml_str);
            let new_id = format!("rId{}", max_id + 1);

            // Extract slide number from path
            let slide_target = slide_path.replace("ppt/", "");

            // Add new relationship before </Relationships>
            let new_rel = format!(
                "  <Relationship Id=\"{}\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide\" Target=\"{}\"/>",
                new_id, slide_target
            );

            let repaired = xml_str.replace(
                "</Relationships>",
                &format!("{}\n</Relationships>", new_rel),
            );
            self.package.add_part(
                "ppt/_rels/presentation.xml.rels".to_string(),
                repaired.into_bytes(),
            );

            // Also update presentation.xml sldIdLst
            self.add_slide_to_presentation(&new_id)?;
        }
        Ok(())
    }

    fn add_slide_to_presentation(&mut self, rel_id: &str) -> Result<()> {
        if let Some(content) = self.package.get_part("ppt/presentation.xml") {
            let xml_str = String::from_utf8_lossy(content).to_string();

            // Find highest slide ID
            let max_slide_id = self.find_max_slide_id(&xml_str);
            let new_slide_id = max_slide_id + 1;

            // Add to sldIdLst
            let new_entry = format!("<p:sldId id=\"{}\" r:id=\"{}\"/>", new_slide_id, rel_id);

            let repaired = if xml_str.contains("</p:sldIdLst>") {
                xml_str.replace("</p:sldIdLst>", &format!("{}\n</p:sldIdLst>", new_entry))
            } else if xml_str.contains("<p:sldIdLst/>") {
                xml_str.replace(
                    "<p:sldIdLst/>",
                    &format!("<p:sldIdLst>{}</p:sldIdLst>", new_entry),
                )
            } else {
                // Insert sldIdLst after sldMasterIdLst
                xml_str.replace(
                    "</p:sldMasterIdLst>",
                    &format!(
                        "</p:sldMasterIdLst>\n<p:sldIdLst>{}</p:sldIdLst>",
                        new_entry
                    ),
                )
            };

            self.package
                .add_part("ppt/presentation.xml".to_string(), repaired.into_bytes());
        }
        Ok(())
    }

    fn find_max_rel_id(&self, xml: &str) -> u32 {
        let re = regex::Regex::new(r#"Id="rId(\d+)""#).unwrap();
        re.captures_iter(xml)
            .filter_map(|cap| cap.get(1).and_then(|m| m.as_str().parse::<u32>().ok()))
            .max()
            .unwrap_or(0)
    }

    fn find_max_slide_id(&self, xml: &str) -> u32 {
        let re = regex::Regex::new(r#"<p:sldId id="(\d+)""#).unwrap();
        re.captures_iter(xml)
            .filter_map(|cap| cap.get(1).and_then(|m| m.as_str().parse::<u32>().ok()))
            .max()
            .unwrap_or(255)
    }

    fn repair_orphan_slide(&mut self, slide_path: &str) -> Result<()> {
        // Remove the orphan reference from presentation.xml.rels
        if let Some(content) = self.package.get_part("ppt/_rels/presentation.xml.rels") {
            let xml_str = String::from_utf8_lossy(content).to_string();
            let slide_target = slide_path.replace("ppt/", "");

            // Find and remove the relationship
            let lines: Vec<&str> = xml_str
                .lines()
                .filter(|line| !line.contains(&format!("Target=\"{}\"", slide_target)))
                .collect();

            let repaired = lines.join("\n");
            self.package.add_part(
                "ppt/_rels/presentation.xml.rels".to_string(),
                repaired.into_bytes(),
            );
        }
        Ok(())
    }

    fn repair_invalid_content_type(&mut self, path: &str) -> Result<()> {
        if let Some(content) = self.package.get_part("[Content_Types].xml") {
            let xml_str = String::from_utf8_lossy(content).to_string();

            // Determine content type based on path
            let content_type = self.infer_content_type(path);

            // Add Override entry
            let new_override = format!(
                "  <Override PartName=\"/{}\" ContentType=\"{}\"/>",
                path, content_type
            );

            let repaired = xml_str.replace("</Types>", &format!("{}\n</Types>", new_override));
            self.package
                .add_part("[Content_Types].xml".to_string(), repaired.into_bytes());
        }
        Ok(())
    }

    fn infer_content_type(&self, path: &str) -> &'static str {
        if path.contains("slide") && path.ends_with(".xml") {
            "application/vnd.openxmlformats-officedocument.presentationml.slide+xml"
        } else if path.contains("slideLayout") {
            "application/vnd.openxmlformats-officedocument.presentationml.slideLayout+xml"
        } else if path.contains("slideMaster") {
            "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml"
        } else if path.contains("theme") {
            "application/vnd.openxmlformats-officedocument.theme+xml"
        } else if path.contains("presentation.xml") {
            "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"
        } else if path.ends_with(".xml") {
            "application/xml"
        } else {
            "application/octet-stream"
        }
    }

    fn repair_missing_namespace(&mut self, path: &str, namespace: &str) -> Result<()> {
        if let Some(content) = self.package.get_part(path) {
            let xml_str = String::from_utf8_lossy(content).to_string();

            // Add namespace to root element
            let ns_decl = match namespace {
                "p" => "xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\"",
                "a" => "xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"",
                "r" => "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"",
                _ => return Ok(()),
            };

            // Find first element and add namespace
            if let Some(pos) = xml_str.find('>') {
                if !xml_str[..pos].contains(ns_decl) {
                    let repaired = format!("{} {}{}", &xml_str[..pos], ns_decl, &xml_str[pos..]);
                    self.package
                        .add_part(path.to_string(), repaired.into_bytes());
                }
            }
        }
        Ok(())
    }

    fn repair_empty_element(&mut self, path: &str, element: &str) -> Result<()> {
        if let Some(content) = self.package.get_part(path) {
            let xml_str = String::from_utf8_lossy(content).to_string();

            // Add minimal content to empty element
            let empty_pattern = format!("<{}/>", element);
            let filled = format!("<{}></{}>", element, element);

            let repaired = xml_str.replace(&empty_pattern, &filled);
            self.package
                .add_part(path.to_string(), repaired.into_bytes());
        }
        Ok(())
    }

    // Template generators

    fn generate_content_types(&self) -> String {
        let mut content = String::from(
            r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
  <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
  <Default Extension="xml" ContentType="application/xml"/>
  <Default Extension="jpeg" ContentType="image/jpeg"/>
  <Default Extension="png" ContentType="image/png"/>
"#,
        );

        // Add overrides for existing parts
        for path in self.package.part_paths() {
            if path.ends_with(".xml") && path != "[Content_Types].xml" {
                let ct = self.infer_content_type(path);
                content.push_str(&format!(
                    "  <Override PartName=\"/{}\" ContentType=\"{}\"/>\n",
                    path, ct
                ));
            }
        }

        content.push_str("</Types>");
        content
    }

    fn generate_package_rels(&self) -> String {
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/>
  <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
  <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
</Relationships>"#.to_string()
    }

    fn generate_presentation_xml(&self) -> String {
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:presentation xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
  <p:sldMasterIdLst>
    <p:sldMasterId id="2147483648" r:id="rId1"/>
  </p:sldMasterIdLst>
  <p:sldIdLst/>
  <p:sldSz cx="9144000" cy="6858000"/>
  <p:notesSz cx="6858000" cy="9144000"/>
</p:presentation>"#.to_string()
    }

    fn generate_presentation_rels(&self) -> String {
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster" Target="slideMasters/slideMaster1.xml"/>
  <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
  <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps" Target="presProps.xml"/>
  <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/viewProps" Target="viewProps.xml"/>
  <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/tableStyles" Target="tableStyles.xml"/>
</Relationships>"#.to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_repair_issue_severity() {
        let issue = RepairIssue::MissingPart {
            path: "test".to_string(),
            description: "test".to_string(),
        };
        assert_eq!(issue.severity(), 3);

        let issue = RepairIssue::OrphanSlide {
            slide_path: "test".to_string(),
        };
        assert_eq!(issue.severity(), 1);
    }

    #[test]
    fn test_repair_issue_is_repairable() {
        let issue = RepairIssue::MissingPart {
            path: "test".to_string(),
            description: "test".to_string(),
        };
        assert!(issue.is_repairable());

        let issue = RepairIssue::CorruptedEntry {
            path: "test".to_string(),
            error: "test".to_string(),
        };
        assert!(!issue.is_repairable());
    }

    #[test]
    fn test_repair_issue_description() {
        let issue = RepairIssue::MissingPart {
            path: "[Content_Types].xml".to_string(),
            description: "Content types definition".to_string(),
        };
        assert!(issue.description().contains("[Content_Types].xml"));
    }

    #[test]
    fn test_repair_result_new() {
        let result = RepairResult::new();
        assert!(result.issues_found.is_empty());
        assert!(result.is_valid);
    }

    #[test]
    fn test_repair_result_total_issues() {
        let mut result = RepairResult::new();
        result.issues_found.push(RepairIssue::MissingPart {
            path: "test".to_string(),
            description: "test".to_string(),
        });
        assert_eq!(result.total_issues(), 1);
    }

    #[test]
    fn test_repair_result_critical_issues() {
        let mut result = RepairResult::new();
        result.issues_found.push(RepairIssue::MissingPart {
            path: "test".to_string(),
            description: "test".to_string(),
        });
        result.issues_found.push(RepairIssue::OrphanSlide {
            slide_path: "test".to_string(),
        });
        assert_eq!(result.critical_issues(), 1);
    }

    #[test]
    fn test_repair_result_fully_repaired() {
        let result = RepairResult::new();
        assert!(result.fully_repaired());
    }

    #[test]
    fn test_infer_content_type() {
        let repair = PptxRepair {
            package: Package::new(),
            issues: Vec::new(),
        };

        assert_eq!(
            repair.infer_content_type("ppt/slides/slide1.xml"),
            "application/vnd.openxmlformats-officedocument.presentationml.slide+xml"
        );
        assert_eq!(
            repair.infer_content_type("ppt/presentation.xml"),
            "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"
        );
    }

    #[test]
    fn test_resolve_path() {
        let repair = PptxRepair {
            package: Package::new(),
            issues: Vec::new(),
        };

        assert_eq!(
            repair.resolve_path("ppt/_rels/presentation.xml.rels", "slides/slide1.xml"),
            "ppt/slides/slide1.xml"
        );
        assert_eq!(
            repair.resolve_path("_rels/.rels", "ppt/presentation.xml"),
            "ppt/presentation.xml"
        );
    }

    #[test]
    fn test_find_max_rel_id() {
        let repair = PptxRepair {
            package: Package::new(),
            issues: Vec::new(),
        };

        let xml = r#"<Relationships>
            <Relationship Id="rId1" Target="a"/>
            <Relationship Id="rId5" Target="b"/>
            <Relationship Id="rId3" Target="c"/>
        </Relationships>"#;

        assert_eq!(repair.find_max_rel_id(xml), 5);
    }

    #[test]
    fn test_find_max_slide_id() {
        let repair = PptxRepair {
            package: Package::new(),
            issues: Vec::new(),
        };

        let xml = r#"<p:sldIdLst>
            <p:sldId id="256" r:id="rId1"/>
            <p:sldId id="260" r:id="rId2"/>
            <p:sldId id="258" r:id="rId3"/>
        </p:sldIdLst>"#;

        assert_eq!(repair.find_max_slide_id(xml), 260);
    }

    #[test]
    fn test_extract_attribute() {
        let repair = PptxRepair {
            package: Package::new(),
            issues: Vec::new(),
        };

        let line = r#"<Relationship Id="rId1" Target="slides/slide1.xml"/>"#;
        assert_eq!(
            repair.extract_attribute(line, "Id"),
            Some("rId1".to_string())
        );
        assert_eq!(
            repair.extract_attribute(line, "Target"),
            Some("slides/slide1.xml".to_string())
        );
        assert_eq!(repair.extract_attribute(line, "Missing"), None);
    }

    #[test]
    fn test_generate_content_types() {
        let repair = PptxRepair {
            package: Package::new(),
            issues: Vec::new(),
        };

        let content = repair.generate_content_types();
        assert!(content.contains("<?xml"));
        assert!(content.contains("<Types"));
        assert!(content.contains("Extension=\"rels\""));
    }

    #[test]
    fn test_generate_package_rels() {
        let repair = PptxRepair {
            package: Package::new(),
            issues: Vec::new(),
        };

        let content = repair.generate_package_rels();
        assert!(content.contains("<?xml"));
        assert!(content.contains("<Relationships"));
        assert!(content.contains("presentation.xml"));
    }

    #[test]
    fn test_generate_presentation_xml() {
        let repair = PptxRepair {
            package: Package::new(),
            issues: Vec::new(),
        };

        let content = repair.generate_presentation_xml();
        assert!(content.contains("<?xml"));
        assert!(content.contains("<p:presentation"));
        assert!(content.contains("p:sldIdLst"));
    }

    #[test]
    fn test_attempt_xml_repair() {
        let repair = PptxRepair {
            package: Package::new(),
            issues: Vec::new(),
        };

        // Test adding XML declaration
        let xml = "<root><child/></root>";
        let repaired = repair.attempt_xml_repair(xml);
        assert!(repaired.starts_with("<?xml"));

        // Test ampersand fix
        let xml = "<?xml version=\"1.0\"?><root>A & B</root>";
        let repaired = repair.attempt_xml_repair(xml);
        assert!(repaired.contains("A &amp; B"));
    }
}