xberg 1.1.4

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
//! API consistency tests for ExtractionConfig and related types.
//!
//! This test suite validates that:
//! 1. ExtractionConfig serialization is complete with all fields
//! 2. All required configuration fields are present
//! 3. Configuration types maintain consistency across different formats
//! 4. No configuration fields are accidentally hidden or lost

use serde_json::json;
use xberg::core::config::ExtractionConfig;
use xberg::core::config::OutputFormat;
#[cfg(feature = "tree-sitter")]
use xberg::core::config::{TreeSitterConfig, TreeSitterProcessConfig};

#[test]
fn test_extraction_config_serialization_includes_all_fields() {
    let config = ExtractionConfig::default();
    let json = serde_json::to_value(&config).expect("Failed to serialize config");

    assert!(
        json.get("use_cache").is_some(),
        "Missing 'use_cache' field in serialized config"
    );
    assert!(
        json.get("enable_quality_processing").is_some(),
        "Missing 'enable_quality_processing' field"
    );
    assert!(
        json.get("force_ocr").is_some(),
        "Missing 'force_ocr' field in serialized config"
    );
    assert!(
        json.get("max_concurrent_extractions").is_some(),
        "Missing 'max_concurrent_extractions' field"
    );
    assert!(
        json.get("result_format").is_some(),
        "Missing 'result_format' field in serialized config"
    );
    assert!(
        json.get("output_format").is_some(),
        "Missing 'output_format' field in serialized config"
    );
}

#[test]
fn test_extraction_config_defaults_are_correct() {
    let config = ExtractionConfig::default();

    assert!(config.use_cache, "Default use_cache should be true");
    assert!(
        config.enable_quality_processing,
        "Default enable_quality_processing should be true"
    );
    assert!(!config.force_ocr, "Default force_ocr should be false");
    assert_eq!(
        config.max_concurrent_extractions, None,
        "Default max_concurrent_extractions should be None"
    );
}

#[test]
fn test_extraction_config_serialization_roundtrip() {
    let config = ExtractionConfig::default();

    let json_string = serde_json::to_string(&config).expect("Failed to serialize");

    let deserialized: ExtractionConfig =
        serde_json::from_str(&json_string).expect("Failed to deserialize config from JSON");

    assert_eq!(
        config.use_cache, deserialized.use_cache,
        "use_cache should survive roundtrip"
    );
    assert_eq!(
        config.enable_quality_processing, deserialized.enable_quality_processing,
        "enable_quality_processing should survive roundtrip"
    );
    assert_eq!(
        config.force_ocr, deserialized.force_ocr,
        "force_ocr should survive roundtrip"
    );
    assert_eq!(
        config.result_format, deserialized.result_format,
        "result_format should survive roundtrip"
    );
    assert_eq!(
        config.output_format, deserialized.output_format,
        "output_format should survive roundtrip"
    );
}

#[test]
fn test_extraction_config_json_structure() {
    let config = ExtractionConfig::default();
    let json = serde_json::to_value(&config).expect("Failed to serialize config");

    let obj = json.as_object().expect("Config should serialize as object");

    let expected_fields = vec![
        "use_cache",
        "enable_quality_processing",
        "force_ocr",
        "max_concurrent_extractions",
        "result_format",
        "output_format",
    ];

    for field in expected_fields {
        assert!(obj.contains_key(field), "Missing field in JSON: {}", field);
    }
}

#[test]
fn test_extraction_config_values_are_correct_types() {
    let config = ExtractionConfig::default();
    let json = serde_json::to_value(&config).expect("Failed to serialize config");

    assert!(
        json.get("use_cache").expect("Value not found").is_boolean(),
        "use_cache should be boolean"
    );
    assert!(
        json.get("enable_quality_processing")
            .expect("Value not found")
            .is_boolean(),
        "enable_quality_processing should be boolean"
    );
    assert!(
        json.get("force_ocr").expect("Value not found").is_boolean(),
        "force_ocr should be boolean"
    );
    assert!(
        json.get("result_format").expect("Value not found").is_string(),
        "result_format should be string"
    );
    assert!(
        json.get("output_format").expect("Value not found").is_string(),
        "output_format should be string"
    );
}

#[test]
fn test_extraction_config_with_custom_values() {
    let config = ExtractionConfig {
        use_cache: false,
        force_ocr: true,
        max_concurrent_extractions: Some(8),
        ..ExtractionConfig::default()
    };

    let json = serde_json::to_value(&config).expect("Failed to serialize");

    assert_eq!(json.get("use_cache").expect("Value not found"), &json!(false));
    assert_eq!(json.get("force_ocr").expect("Value not found"), &json!(true));
    assert_eq!(
        json.get("max_concurrent_extractions").expect("Value not found"),
        &json!(8)
    );
}

#[test]
fn test_extraction_config_partial_json_parsing() {
    let partial_json = json!({
        "use_cache": false,
    });

    let config: ExtractionConfig = serde_json::from_value(partial_json).expect("Failed to parse partial config");

    assert!(!config.use_cache, "Explicit use_cache should be respected");
    assert!(
        config.enable_quality_processing,
        "Omitted enable_quality_processing should use default"
    );
    assert!(!config.force_ocr, "Omitted force_ocr should use default");
}

#[test]
fn test_extraction_config_empty_json_uses_defaults() {
    let empty_json = json!({});

    let config: ExtractionConfig = serde_json::from_value(empty_json).expect("Failed to parse empty config");

    let default_config = ExtractionConfig::default();
    assert_eq!(config.use_cache, default_config.use_cache);
    assert_eq!(
        config.enable_quality_processing,
        default_config.enable_quality_processing
    );
    assert_eq!(config.force_ocr, default_config.force_ocr);
    assert_eq!(config.result_format, default_config.result_format);
    assert_eq!(config.output_format, default_config.output_format);
}

#[test]
fn test_extraction_config_output_format_valid_values() {
    let json_plain = json!({"output_format": "plain"});
    let config_plain: ExtractionConfig =
        serde_json::from_value(json_plain).expect("Failed to parse plain output_format");
    assert_eq!(config_plain.output_format, OutputFormat::Plain);

    let json_markdown = json!({"output_format": "markdown"});
    let config_markdown: ExtractionConfig =
        serde_json::from_value(json_markdown).expect("Failed to parse markdown output_format");
    assert_eq!(config_markdown.output_format, OutputFormat::Markdown);

    let json_html = json!({"output_format": "html"});
    let config_html: ExtractionConfig = serde_json::from_value(json_html).expect("Failed to parse html output_format");
    assert_eq!(config_html.output_format, OutputFormat::Html);
}

#[test]
fn test_extraction_config_result_format_valid_values() {
    let json_unified = json!({"result_format": "unified"});
    let config_unified: ExtractionConfig =
        serde_json::from_value(json_unified).expect("Failed to parse unified result_format");
    let _ = config_unified.result_format;
}

#[test]
fn test_extraction_config_no_unknown_fields_in_default() {
    let config = ExtractionConfig::default();
    let json = serde_json::to_value(&config).expect("Failed to serialize");
    let obj = json.as_object().expect("Should be object");

    let expected_fields = vec![
        "use_cache",
        "enable_quality_processing",
        "ocr",
        "force_ocr",
        "ocr_strategy",
        "disable_ocr",
        "chunking",
        "content_filter",
        "images",
        "pdf_options",
        "token_reduction",
        "language_detection",
        "pages",
        "keywords",
        "postprocessor",
        "html_options",
        "html_output",
        "max_concurrent_extractions",
        "result_format",
        "output_format",
        "escape_markdown",
        "table_anchors",
        "jupyter_cell_rendering",
        "apply_notebook_cell_tags",
        "include_document_structure",
        "security_limits",
        "acceleration",
        "cache_namespace",
        "cache_ttl_secs",
        "concurrency",
        "csv",
        "email",
        "geojson",
        "layout",
        "max_archive_depth",
        "max_embedded_file_bytes",
        "mime_detection_policy",
        "extraction_timeout_secs",
        "tree_sitter",
        "use_layout_for_markdown",
        "transcription",
        "url",
    ];

    for key in obj.keys() {
        assert!(
            expected_fields.contains(&key.as_str()),
            "Unexpected field in config: {}",
            key
        );
    }
}

#[test]
fn test_extraction_config_needs_image_processing() {
    let mut config = ExtractionConfig::default();

    assert!(
        !config.needs_image_processing(),
        "Default config should not need image processing"
    );

    config.ocr = Some(xberg::OcrConfig {
        backend: "tesseract".to_string(),
        language: vec!["eng".to_string()],
        ..Default::default()
    });
    assert!(
        config.needs_image_processing(),
        "Config with OCR should need image processing"
    );

    config.ocr = None;
    config.images = Some(xberg::ImageExtractionConfig {
        extract_images: true,
        target_dpi: 150,
        max_image_dimension: 2000,
        inject_placeholders: true,
        auto_adjust_dpi: true,
        min_dpi: 72,
        max_dpi: 600,
        max_images_per_page: None,
        classify: true,
        include_page_rasters: false,
        run_ocr_on_images: true,
        ocr_text_only: false,
        append_ocr_text: false,
        include_data_base64: false,
        output_format: xberg::core::config::extraction::ImageOutputFormat::Native,
        #[cfg(feature = "svg")]
        svg: xberg::core::config::extraction::SvgOptions::default(),
    });
    assert!(
        config.needs_image_processing(),
        "Config with image extraction should need image processing"
    );
}

#[test]
fn test_output_format_serialization_lowercase() {
    let json = serde_json::json!({"output_format": "markdown"});
    let config: ExtractionConfig = serde_json::from_value(json).expect("Failed to parse");
    let reserialized = serde_json::to_value(&config).expect("Failed to reserialize");

    assert_eq!(reserialized["output_format"], "markdown");
}

#[test]
fn test_extraction_config_field_presence_consistency() {
    let config = ExtractionConfig::default();
    let json1 = serde_json::to_value(&config).expect("Failed to serialize");

    let config2 = ExtractionConfig {
        force_ocr: true,
        ..ExtractionConfig::default()
    };
    let json2 = serde_json::to_value(&config2).expect("Failed to serialize");

    let keys1: Vec<_> = json1.as_object().expect("Expected object value").keys().collect();
    let keys2: Vec<_> = json2.as_object().expect("Expected object value").keys().collect();

    assert_eq!(keys1.len(), keys2.len(), "Configs should have same number of keys");
}

#[test]
fn test_output_format_all_variants() {
    let formats = vec![
        OutputFormat::Plain,
        OutputFormat::Markdown,
        OutputFormat::Html,
        OutputFormat::Djot,
    ];

    for fmt in &formats {
        let serialized = serde_json::to_value(fmt.clone()).expect("Failed to serialize");
        let deserialized: OutputFormat = serde_json::from_value(serialized).expect("Failed to deserialize");
        assert_eq!(*fmt, deserialized, "Format should survive roundtrip");
    }
}

#[test]
fn test_include_document_structure_default_is_false() {
    let config = ExtractionConfig::default();
    assert!(
        !config.include_document_structure,
        "Default include_document_structure should be false"
    );
}

#[test]
fn test_include_document_structure_serialization_roundtrip() {
    let config = ExtractionConfig {
        include_document_structure: true,
        ..ExtractionConfig::default()
    };

    let json_string = serde_json::to_string(&config).expect("Failed to serialize");

    let deserialized: ExtractionConfig =
        serde_json::from_str(&json_string).expect("Failed to deserialize config from JSON");

    assert_eq!(
        config.include_document_structure, deserialized.include_document_structure,
        "include_document_structure should survive roundtrip"
    );
    assert!(
        deserialized.include_document_structure,
        "Deserialized include_document_structure should be true"
    );

    let config_false = ExtractionConfig {
        include_document_structure: false,
        ..ExtractionConfig::default()
    };

    let json_string_false = serde_json::to_string(&config_false).expect("Failed to serialize");
    let deserialized_false: ExtractionConfig =
        serde_json::from_str(&json_string_false).expect("Failed to deserialize config from JSON");

    assert!(
        !deserialized_false.include_document_structure,
        "Explicitly false include_document_structure should survive roundtrip"
    );
}

#[cfg(feature = "tree-sitter")]
#[test]
fn test_tree_sitter_process_config_defaults() {
    let config = TreeSitterProcessConfig::default();
    assert!(config.structure, "Default structure should be true");
    assert!(config.imports, "Default imports should be true");
    assert!(config.exports, "Default exports should be true");
    assert!(!config.comments, "Default comments should be false");
    assert!(!config.docstrings, "Default docstrings should be false");
    assert!(!config.symbols, "Default symbols should be false");
    assert!(!config.diagnostics, "Default diagnostics should be false");
    assert!(config.chunk_max_size.is_none(), "Default chunk_max_size should be None");
}

#[cfg(feature = "tree-sitter")]
#[test]
fn test_tree_sitter_config_defaults() {
    let config = TreeSitterConfig::default();
    assert!(config.cache_dir.is_none(), "Default cache_dir should be None");
    assert!(config.languages.is_none(), "Default languages should be None");
    assert!(config.groups.is_none(), "Default groups should be None");
    assert!(config.process.structure, "Default process.structure should be true");
}

#[cfg(feature = "tree-sitter")]
#[test]
fn test_tree_sitter_config_serialization_roundtrip() {
    let config = TreeSitterConfig {
        enabled: true,
        cache_dir: Some("/tmp/grammars".into()),
        languages: Some(vec!["python".to_string(), "rust".to_string()]),
        groups: Some(vec!["web".to_string()]),
        process: TreeSitterProcessConfig {
            structure: true,
            imports: true,
            exports: false,
            comments: true,
            docstrings: true,
            symbols: false,
            diagnostics: false,
            data_extraction: false,
            chunk_max_size: Some(4000),
            content_mode: Default::default(),
        },
    };

    let json_string = serde_json::to_string(&config).expect("Failed to serialize");
    let deserialized: TreeSitterConfig = serde_json::from_str(&json_string).expect("Failed to deserialize");

    assert_eq!(config.cache_dir, deserialized.cache_dir);
    assert_eq!(config.languages, deserialized.languages);
    assert_eq!(config.groups, deserialized.groups);
    assert_eq!(config.process.structure, deserialized.process.structure);
    assert_eq!(config.process.exports, deserialized.process.exports);
    assert_eq!(config.process.comments, deserialized.process.comments);
    assert_eq!(config.process.docstrings, deserialized.process.docstrings);
    assert_eq!(config.process.chunk_max_size, deserialized.process.chunk_max_size);
}

#[cfg(feature = "tree-sitter")]
#[test]
fn test_tree_sitter_config_in_extraction_config_roundtrip() {
    let config = ExtractionConfig {
        tree_sitter: Some(TreeSitterConfig {
            languages: Some(vec!["python".to_string()]),
            ..TreeSitterConfig::default()
        }),
        ..ExtractionConfig::default()
    };

    let json_string = serde_json::to_string(&config).expect("Failed to serialize");
    let deserialized: ExtractionConfig = serde_json::from_str(&json_string).expect("Failed to deserialize");

    let ts = deserialized
        .tree_sitter
        .expect("tree_sitter should be present after roundtrip");
    assert_eq!(ts.languages, Some(vec!["python".to_string()]));
    assert!(ts.process.structure, "process.structure should default to true");
}

#[cfg(feature = "tree-sitter")]
#[test]
fn test_tree_sitter_partial_json_parsing() {
    let json = json!({
        "tree_sitter": {
            "languages": ["rust"],
            "process": {
                "comments": true
            }
        }
    });

    let config: ExtractionConfig = serde_json::from_value(json).expect("Failed to parse");
    let ts = config.tree_sitter.expect("tree_sitter should be present");
    assert_eq!(ts.languages, Some(vec!["rust".to_string()]));
    assert!(ts.groups.is_none(), "Omitted groups should be None");
    assert!(ts.process.comments, "Explicit comments=true should be respected");
    assert!(ts.process.structure, "Omitted structure should default to true");
    assert!(!ts.process.symbols, "Omitted symbols should default to false");
}

#[cfg(feature = "tree-sitter")]
#[test]
fn test_tree_sitter_process_config_all_fields_serialized() {
    let config = TreeSitterProcessConfig {
        structure: false,
        imports: false,
        exports: false,
        comments: true,
        docstrings: true,
        symbols: true,
        diagnostics: true,
        data_extraction: true,
        chunk_max_size: Some(2000),
        content_mode: Default::default(),
    };

    let json = serde_json::to_value(&config).expect("Failed to serialize");
    let obj = json.as_object().expect("Should be object");

    let expected_fields = [
        "structure",
        "imports",
        "exports",
        "comments",
        "docstrings",
        "symbols",
        "diagnostics",
        "data_extraction",
        "chunk_max_size",
    ];
    for field in expected_fields {
        assert!(obj.contains_key(field), "Missing field: {field}");
    }
}

#[cfg(feature = "tree-sitter")]
#[test]
fn test_format_metadata_code_variant_serialization() {
    use xberg::types::metadata::{CodeMetadata, FormatMetadata};

    let metadata = FormatMetadata::Code(CodeMetadata {
        chunks: Vec::new(),
        data: None,
    });
    let json = serde_json::to_value(&metadata).expect("Failed to serialize FormatMetadata::Code");

    assert_eq!(json["format_type"], "code", "format_type tag should be 'code'");
    assert!(json["chunks"].is_array(), "chunks should serialize as an array");
}

#[cfg(feature = "tree-sitter")]
#[test]
fn test_tslp_types_reexported() {
    let _: xberg::ProcessConfig = xberg::ProcessConfig::new("rust");

    let _kind = xberg::StructureKind::Function;
    let _kind = xberg::StructureKind::Class;
    let _kind = xberg::StructureKind::Method;

    let _kind = xberg::ExportKind::Named;
    let _kind = xberg::ExportKind::Default;

    let _kind = xberg::CommentKind::Line;
    let _kind = xberg::CommentKind::Block;

    let _sev = xberg::DiagnosticSeverity::Error;
    let _sev = xberg::DiagnosticSeverity::Warning;

    let metrics = xberg::FileMetrics::default();
    assert_eq!(metrics.total_lines, 0);
}