xberg 1.1.2

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
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
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
//! Document extractor registry implementation.

use crate::core::config::{ExtractInput, ExtractionConfig};
use crate::plugins::{DocumentExtractor, InternalDocumentExtractor, Plugin};
use crate::types::internal::InternalDocument;
use crate::{Result, XbergError};
use std::collections::{BTreeMap, HashMap};
use std::path::Path;
use std::sync::Arc;

/// Registered document extractor plus optional native pipeline capability.
#[derive(Clone)]
pub(crate) struct RegisteredDocumentExtractor {
    extractor: Arc<dyn DocumentExtractor>,
    internal: Option<Arc<dyn InternalDocumentExtractor>>,
}

impl RegisteredDocumentExtractor {
    fn public(extractor: Arc<dyn DocumentExtractor>) -> Self {
        Self {
            extractor,
            internal: None,
        }
    }

    fn internal<T>(extractor: Arc<T>) -> Self
    where
        T: InternalDocumentExtractor + 'static,
    {
        Self {
            extractor: extractor.clone(),
            internal: Some(extractor),
        }
    }

    pub(crate) fn extractor(&self) -> Arc<dyn DocumentExtractor> {
        Arc::clone(&self.extractor)
    }

    pub(crate) fn plugin(&self) -> &dyn DocumentExtractor {
        self.extractor.as_ref()
    }

    pub(crate) fn is_builtin(&self) -> bool {
        self.internal.is_some()
    }

    async fn extract_content_inner(
        &self,
        content: &[u8],
        mime_type: &str,
        config: &ExtractionConfig,
    ) -> Result<InternalDocument> {
        if let Some(internal) = &self.internal {
            return internal.extract_content(content, mime_type, config).await;
        }

        let result = self
            .extractor
            .extract(
                ExtractInput::from_bytes(content.to_vec(), mime_type.to_string(), None),
                config,
            )
            .await?;
        Ok(result.into())
    }

    async fn extract_path_inner(
        &self,
        path: &Path,
        mime_type: &str,
        config: &ExtractionConfig,
    ) -> Result<InternalDocument> {
        if let Some(internal) = &self.internal {
            return internal.extract_path(path, mime_type, config).await;
        }

        let mut input = ExtractInput::from_uri(path.to_string_lossy().into_owned());
        input.mime_type = Some(mime_type.to_string());
        let result = self.extractor.extract(input, config).await?;
        Ok(result.into())
    }
}

impl Plugin for RegisteredDocumentExtractor {
    fn name(&self) -> &str {
        self.plugin().name()
    }

    fn version(&self) -> String {
        self.plugin().version()
    }

    fn initialize(&self) -> Result<()> {
        self.plugin().initialize()
    }

    fn shutdown(&self) -> Result<()> {
        self.plugin().shutdown()
    }

    fn description(&self) -> &str {
        self.plugin().description()
    }

    fn author(&self) -> &str {
        self.plugin().author()
    }
}

#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
impl InternalDocumentExtractor for RegisteredDocumentExtractor {
    async fn extract_content(
        &self,
        content: &[u8],
        mime_type: &str,
        config: &ExtractionConfig,
    ) -> Result<InternalDocument> {
        self.extract_content_inner(content, mime_type, config).await
    }

    async fn extract_path(&self, path: &Path, mime_type: &str, config: &ExtractionConfig) -> Result<InternalDocument> {
        self.extract_path_inner(path, mime_type, config).await
    }

    fn supported_mime_types(&self) -> &[&str] {
        self.plugin().supported_mime_types()
    }

    fn priority(&self) -> i32 {
        self.plugin().priority()
    }

    fn can_handle(&self, path: &Path, mime_type: &str) -> bool {
        self.plugin().can_handle(path, mime_type)
    }
}

#[cfg_attr(alef, alef(skip))]
/// Registry for document extractor plugins.
///
/// Manages extractors with MIME type and priority-based selection.
///
/// # Thread Safety
///
/// The registry is thread-safe and can be accessed concurrently from multiple threads.
pub struct DocumentExtractorRegistry {
    extractors: HashMap<String, BTreeMap<i32, RegisteredDocumentExtractor>>,
    name_index: HashMap<String, Vec<(String, i32)>>,
}

impl DocumentExtractorRegistry {
    /// Create a new empty extractor registry.
    pub fn new() -> Self {
        Self {
            extractors: HashMap::new(),
            name_index: HashMap::new(),
        }
    }

    /// Register a document extractor.
    ///
    /// The extractor is registered for all MIME types it supports.
    ///
    /// # Arguments
    ///
    /// * `extractor` - The extractor to register
    ///
    /// # Returns
    ///
    /// - `Ok(())` if registration succeeded
    /// - `Err(...)` if initialization failed
    pub fn register(&mut self, extractor: Arc<dyn DocumentExtractor>) -> Result<()> {
        self.register_entry(RegisteredDocumentExtractor::public(extractor))
    }

    /// Register a native extractor with access to the internal pipeline representation.
    pub(crate) fn register_internal<T>(&mut self, extractor: Arc<T>) -> Result<()>
    where
        T: InternalDocumentExtractor + 'static,
    {
        self.register_entry(RegisteredDocumentExtractor::internal(extractor))
    }

    fn register_entry(&mut self, entry: RegisteredDocumentExtractor) -> Result<()> {
        let extractor = entry.extractor();
        let name = extractor.name().to_string();
        let priority = extractor.priority();
        let mime_types: Vec<String> = extractor.supported_mime_types().iter().map(|s| s.to_string()).collect();

        if let Err(e) = super::validate_plugin_name(&name) {
            tracing::warn!(
                "Failed to validate document extractor name '{}': {}. \
                 Registration aborted. Plugin names must be non-empty and contain only alphanumeric characters, hyphens, and underscores.",
                name,
                e
            );
            return Err(e);
        }

        if let Err(e) = extractor.initialize() {
            tracing::error!(
                "Failed to initialize document extractor '{}': {}. \
                 Extraction for MIME types {:?} will be unavailable.",
                name,
                e,
                mime_types
            );
            return Err(e);
        }

        if self.name_index.contains_key(&name) {
            tracing::debug!(
                "Document extractor '{}' is already registered. Removing old instance and registering new one.",
                name
            );
            self.remove(&name)?;
        }

        let mut index_entries = Vec::new();

        for mime_type in &mime_types {
            let priority_map = self.extractors.entry(mime_type.clone()).or_default();
            if let Some(displaced) = priority_map.insert(priority, entry.clone()) {
                let displaced_name = displaced.plugin().name().to_string();
                if displaced_name != name {
                    tracing::warn!(
                        "Document extractor '{}' claims MIME type '{}' at priority {}, the same \
                         (MIME type, priority) slot already held by extractor '{}'. Only one extractor \
                         can occupy a given slot; '{}' now shadows '{}' for this MIME type. Register at \
                         a distinct priority to keep both reachable.",
                        name,
                        mime_type,
                        priority,
                        displaced_name,
                        name,
                        displaced_name
                    );
                    // `displaced`'s name_index entry still lists this (mime_type, priority) pair, but
                    // priority_map no longer holds its slot — leaving that pair in place would let
                    // `remove(&displaced_name)` believe it cleaned up a slot it never touched, while
                    // the *other* MIME types `displaced_name` is still registered for stay orphaned
                    // (#216). Prune the stale pair so `remove` only tracks slots it actually owns. ~keep
                    if let Some(entries) = self.name_index.get_mut(&displaced_name) {
                        entries.retain(|(m, p)| !(m == mime_type && *p == priority));
                        if entries.is_empty() {
                            self.name_index.remove(&displaced_name);
                        }
                    }
                }
            }
            index_entries.push((mime_type.clone(), priority));
        }

        self.name_index.insert(name.clone(), index_entries);
        tracing::debug!(
            "Registered document extractor '{}' with priority {} for MIME types: {:?}",
            name,
            priority,
            mime_types
        );

        Ok(())
    }

    /// Get the highest priority extractor for a MIME type.
    ///
    /// # Arguments
    ///
    /// * `mime_type` - MIME type to look up
    ///
    /// # Returns
    ///
    /// The highest priority extractor, or an error if none found.
    #[cfg_attr(feature = "otel", tracing::instrument(
        skip(self),
        fields(
            registry.mime_type = %mime_type,
            registry.found = tracing::field::Empty,
        )
    ))]
    pub fn get(&self, mime_type: &str) -> Result<Arc<dyn DocumentExtractor>> {
        Ok(self.get_registered(mime_type)?.extractor())
    }

    pub(crate) fn get_registered(&self, mime_type: &str) -> Result<RegisteredDocumentExtractor> {
        if let Some(priority_map) = self.extractors.get(mime_type)
            && let Some((_priority, entry)) = priority_map.iter().next_back()
        {
            #[cfg(feature = "otel")]
            tracing::Span::current().record("registry.found", true);
            return Ok(entry.clone());
        }

        let mut best_match: Option<(i32, RegisteredDocumentExtractor)> = None;

        for (registered_mime, priority_map) in &self.extractors {
            if registered_mime.ends_with("/*") {
                let prefix = &registered_mime[..registered_mime.len() - 1];
                if mime_type.starts_with(prefix)
                    && let Some((_priority, entry)) = priority_map.iter().next_back()
                {
                    let priority = entry.extractor.priority();
                    match &best_match {
                        None => best_match = Some((priority, entry.clone())),
                        Some((current_priority, _)) => {
                            if priority > *current_priority {
                                best_match = Some((priority, entry.clone()));
                            }
                        }
                    }
                }
            }
        }

        if let Some((_priority, entry)) = best_match {
            #[cfg(feature = "otel")]
            tracing::Span::current().record("registry.found", true);
            return Ok(entry);
        }

        #[cfg(feature = "otel")]
        tracing::Span::current().record("registry.found", false);
        Err(XbergError::UnsupportedFormat(mime_type.to_string()))
    }

    /// Ordered extractor fallback candidates for `(path, mime_type)` (#217).
    ///
    /// Mirrors `get_registered`'s "exact MIME match wins over `type/*` wildcard"
    /// rule, but instead of returning only the single highest-priority match,
    /// returns every match in priority order (highest first) so a caller can
    /// fall back to the next one when the first fails. Unlike `get_registered`,
    /// this also consults [`DocumentExtractor::can_handle`]: an extractor that
    /// declares a MIME type but declines this specific file is excluded from
    /// the candidate list entirely, rather than being selected and left to fail.
    pub(crate) fn get_candidates(&self, path: &Path, mime_type: &str) -> Vec<RegisteredDocumentExtractor> {
        let mut exact: Vec<RegisteredDocumentExtractor> = Vec::new();
        if let Some(priority_map) = self.extractors.get(mime_type) {
            for entry in priority_map.values().rev() {
                if entry.plugin().can_handle(path, mime_type) {
                    exact.push(entry.clone());
                }
            }
        }
        if !exact.is_empty() {
            return exact;
        }

        let mut wildcard: Vec<(i32, RegisteredDocumentExtractor)> = Vec::new();
        for (registered_mime, priority_map) in &self.extractors {
            if registered_mime.ends_with("/*") {
                let prefix = &registered_mime[..registered_mime.len() - 1];
                if mime_type.starts_with(prefix) {
                    for entry in priority_map.values().rev() {
                        if entry.plugin().can_handle(path, mime_type) {
                            wildcard.push((entry.plugin().priority(), entry.clone()));
                        }
                    }
                }
            }
        }
        wildcard.sort_by_key(|(priority, _)| std::cmp::Reverse(*priority));
        wildcard.into_iter().map(|(_, entry)| entry).collect()
    }

    /// List all registered extractors.
    pub fn list(&self) -> Vec<String> {
        self.name_index.keys().cloned().collect()
    }

    /// Remove an extractor from the registry.
    pub fn remove(&mut self, name: &str) -> Result<()> {
        let index_entries = match self.name_index.remove(name) {
            Some(entries) => entries,
            None => {
                tracing::debug!(
                    "Document extractor '{}' not found in registry (already removed or never registered)",
                    name
                );
                return Ok(());
            }
        };

        let mut extractor_to_shutdown: Option<Arc<dyn DocumentExtractor>> = None;

        for (mime_type, priority) in index_entries {
            if let Some(priority_map) = self.extractors.get_mut(&mime_type) {
                if let Some(entry) = priority_map.remove(&priority)
                    && extractor_to_shutdown.is_none()
                {
                    extractor_to_shutdown = Some(entry.extractor());
                }

                if priority_map.is_empty() {
                    self.extractors.remove(&mime_type);
                }
            }
        }

        if let Some(extractor) = extractor_to_shutdown {
            if let Err(e) = extractor.shutdown() {
                tracing::warn!(
                    "Failed to shutdown document extractor '{}': {}. \
                     Resources may not have been properly released.",
                    name,
                    e
                );
                return Err(e);
            }
            tracing::debug!("Successfully removed and shut down document extractor '{}'", name);
        }

        Ok(())
    }

    /// Shutdown all extractors and clear the registry.
    pub fn shutdown_all(&mut self) -> Result<()> {
        let names = self.list();
        let count = names.len();

        if count > 0 {
            tracing::debug!("Shutting down {} document extractors", count);
        }

        for name in names {
            self.remove(&name)?;
        }

        if count > 0 {
            tracing::debug!("Successfully shut down all {} document extractors", count);
        }
        Ok(())
    }

    /// Drain the registry. Alias for `shutdown_all` used by alef trait-bridge codegen.
    pub fn clear(&mut self) -> Result<()> {
        self.shutdown_all()
    }
}

impl Default for DocumentExtractorRegistry {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::config::{ExtractInput, ExtractionConfig};
    use crate::plugins::{DocumentExtractor, InternalDocumentExtractor, Plugin};
    use crate::types::{ExtractedDocument, ExtractionMethod};

    use async_trait::async_trait;

    struct MockExtractor {
        name: String,
        mime_types: &'static [&'static str],
        priority: i32,
    }

    struct PublicMockExtractor {
        name: &'static str,
        extraction_method: Option<ExtractionMethod>,
    }

    impl Plugin for PublicMockExtractor {
        fn name(&self) -> &str {
            self.name
        }

        fn version(&self) -> String {
            "1.0.0".to_string()
        }
    }

    #[async_trait]
    impl DocumentExtractor for PublicMockExtractor {
        async fn extract(&self, _input: ExtractInput, _config: &ExtractionConfig) -> Result<ExtractedDocument> {
            Ok(ExtractedDocument {
                mime_type: "application/x-public".into(),
                extraction_method: self.extraction_method,
                ..Default::default()
            })
        }

        fn supported_mime_types(&self) -> &[&str] {
            &["application/x-public"]
        }
    }

    impl Plugin for MockExtractor {
        fn name(&self) -> &str {
            &self.name
        }
        fn version(&self) -> String {
            "1.0.0".to_string()
        }
        fn initialize(&self) -> Result<()> {
            Ok(())
        }
        fn shutdown(&self) -> Result<()> {
            Ok(())
        }
    }

    #[async_trait]
    impl InternalDocumentExtractor for MockExtractor {
        async fn extract_content(
            &self,
            _: &[u8],
            _: &str,
            _: &ExtractionConfig,
        ) -> Result<crate::types::internal::InternalDocument> {
            Ok(crate::types::internal::InternalDocument::new("mock"))
        }

        fn supported_mime_types(&self) -> &[&str] {
            self.mime_types
        }

        fn priority(&self) -> i32 {
            self.priority
        }
    }

    #[test]
    fn test_document_extractor_registry_exact_match() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractor = Arc::new(MockExtractor {
            name: "pdf-extractor".to_string(),
            mime_types: &["application/pdf"],
            priority: 100,
        });

        registry.register(extractor).unwrap();

        let retrieved = registry.get("application/pdf").unwrap();
        assert_eq!(retrieved.name(), "pdf-extractor");

        let names = registry.list();
        assert_eq!(names.len(), 1);
        assert!(names.contains(&"pdf-extractor".to_string()));
    }

    #[test]
    fn should_distinguish_builtin_extractors_from_public_plugins() {
        let builtin = Arc::new(MockExtractor {
            name: "builtin".to_string(),
            mime_types: &["application/x-builtin"],
            priority: 100,
        });
        let plugin = Arc::new(MockExtractor {
            name: "plugin".to_string(),
            mime_types: &["application/x-plugin"],
            priority: 100,
        });
        let mut registry = DocumentExtractorRegistry::new();
        registry.register_internal(builtin).unwrap();
        registry.register(plugin).unwrap();

        assert!(registry.get_registered("application/x-builtin").unwrap().is_builtin());
        assert!(!registry.get_registered("application/x-plugin").unwrap().is_builtin());
    }

    #[tokio::test]
    async fn should_preserve_explicit_public_plugin_provenance_for_bytes_and_paths() {
        let registered = RegisteredDocumentExtractor::public(Arc::new(PublicMockExtractor {
            name: "public-explicit",
            extraction_method: Some(ExtractionMethod::Ocr),
        }));
        let config = ExtractionConfig::default();

        let bytes = registered
            .extract_content_inner(b"content", "application/x-public", &config)
            .await
            .unwrap();
        let path = registered
            .extract_path_inner(Path::new("unused"), "application/x-public", &config)
            .await
            .unwrap();

        for document in [bytes, path] {
            assert_eq!(
                document.metadata.additional.get("extraction_method"),
                Some(&serde_json::Value::String("ocr".to_string()))
            );
        }
    }

    #[tokio::test]
    async fn should_leave_unspecified_public_plugin_provenance_absent_for_bytes_and_paths() {
        let registered = RegisteredDocumentExtractor::public(Arc::new(PublicMockExtractor {
            name: "public-unspecified",
            extraction_method: None,
        }));
        let config = ExtractionConfig::default();

        let bytes = registered
            .extract_content_inner(b"content", "application/x-public", &config)
            .await
            .unwrap();
        let path = registered
            .extract_path_inner(Path::new("unused"), "application/x-public", &config)
            .await
            .unwrap();

        for document in [bytes, path] {
            assert!(!document.metadata.additional.contains_key("extraction_method"));
        }
    }

    #[test]
    fn test_document_extractor_registry_prefix_match() {
        let mut registry = DocumentExtractorRegistry::new();

        let image_extractor = Arc::new(MockExtractor {
            name: "image-extractor".to_string(),
            mime_types: &["image/*"],
            priority: 50,
        });

        registry.register(image_extractor).unwrap();

        let retrieved = registry.get("image/png").unwrap();
        assert_eq!(retrieved.name(), "image-extractor");

        let retrieved_jpg = registry.get("image/jpeg").unwrap();
        assert_eq!(retrieved_jpg.name(), "image-extractor");
    }

    #[test]
    fn test_document_extractor_registry_priority() {
        let mut registry = DocumentExtractorRegistry::new();

        let low_priority = Arc::new(MockExtractor {
            name: "low-priority-pdf".to_string(),
            mime_types: &["application/pdf"],
            priority: 10,
        });

        let high_priority = Arc::new(MockExtractor {
            name: "high-priority-pdf".to_string(),
            mime_types: &["application/pdf"],
            priority: 100,
        });

        registry.register(low_priority).unwrap();
        registry.register(high_priority).unwrap();

        let retrieved = registry.get("application/pdf").unwrap();
        assert_eq!(retrieved.name(), "high-priority-pdf");
    }

    #[test]
    fn test_document_extractor_registry_not_found() {
        let registry = DocumentExtractorRegistry::new();

        let result = registry.get("application/unknown");
        assert!(matches!(result, Err(XbergError::UnsupportedFormat(_))));
    }

    #[test]
    fn test_document_extractor_registry_remove() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractor = Arc::new(MockExtractor {
            name: "test-extractor".to_string(),
            mime_types: &["text/plain"],
            priority: 50,
        });

        registry.register(extractor).unwrap();
        assert!(registry.get("text/plain").is_ok());

        registry.remove("test-extractor").unwrap();
        assert!(registry.get("text/plain").is_err());
    }

    #[test]
    fn test_document_extractor_registry_shutdown_all() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractor1 = Arc::new(MockExtractor {
            name: "extractor1".to_string(),
            mime_types: &["text/plain"],
            priority: 50,
        });

        let extractor2 = Arc::new(MockExtractor {
            name: "extractor2".to_string(),
            mime_types: &["application/pdf"],
            priority: 50,
        });

        registry.register(extractor1).unwrap();
        registry.register(extractor2).unwrap();

        assert_eq!(registry.list().len(), 2);

        registry.shutdown_all().unwrap();
        assert_eq!(registry.list().len(), 0);
    }

    #[test]
    fn test_document_extractor_registry_default() {
        let registry = DocumentExtractorRegistry::default();
        assert_eq!(registry.list().len(), 0);
    }

    #[test]
    fn test_document_extractor_registry_exact_over_prefix() {
        let mut registry = DocumentExtractorRegistry::new();

        let prefix_extractor = Arc::new(MockExtractor {
            name: "prefix-extractor".to_string(),
            mime_types: &["image/*"],
            priority: 100,
        });

        let exact_extractor = Arc::new(MockExtractor {
            name: "exact-extractor".to_string(),
            mime_types: &["image/png"],
            priority: 50,
        });

        registry.register(prefix_extractor).unwrap();
        registry.register(exact_extractor).unwrap();

        let retrieved = registry.get("image/png").unwrap();
        assert_eq!(retrieved.name(), "exact-extractor");

        let retrieved_jpg = registry.get("image/jpeg").unwrap();
        assert_eq!(retrieved_jpg.name(), "prefix-extractor");
    }

    #[test]
    fn test_document_extractor_registry_invalid_name_empty() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractor = Arc::new(MockExtractor {
            name: "".to_string(),
            mime_types: &["text/plain"],
            priority: 50,
        });

        let result = registry.register(extractor);
        assert!(matches!(result, Err(XbergError::Validation { .. })));
    }

    #[test]
    fn test_document_extractor_registry_invalid_name_whitespace() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractor = Arc::new(MockExtractor {
            name: "my extractor".to_string(),
            mime_types: &["text/plain"],
            priority: 50,
        });

        let result = registry.register(extractor);
        assert!(matches!(result, Err(XbergError::Validation { .. })));
    }

    #[test]
    fn test_document_extractor_registry_multiple_mime_types() {
        let mut registry = DocumentExtractorRegistry::new();

        let multi_extractor = Arc::new(MockExtractor {
            name: "multi-extractor".to_string(),
            mime_types: &["text/plain", "text/markdown", "text/html"],
            priority: 50,
        });

        registry.register(multi_extractor).unwrap();

        assert_eq!(registry.get("text/plain").unwrap().name(), "multi-extractor");
        assert_eq!(registry.get("text/markdown").unwrap().name(), "multi-extractor");
        assert_eq!(registry.get("text/html").unwrap().name(), "multi-extractor");
    }

    struct FailingExtractor {
        name: String,
        fail_on_init: bool,
    }

    impl Plugin for FailingExtractor {
        fn name(&self) -> &str {
            &self.name
        }
        fn version(&self) -> String {
            "1.0.0".to_string()
        }
        fn initialize(&self) -> Result<()> {
            if self.fail_on_init {
                Err(XbergError::Plugin {
                    message: "Extractor initialization failed".to_string(),
                    plugin_name: self.name.clone(),
                })
            } else {
                Ok(())
            }
        }
        fn shutdown(&self) -> Result<()> {
            Ok(())
        }
    }

    #[async_trait]
    impl InternalDocumentExtractor for FailingExtractor {
        async fn extract_content(
            &self,
            _: &[u8],
            _: &str,
            _: &ExtractionConfig,
        ) -> Result<crate::types::internal::InternalDocument> {
            Ok(crate::types::internal::InternalDocument::new("mock"))
        }

        fn supported_mime_types(&self) -> &[&str] {
            &["text/plain"]
        }

        fn priority(&self) -> i32 {
            50
        }
    }

    #[test]
    fn test_document_extractor_initialization_failure_logs_error() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractor = Arc::new(FailingExtractor {
            name: "failing-extractor".to_string(),
            fail_on_init: true,
        });

        let result = registry.register(extractor);
        assert!(result.is_err());
        assert_eq!(registry.list().len(), 0);
    }

    #[test]
    fn test_document_extractor_invalid_name_empty_logs_warning() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractor = Arc::new(MockExtractor {
            name: "".to_string(),
            mime_types: &["text/plain"],
            priority: 50,
        });

        let result = registry.register(extractor);
        assert!(matches!(result, Err(XbergError::Validation { .. })));
    }

    #[test]
    fn test_document_extractor_invalid_name_with_spaces_logs_warning() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractor = Arc::new(MockExtractor {
            name: "invalid extractor".to_string(),
            mime_types: &["text/plain"],
            priority: 50,
        });

        let result = registry.register(extractor);
        assert!(matches!(result, Err(XbergError::Validation { .. })));
    }

    #[test]
    fn test_document_extractor_successful_registration_logs_debug() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractor = Arc::new(MockExtractor {
            name: "valid-pdf-extractor".to_string(),
            mime_types: &["application/pdf"],
            priority: 100,
        });

        let result = registry.register(extractor);
        assert!(result.is_ok());
        assert_eq!(registry.list().len(), 1);
    }

    #[test]
    fn test_document_extractor_remove_nonexistent_logs_debug() {
        let mut registry = DocumentExtractorRegistry::new();

        let result = registry.remove("nonexistent-extractor");
        assert!(result.is_ok());
        assert_eq!(registry.list().len(), 0);
    }

    #[test]
    fn test_document_extractor_shutdown_empty_registry() {
        let mut registry = DocumentExtractorRegistry::new();
        let result = registry.shutdown_all();
        assert!(result.is_ok());
        assert_eq!(registry.list().len(), 0);
    }

    #[test]
    fn test_document_extractor_shutdown_with_multiple_extractors() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractor1 = Arc::new(MockExtractor {
            name: "extractor1".to_string(),
            mime_types: &["text/plain"],
            priority: 50,
        });

        let extractor2 = Arc::new(MockExtractor {
            name: "extractor2".to_string(),
            mime_types: &["application/pdf"],
            priority: 100,
        });

        let extractor3 = Arc::new(MockExtractor {
            name: "extractor3".to_string(),
            mime_types: &["image/png"],
            priority: 75,
        });

        registry.register(extractor1).unwrap();
        registry.register(extractor2).unwrap();
        registry.register(extractor3).unwrap();

        assert_eq!(registry.list().len(), 3);

        registry.shutdown_all().unwrap();
        assert_eq!(registry.list().len(), 0);
    }

    /// #216: two different extractors claiming the same MIME type at the same
    /// priority must not silently overwrite one another with no trace — the
    /// later registration should win the slot (documented, not accidental) and
    /// the collision must be observable, not merely inferred from which one
    /// answers `get()`.
    #[test]
    fn test_document_extractor_priority_collision_last_registration_wins_slot() {
        let mut registry = DocumentExtractorRegistry::new();

        let first = Arc::new(MockExtractor {
            name: "collider-a".to_string(),
            mime_types: &["application/x-collision"],
            priority: 50,
        });
        let second = Arc::new(MockExtractor {
            name: "collider-b".to_string(),
            mime_types: &["application/x-collision"],
            priority: 50,
        });

        registry.register(first).unwrap();
        registry.register(second).unwrap();

        // "collider-a" had only this one (MIME type, priority) slot, and "collider-b"
        // took it over entirely, so "collider-a" is left with nothing registered at
        // all — `list()` must not keep it around as an unreachable zombie entry.
        let names = registry.list();
        assert_eq!(names, vec!["collider-b".to_string()]);

        // The later registration answers the shared slot.
        assert_eq!(registry.get("application/x-collision").unwrap().name(), "collider-b");
    }

    /// #216: re-registering under a name that is already registered for a
    /// *different* MIME type must not orphan the old MIME entry. Before the
    /// fix, `name_index.insert` overwrote the old index entries wholesale, so
    /// `remove("dup-extractor")` could never reach "text/plain" again and the
    /// stale registration stayed in `get()` forever.
    #[test]
    fn test_document_extractor_duplicate_name_reregistration_does_not_orphan_old_mime() {
        let mut registry = DocumentExtractorRegistry::new();

        let first = Arc::new(MockExtractor {
            name: "dup-extractor".to_string(),
            mime_types: &["text/plain"],
            priority: 50,
        });
        registry.register(first).unwrap();
        assert!(registry.get("text/plain").is_ok());

        let second = Arc::new(MockExtractor {
            name: "dup-extractor".to_string(),
            mime_types: &["application/pdf"],
            priority: 50,
        });
        registry.register(second).unwrap();

        // The re-registration only claims application/pdf; text/plain must be
        // fully released, not orphaned as an unreachable zombie entry.
        assert!(registry.get("text/plain").is_err());
        assert_eq!(registry.get("application/pdf").unwrap().name(), "dup-extractor");

        // remove() must now be able to fully clean up the current registration.
        registry.remove("dup-extractor").unwrap();
        assert!(registry.get("application/pdf").is_err());
        assert_eq!(registry.list().len(), 0);
    }

    struct CanHandleExtractor {
        name: String,
        mime_types: &'static [&'static str],
        priority: i32,
        can_handle_result: bool,
    }

    impl Plugin for CanHandleExtractor {
        fn name(&self) -> &str {
            &self.name
        }
        fn version(&self) -> String {
            "1.0.0".to_string()
        }
        fn initialize(&self) -> Result<()> {
            Ok(())
        }
        fn shutdown(&self) -> Result<()> {
            Ok(())
        }
    }

    #[async_trait]
    impl InternalDocumentExtractor for CanHandleExtractor {
        async fn extract_content(
            &self,
            _: &[u8],
            _: &str,
            _: &ExtractionConfig,
        ) -> Result<crate::types::internal::InternalDocument> {
            Ok(crate::types::internal::InternalDocument::new("mock"))
        }

        fn supported_mime_types(&self) -> &[&str] {
            self.mime_types
        }

        fn priority(&self) -> i32 {
            self.priority
        }

        fn can_handle(&self, _path: &std::path::Path, _mime_type: &str) -> bool {
            self.can_handle_result
        }
    }

    /// #217: `get_candidates` must return every registered match for a MIME type
    /// in priority order (highest first), so a caller can fall back when the
    /// first one fails.
    #[test]
    fn test_get_candidates_orders_by_priority_descending() {
        let mut registry = DocumentExtractorRegistry::new();
        registry
            .register(Arc::new(MockExtractor {
                name: "low".to_string(),
                mime_types: &["application/pdf"],
                priority: 10,
            }))
            .unwrap();
        registry
            .register(Arc::new(MockExtractor {
                name: "high".to_string(),
                mime_types: &["application/pdf"],
                priority: 90,
            }))
            .unwrap();
        registry
            .register(Arc::new(MockExtractor {
                name: "mid".to_string(),
                mime_types: &["application/pdf"],
                priority: 50,
            }))
            .unwrap();

        let candidates = registry.get_candidates(std::path::Path::new("f.pdf"), "application/pdf");
        let names: Vec<&str> = candidates.iter().map(|c| c.plugin().name()).collect();
        assert_eq!(names, vec!["high", "mid", "low"]);
    }

    /// #217: an extractor whose `can_handle` declines this specific file must be
    /// excluded from the candidate list entirely, even though it declares the
    /// MIME type — it must never be "selected and left to fail".
    #[test]
    fn test_get_candidates_excludes_extractor_that_declines_can_handle() {
        let mut registry = DocumentExtractorRegistry::new();
        registry
            .register(Arc::new(CanHandleExtractor {
                name: "picky".to_string(),
                mime_types: &["application/pdf"],
                priority: 90,
                can_handle_result: false,
            }))
            .unwrap();
        registry
            .register(Arc::new(MockExtractor {
                name: "generic".to_string(),
                mime_types: &["application/pdf"],
                priority: 50,
            }))
            .unwrap();

        let candidates = registry.get_candidates(std::path::Path::new("f.pdf"), "application/pdf");
        let names: Vec<&str> = candidates.iter().map(|c| c.plugin().name()).collect();
        assert_eq!(
            names,
            vec!["generic"],
            "the can_handle=false extractor must be excluded, not just deprioritized"
        );
    }

    #[test]
    fn test_get_candidates_empty_when_nothing_registered() {
        let registry = DocumentExtractorRegistry::new();
        assert!(
            registry
                .get_candidates(std::path::Path::new("f.pdf"), "application/pdf")
                .is_empty()
        );
    }

    #[test]
    fn test_document_extractor_priority_ordering_complex() {
        let mut registry = DocumentExtractorRegistry::new();

        let extractors = vec![
            (
                Arc::new(MockExtractor {
                    name: "priority-1".to_string(),
                    mime_types: &["application/pdf"],
                    priority: 1,
                }),
                1,
            ),
            (
                Arc::new(MockExtractor {
                    name: "priority-100".to_string(),
                    mime_types: &["application/pdf"],
                    priority: 100,
                }),
                100,
            ),
            (
                Arc::new(MockExtractor {
                    name: "priority-50".to_string(),
                    mime_types: &["application/pdf"],
                    priority: 50,
                }),
                50,
            ),
        ];

        for (extractor, _priority) in &extractors {
            registry.register(extractor.clone()).unwrap();
        }

        let retrieved = registry.get("application/pdf").unwrap();
        assert_eq!(retrieved.name(), "priority-100");
    }
}