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
//! Plugin system for extending Xberg functionality.
//!
//! The plugin system provides a trait-based architecture that allows extending
//! Xberg with custom extractors, OCR backends, post-processors, and validators.
//!
//! # Plugin Types
//!
//! - [`Plugin`] - Base trait that all plugins must implement
//! - [`OcrBackend`] - OCR processing plugins
//! - [`EmbeddingBackend`] - In-process embedding backend plugins
//! - [`DocumentExtractor`] - Document format extraction plugins
//! - [`PostProcessor`] - Content post-processing plugins
//! - [`Validator`] - Validation plugins
//!
//! # Language Support
//!
//! Plugins can be implemented in:
//! - **Rust** (native, highest performance)
//! - **Python** (via PyO3 FFI bridge)
//! - **Node.js** (future - via napi-rs FFI bridge)
//!
//! # Lifecycle Pattern
//!
//! Plugins are stored in `Arc<dyn Trait>` for thread-safe shared access:
//!
//! ```rust
//! use xberg::plugins::{Plugin, DocumentExtractor};
//! use xberg::plugins::registry::get_document_extractor_registry;
//! use std::sync::Arc;
//!
//! # struct MyExtractor;
//! # use xberg::{ExtractInput, ExtractionConfig, ExtractedDocument};
//! # impl xberg::plugins::Plugin for MyExtractor {
//! # fn name(&self) -> &str { "my" }
//! # fn version(&self) -> String { "1.0.0".to_string() }
//! # fn initialize(&self) -> xberg::Result<()> { Ok(()) }
//! # fn shutdown(&self) -> xberg::Result<()> { Ok(()) }
//! # }
//! # #[async_trait::async_trait]
//! # impl DocumentExtractor for MyExtractor {
//! # async fn extract(&self, _: ExtractInput, _: &ExtractionConfig) -> xberg::Result<ExtractedDocument> {
//! # Ok(ExtractedDocument::default())
//! # }
//! # fn supported_mime_types(&self) -> &[&str] { &[] }
//! # fn priority(&self) -> i32 { 50 }
//! # }
//! // 1. Create plugin instance
//! let plugin = MyExtractor;
//!
//! // 2. Wrap in Arc for registration
//! let plugin = Arc::new(plugin);
//!
//! // 3. Register with registry (calls initialize internally)
//! let registry = get_document_extractor_registry();
//! let mut registry = registry.write();
//! registry.register(plugin)?;
//! # Ok::<(), xberg::XbergError>(())
//! ```
//!
//! # Example: Custom Document Extractor
//!
//! ```rust
//! use xberg::plugins::{Plugin, DocumentExtractor};
//! use xberg::{ExtractInput, ExtractionConfig, Result};
//! use xberg::types::{ExtractedDocument, Metadata};
//! use async_trait::async_trait;
//!
//! struct CustomJsonExtractor;
//!
//! impl Plugin for CustomJsonExtractor {
//! fn name(&self) -> &str { "custom-json-extractor" }
//! fn version(&self) -> String { "1.0.0".to_string() }
//! fn initialize(&self) -> Result<()> {
//! println!("JSON extractor initialized");
//! Ok(())
//! }
//! fn shutdown(&self) -> Result<()> {
//! println!("JSON extractor shutdown");
//! Ok(())
//! }
//! }
//!
//! #[async_trait]
//! impl DocumentExtractor for CustomJsonExtractor {
//! async fn extract(&self, input: ExtractInput, _config: &ExtractionConfig)
//! -> Result<ExtractedDocument> {
//! // Parse JSON and extract all string values
//! let content = input.bytes.unwrap_or_default();
//! let json: serde_json::Value = serde_json::from_slice(&content)?;
//! let extracted_text = extract_strings_from_json(&json);
//!
//! let mut metadata = Metadata::default();
//! metadata.additional.insert("extracted_fields".to_string().into(), serde_json::json!(true));
//!
//! // `ExtractedDocument` has private internal fields, so a struct literal with
//! // `..Default::default()` does not compile outside the crate. Build a default
//! // and assign the public fields instead.
//! let mut document = ExtractedDocument::default();
//! document.content = extracted_text;
//! document.mime_type = std::borrow::Cow::Borrowed("application/json");
//! document.metadata = metadata;
//! Ok(document)
//! }
//!
//! fn supported_mime_types(&self) -> &[&str] {
//! &["application/json", "text/json"]
//! }
//!
//! fn priority(&self) -> i32 { 50 } // Default priority
//! }
//!
//! fn extract_strings_from_json(value: &serde_json::Value) -> String {
//! match value {
//! serde_json::Value::String(s) => format!("{}\n", s),
//! serde_json::Value::Array(arr) => {
//! arr.iter().map(extract_strings_from_json).collect()
//! }
//! serde_json::Value::Object(obj) => {
//! obj.values().map(extract_strings_from_json).collect()
//! }
//! _ => String::new(),
//! }
//! }
//! ```
//!
//! # Safety and Threading
//!
//! **CRITICAL**: All plugins must be `Send + Sync` because they are:
//! - Stored in `Arc<dyn Trait>` for shared ownership
//! - Accessed concurrently from multiple threads
//! - Called with `&self` (shared references)
//!
//! **Interior Mutability Pattern**:
//! Since plugins receive `&self` (not `&mut self`), use these for mutable state:
//! - `Mutex<T>` - Exclusive access, blocking
//! - `RwLock<T>` - Shared read, exclusive write
//! - `AtomicBool` / `AtomicU64` - Lock-free primitives
//! - `OnceCell<T>` - One-time initialization
//!
//! ```rust
//! use xberg::plugins::Plugin;
//! use std::sync::Mutex;
//!
//! struct StatefulPlugin {
//! // Use interior mutability for state
//! call_count: std::sync::atomic::AtomicU64,
//! cache: Mutex<Option<Vec<String>>>,
//! }
//!
//! impl Plugin for StatefulPlugin {
//! fn name(&self) -> &str { "stateful-plugin" }
//! fn version(&self) -> String { "1.0.0".to_string() }
//!
//! fn initialize(&self) -> xberg::Result<()> {
//! // Modify through interior mutability
//! let mut cache = self.cache.lock().unwrap();
//! *cache = Some(vec!["initialized".to_string()]);
//! Ok(())
//! }
//!
//! fn shutdown(&self) -> xberg::Result<()> {
//! self.call_count.store(0, std::sync::atomic::Ordering::Release);
//! Ok(())
//! }
//! }
//! ```
pub
pub
pub
pub
pub use ;
pub use ;
pub use ;
pub use ;
pub use InternalRenderer;
pub use ensure_renderers_initialized;
pub use ;
pub use ;
pub use ;
pub use Plugin;
pub use ;
/// Re-exports for the OCR backend plugin type, used by alef-generated bindings.
/// Re-exports for the post-processor plugin type, used by alef-generated bindings.
/// Re-exports for the embedding backend plugin type, used by alef-generated bindings.
/// Re-exports for the reranker backend plugin type, used by alef-generated bindings.
///
/// Re-exports for the tokenizer backend plugin type, used by alef-generated bindings.
/// Re-exports for the document extractor plugin type, used by alef-generated bindings.
pub use get_embedding_backend_registry;
pub use ensure_ocr_backends_initialized;