xberg 1.0.7

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 101 formats and 306 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
//! Configuration loading integration tests.
//!
//! Tests the config loading APIs:
//! - from_file() with TOML/YAML/JSON
//! - discover() for searching parent directories
//! - Error handling for invalid configs

use std::fs;
use tempfile::TempDir;
use xberg::XbergError;
use xberg::core::config::ExtractionConfig;

/// Test loading config from TOML file.
#[test]
fn test_from_file_toml_succeeds() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.toml");

    let toml_content = r#"
[ocr]
enabled = true
backend = "tesseract"

[chunking]
max_chars = 1000
max_overlap = 100
"#;

    fs::write(&config_path, toml_content).expect("Operation failed");

    let config = ExtractionConfig::from_file(&config_path);
    assert!(config.is_ok(), "Should load TOML config successfully");

    let config = config.expect("Operation failed");
    assert!(config.ocr.is_some(), "Should have OCR config");
    assert!(config.chunking.is_some(), "Should have chunking config");

    let chunking = config.chunking.expect("Operation failed");
    assert_eq!(chunking.max_characters, 1000);
    assert_eq!(chunking.overlap, 100);
}

/// Test loading config from YAML file.
#[test]
fn test_from_file_yaml_succeeds() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.yaml");

    let yaml_content = r#"
ocr:
  enabled: true
  backend: tesseract
chunking:
  max_characters: 1000
  overlap: 100
"#;

    fs::write(&config_path, yaml_content).expect("Operation failed");

    let config = ExtractionConfig::from_file(&config_path);
    assert!(config.is_ok(), "Should load YAML config successfully");

    let config = config.expect("Operation failed");
    assert!(config.ocr.is_some(), "Should have OCR config");
    assert!(config.chunking.is_some(), "Should have chunking config");

    let chunking = config.chunking.expect("Operation failed");
    assert_eq!(chunking.max_characters, 1000);
    assert_eq!(chunking.overlap, 100);
}

/// Test loading config from JSON file.
#[test]
fn test_from_file_json_succeeds() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.json");

    let json_content = r#"
{
  "ocr": {
    "enabled": true,
    "backend": "tesseract"
  },
  "chunking": {
    "max_chars": 1000,
    "max_overlap": 100
  }
}
"#;

    fs::write(&config_path, json_content).expect("Operation failed");

    let config = ExtractionConfig::from_file(&config_path);
    assert!(config.is_ok(), "Should load JSON config successfully");

    let config = config.expect("Operation failed");
    assert!(config.ocr.is_some(), "Should have OCR config");
    assert!(config.chunking.is_some(), "Should have chunking config");

    let chunking = config.chunking.expect("Operation failed");
    assert_eq!(chunking.max_characters, 1000);
    assert_eq!(chunking.overlap, 100);
}

/// Test loading config from .yml extension.
#[test]
fn test_from_file_yml_extension_succeeds() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.yml");

    let yml_content = r#"
ocr:
  enabled: true
"#;

    fs::write(&config_path, yml_content).expect("Operation failed");

    let config = ExtractionConfig::from_file(&config_path);
    assert!(config.is_ok(), "Should load .yml config successfully");
}

/// Test from_file with nonexistent path fails.
#[test]
fn test_from_file_nonexistent_path_fails() {
    let result = ExtractionConfig::from_file("/nonexistent/path/config.toml");
    assert!(result.is_err(), "Should fail for nonexistent path: {:?}", result);
}

/// Test from_file with malformed TOML fails.
#[test]
fn test_from_file_malformed_toml_fails() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.toml");

    let malformed_toml = r#"
[ocr
enabled = true
"#;

    fs::write(&config_path, malformed_toml).expect("Operation failed");

    let result = ExtractionConfig::from_file(&config_path);
    assert!(result.is_err(), "Should fail for malformed TOML: {:?}", result);
}

/// Test from_file with malformed JSON fails.
#[test]
fn test_from_file_malformed_json_fails() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.json");

    let malformed_json = r#"
{
  "ocr": {
    "enabled": true
  }
  "chunking": {}
}
"#;

    fs::write(&config_path, malformed_json).expect("Operation failed");

    let result = ExtractionConfig::from_file(&config_path);
    assert!(result.is_err(), "Should fail for malformed JSON: {:?}", result);
}

/// Test from_file with malformed YAML fails.
#[test]
fn test_from_file_malformed_yaml_fails() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.yaml");

    let malformed_yaml = r#"
ocr:
  enabled: true
  - invalid_list
"#;

    fs::write(&config_path, malformed_yaml).expect("Operation failed");

    let result = ExtractionConfig::from_file(&config_path);
    assert!(result.is_err(), "Should fail for malformed YAML: {:?}", result);
}

/// Test from_file with empty file uses defaults.
#[test]
fn test_from_file_empty_file_uses_defaults() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.toml");

    fs::write(&config_path, "").expect("Operation failed");

    let config = ExtractionConfig::from_file(&config_path);
    assert!(config.is_ok(), "Should load empty file successfully");

    let config = config.expect("Operation failed");
    assert!(config.ocr.is_none(), "Default config should have no OCR");
    assert!(config.chunking.is_none(), "Default config should have no chunking");
}

/// Test from_file with unsupported extension fails.
#[test]
fn test_from_file_unsupported_extension_fails() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.txt");

    fs::write(&config_path, "ocr:\n  enabled: true").expect("Operation failed");

    let result = ExtractionConfig::from_file(&config_path);
    assert!(result.is_err(), "Should fail for unsupported extension: {:?}", result);

    if let Err(XbergError::Validation { message, .. }) = result {
        assert!(
            message.contains("format") || message.contains("extension") || message.contains("Unsupported"),
            "Error should mention format/extension: {}",
            message
        );
    }
}

/// Test discover() finds config in current directory.
#[test]
#[serial_test::serial]
fn test_discover_finds_config_in_current_dir() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("xberg.toml");

    let toml_content = r#"
[ocr]
enabled = true
"#;

    fs::write(&config_path, toml_content).expect("Operation failed");

    let original_dir = std::env::current_dir().expect("Operation failed");
    std::env::set_current_dir(temp_dir.path()).expect("Operation failed");

    let result = ExtractionConfig::discover();

    std::env::set_current_dir(original_dir).expect("Operation failed");

    assert!(result.is_ok(), "Discover should succeed");
    let config = result.expect("Operation failed");
    assert!(config.is_some(), "Should find config in current directory");
    assert!(
        config.expect("Operation failed").ocr.is_some(),
        "Should have OCR config"
    );
}

/// Test discover() finds config in parent directory.
#[test]
#[serial_test::serial]
fn test_discover_finds_config_in_parent_dir() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("xberg.toml");

    let toml_content = r#"
[ocr]
enabled = true
"#;

    fs::write(&config_path, toml_content).expect("Operation failed");

    let sub_dir = temp_dir.path().join("subdir");
    fs::create_dir(&sub_dir).expect("Operation failed");

    let original_dir = std::env::current_dir().expect("Operation failed");
    std::env::set_current_dir(&sub_dir).expect("Operation failed");

    let result = ExtractionConfig::discover();

    std::env::set_current_dir(original_dir).expect("Operation failed");

    assert!(result.is_ok(), "Discover should succeed");
    let config = result.expect("Operation failed");
    assert!(config.is_some(), "Should find config in parent directory");
    assert!(
        config.expect("Operation failed").ocr.is_some(),
        "Should have OCR config"
    );
}

/// Test discover() returns None when no config found.
#[test]
#[serial_test::serial]
fn test_discover_returns_none_when_not_found() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let sub_dir = temp_dir.path().join("subdir");
    fs::create_dir(&sub_dir).expect("Operation failed");

    let original_dir = std::env::current_dir().expect("Operation failed");
    std::env::set_current_dir(&sub_dir).expect("Operation failed");

    let result = ExtractionConfig::discover();

    std::env::set_current_dir(original_dir).expect("Operation failed");

    assert!(result.is_ok(), "Discover should succeed even when no config found");
    let _config = result.expect("Operation failed");
}

/// Test discover() prefers certain file names.
#[test]
#[serial_test::serial]
fn test_discover_file_name_preference() {
    let temp_dir = TempDir::new().expect("Operation failed");

    fs::write(temp_dir.path().join("xberg.toml"), "[ocr]\nenabled = true").expect("Operation failed");
    fs::write(temp_dir.path().join(".xberg.toml"), "[ocr]\nenabled = false").expect("Operation failed");

    let original_dir = std::env::current_dir().expect("Operation failed");
    if std::env::set_current_dir(temp_dir.path()).is_err() {
        return;
    }

    let result = ExtractionConfig::discover();

    let _ = std::env::set_current_dir(original_dir);

    assert!(result.is_ok(), "Discover should succeed");
    let config = result.expect("Operation failed");
    assert!(config.is_some(), "Should find a config file");
}

/// Test discover() with nested directories.
#[test]
#[serial_test::serial]
fn test_discover_with_nested_directories() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("xberg.toml");

    let toml_content = r#"
[ocr]
enabled = true
"#;

    fs::write(&config_path, toml_content).expect("Operation failed");

    let level1 = temp_dir.path().join("level1");
    let level2 = level1.join("level2");
    let level3 = level2.join("level3");
    fs::create_dir_all(&level3).expect("Operation failed");

    let original_dir = std::env::current_dir().expect("Operation failed");
    if std::env::set_current_dir(&level3).is_err() {
        return;
    }

    let result = ExtractionConfig::discover();

    let _ = std::env::set_current_dir(&original_dir);

    assert!(result.is_ok(), "Discover should succeed");
    let config = result.expect("Operation failed");
    assert!(config.is_some(), "Should find config in ancestor directory");
    assert!(
        config.expect("Operation failed").ocr.is_some(),
        "Should have OCR config"
    );
}

/// Test config loading with all supported features.
#[test]
fn test_from_file_comprehensive_config() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.toml");

    let toml_base = r#"
[ocr]
enabled = true
backend = "tesseract"

[chunking]
max_chars = 2000
max_overlap = 200

[language_detection]
enabled = true

[images]
extract_images = true
"#;

    #[cfg(not(feature = "pdf"))]
    let toml_content = toml_base.to_owned();
    #[cfg(feature = "pdf")]
    let toml_content = format!("{toml_base}\n[pdf_options]\nextract_images = true\n");

    fs::write(&config_path, toml_content).expect("Operation failed");

    let config = ExtractionConfig::from_file(&config_path);
    assert!(config.is_ok(), "Should load comprehensive config successfully");

    let config = config.expect("Operation failed");
    assert!(config.ocr.is_some(), "Should have OCR config");
    assert!(config.chunking.is_some(), "Should have chunking config");
    assert!(
        config.language_detection.is_some(),
        "Should have language detection config"
    );
    assert!(config.images.is_some(), "Should have image extraction config");
    #[cfg(feature = "pdf")]
    assert!(config.pdf_options.is_some(), "Should have PDF config");
}

/// Test config validation with invalid values.
#[test]
fn test_from_file_with_invalid_values() {
    let temp_dir = TempDir::new().expect("Operation failed");
    let config_path = temp_dir.path().join("config.toml");

    let toml_content = r#"
[chunking]
max_chars = -1000
max_overlap = -100
"#;

    fs::write(&config_path, toml_content).expect("Operation failed");

    let result = ExtractionConfig::from_file(&config_path);
    if let Ok(config) = result
        && let Some(chunking) = config.chunking
    {
        assert!(chunking.max_characters > 0, "max_characters should be positive");
    }
}