xberg 1.3.10

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
//! Page boundary handling and page range calculation for chunked text.
//!
//! This module provides functions to track which pages text chunks span,
//! enabling accurate page-level metadata for document processing.

use crate::error::{Result, XbergError};
use crate::types::{PageBoundary, PageSpan};

/// Validates the consistency and correctness of page boundaries.
///
/// # Validation Rules
///
/// 1. Boundaries must be sorted by byte_start (monotonically increasing)
/// 2. Boundaries must not overlap (byte_end[i] <= byte_start[i+1])
/// 3. Each boundary must have byte_start < byte_end
///
/// # Arguments
///
/// * `boundaries` - Page boundary markers to validate
///
/// # Returns
///
/// Returns `Ok(())` if all boundaries are valid.
/// Returns `XbergError::Validation` if any boundary is invalid.
pub(crate) fn validate_page_boundaries(boundaries: &[PageBoundary]) -> Result<()> {
    if boundaries.is_empty() {
        return Ok(());
    }

    for (idx, boundary) in boundaries.iter().enumerate() {
        if boundary.byte_start > boundary.byte_end {
            return Err(XbergError::validation(format!(
                "Invalid boundary range at index {}: byte_start ({}) must be <= byte_end ({})",
                idx, boundary.byte_start, boundary.byte_end
            )));
        }
    }

    for i in 0..boundaries.len() - 1 {
        let current = &boundaries[i];
        let next = &boundaries[i + 1];

        if current.byte_start > next.byte_start {
            return Err(XbergError::validation(format!(
                "Page boundaries not sorted: boundary at index {} (byte_start={}) comes after boundary at index {} (byte_start={})",
                i,
                current.byte_start,
                i + 1,
                next.byte_start
            )));
        }

        if current.byte_end > next.byte_start {
            return Err(XbergError::validation(format!(
                "Overlapping page boundaries: boundary {} ends at {} but boundary {} starts at {}",
                i,
                current.byte_end,
                i + 1,
                next.byte_start
            )));
        }
    }

    Ok(())
}

/// Validated page boundaries with an indexed overlap lookup.
///
/// Boundaries are validated once, at construction, instead of on every query. Validation
/// guarantees both `byte_start` and `byte_end` are non-decreasing, so the boundaries
/// overlapping a byte range are a contiguous run that a binary search locates in `O(log pages)`.
/// Resolving every chunk of a large paginated document is therefore `O(chunks * log pages)`
/// rather than `O(chunks * pages)`.
pub(crate) struct PageIndex<'a> {
    boundaries: &'a [PageBoundary],
}

impl<'a> PageIndex<'a> {
    /// Validates `boundaries` once.
    ///
    /// # Errors
    ///
    /// Returns `XbergError::Validation` if the boundaries are unsorted, overlapping or inverted.
    pub(crate) fn new(boundaries: &'a [PageBoundary]) -> Result<Self> {
        validate_page_boundaries(boundaries)?;
        Ok(Self { boundaries })
    }

    /// The boundaries overlapping `[byte_start, byte_end)`, in page order. A boundary overlaps
    /// when `byte_start < boundary.byte_end && byte_end > boundary.byte_start`.
    fn overlapping(&self, byte_start: usize, byte_end: usize) -> impl Iterator<Item = &'a PageBoundary> {
        let boundaries = self.boundaries;
        let first = boundaries.partition_point(|b| b.byte_end <= byte_start);
        boundaries[first..].iter().take_while(move |b| b.byte_start < byte_end)
    }

    /// `(first_page, last_page)` of the pages a byte range spans, 1-indexed.
    ///
    /// `(None, None)` when there are no boundaries or the range overlaps no page.
    pub(crate) fn range(&self, byte_start: usize, byte_end: usize) -> (Option<u32>, Option<u32>) {
        let mut overlapping = self.overlapping(byte_start, byte_end);
        let first = overlapping.next().map(|b| b.page_number);
        let last = overlapping.last().map(|b| b.page_number).or(first);
        (first, last)
    }

    /// The ordered [`PageSpan`]s of the pages a byte range spans, each with `bbox: None`.
    ///
    /// Uses the same overlap test as [`Self::range`], so the first and last spans' pages equal
    /// `first_page`/`last_page`. Bounding boxes are filled in by a later pass (see
    /// `chunking::page_spans::populate_page_span_bboxes`) once the document's structured node
    /// tree is available.
    pub(crate) fn spans(&self, byte_start: usize, byte_end: usize) -> Vec<PageSpan> {
        self.overlapping(byte_start, byte_end)
            .map(|b| PageSpan {
                page: b.page_number,
                bbox: None,
            })
            .collect()
    }

    /// Byte offset where the page containing `offset` begins.
    ///
    /// Falls back to the closest preceding boundary's start when `offset` falls strictly
    /// within no `[byte_start, byte_end)` range (a zero-width boundary for a blank page, a gap
    /// between pages, or an offset at content end). `None` if no boundary starts at or before
    /// `offset`.
    pub(crate) fn page_start(&self, offset: usize) -> Option<usize> {
        let boundaries = self.boundaries;
        let containing = boundaries.partition_point(|b| b.byte_end <= offset);
        if let Some(b) = boundaries.get(containing)
            && b.byte_start <= offset
        {
            return Some(b.byte_start);
        }
        let preceding = boundaries.partition_point(|b| b.byte_start <= offset);
        preceding.checked_sub(1).map(|i| boundaries[i].byte_start)
    }
}

/// Calculate which pages a byte range spans.
///
/// Returns `(first_page, last_page)`, 1-indexed, or `(None, None)` if `boundaries` is empty or
/// the range overlaps no page. Callers resolving many ranges against one boundary set build a
/// [`PageIndex`] once instead.
///
/// # Errors
///
/// Returns `XbergError::Validation` if boundaries are invalid.
#[cfg(test)]
pub(crate) fn calculate_page_range(
    byte_start: usize,
    byte_end: usize,
    boundaries: &[PageBoundary],
) -> Result<(Option<u32>, Option<u32>)> {
    Ok(PageIndex::new(boundaries)?.range(byte_start, byte_end))
}

/// Calculate the ordered list of pages a byte range spans, as [`PageSpan`]s.
///
/// # Errors
///
/// Returns `XbergError::Validation` if boundaries are invalid.
#[cfg(test)]
pub(crate) fn calculate_page_spans(
    byte_start: usize,
    byte_end: usize,
    boundaries: &[PageBoundary],
) -> Result<Vec<PageSpan>> {
    Ok(PageIndex::new(boundaries)?.spans(byte_start, byte_end))
}

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

    #[test]
    fn test_validate_page_boundaries_valid() {
        let boundaries = vec![
            PageBoundary {
                byte_start: 0,
                byte_end: 20,
                page_number: 1,
            },
            PageBoundary {
                byte_start: 20,
                byte_end: 40,
                page_number: 2,
            },
            PageBoundary {
                byte_start: 40,
                byte_end: 60,
                page_number: 3,
            },
        ];

        let result = validate_page_boundaries(&boundaries);
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_page_boundaries_empty() {
        let boundaries: Vec<PageBoundary> = vec![];
        let result = validate_page_boundaries(&boundaries);
        assert!(result.is_ok());
    }

    #[test]
    fn test_calculate_page_range_within_page() {
        let boundaries = vec![
            PageBoundary {
                byte_start: 0,
                byte_end: 100,
                page_number: 1,
            },
            PageBoundary {
                byte_start: 100,
                byte_end: 200,
                page_number: 2,
            },
        ];

        let (first, last) = calculate_page_range(10, 50, &boundaries).unwrap();
        assert_eq!(first, Some(1));
        assert_eq!(last, Some(1));
    }

    #[test]
    fn test_calculate_page_range_spanning_pages() {
        let boundaries = vec![
            PageBoundary {
                byte_start: 0,
                byte_end: 100,
                page_number: 1,
            },
            PageBoundary {
                byte_start: 100,
                byte_end: 200,
                page_number: 2,
            },
        ];

        let (first, last) = calculate_page_range(50, 150, &boundaries).unwrap();
        assert_eq!(first, Some(1));
        assert_eq!(last, Some(2));
    }

    #[test]
    fn test_calculate_page_range_empty_boundaries() {
        let boundaries: Vec<PageBoundary> = vec![];

        let (first, last) = calculate_page_range(0, 50, &boundaries).unwrap();
        assert_eq!(first, None);
        assert_eq!(last, None);
    }

    #[test]
    fn test_calculate_page_range_no_overlap() {
        let boundaries = vec![
            PageBoundary {
                byte_start: 0,
                byte_end: 100,
                page_number: 1,
            },
            PageBoundary {
                byte_start: 100,
                byte_end: 200,
                page_number: 2,
            },
        ];

        let (first, last) = calculate_page_range(200, 250, &boundaries).unwrap();
        assert_eq!(first, None);
        assert_eq!(last, None);
    }

    #[test]
    fn test_calculate_page_range_three_pages() {
        let boundaries = vec![
            PageBoundary {
                byte_start: 0,
                byte_end: 100,
                page_number: 1,
            },
            PageBoundary {
                byte_start: 100,
                byte_end: 200,
                page_number: 2,
            },
            PageBoundary {
                byte_start: 200,
                byte_end: 300,
                page_number: 3,
            },
        ];

        let (first, last) = calculate_page_range(50, 250, &boundaries).unwrap();
        assert_eq!(first, Some(1));
        assert_eq!(last, Some(3));
    }

    #[test]
    fn test_calculate_page_range_with_invalid_boundaries() {
        let boundaries = vec![PageBoundary {
            byte_start: 15,
            byte_end: 10,
            page_number: 1,
        }];

        let result = calculate_page_range(0, 20, &boundaries);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("Invalid boundary range"));
    }

    #[test]
    fn test_page_boundaries_with_gaps() {
        let boundaries = vec![
            PageBoundary {
                byte_start: 0,
                byte_end: 10,
                page_number: 1,
            },
            PageBoundary {
                byte_start: 15,
                byte_end: 25,
                page_number: 2,
            },
        ];

        let result = validate_page_boundaries(&boundaries);
        assert!(result.is_ok());
    }

    #[test]
    fn test_chunk_with_same_start_and_end() {
        let boundaries = vec![PageBoundary {
            byte_start: 10,
            byte_end: 10,
            page_number: 1,
        }];

        let result = validate_page_boundaries(&boundaries);
        assert!(result.is_ok());
    }

    #[test]
    fn test_zero_length_boundary_is_valid() {
        let boundaries = vec![
            PageBoundary {
                byte_start: 0,
                byte_end: 100,
                page_number: 1,
            },
            PageBoundary {
                byte_start: 100,
                byte_end: 100,
                page_number: 2,
            },
            PageBoundary {
                byte_start: 100,
                byte_end: 200,
                page_number: 3,
            },
        ];
        assert!(validate_page_boundaries(&boundaries).is_ok());
    }

    #[test]
    fn should_return_single_page_span_when_chunk_is_within_one_page() {
        let boundaries = vec![
            PageBoundary {
                byte_start: 0,
                byte_end: 100,
                page_number: 1,
            },
            PageBoundary {
                byte_start: 100,
                byte_end: 200,
                page_number: 2,
            },
        ];

        let spans = calculate_page_spans(10, 50, &boundaries).unwrap();
        assert_eq!(spans.len(), 1, "single-page chunk must produce exactly one span");
        assert_eq!(spans[0].page, 1);
        assert_eq!(
            spans[0].bbox, None,
            "bbox is filled in a later pass, not by calculate_page_spans"
        );
    }

    #[test]
    fn should_return_one_span_per_page_when_chunk_spans_multiple_pages() {
        let boundaries = vec![
            PageBoundary {
                byte_start: 0,
                byte_end: 100,
                page_number: 3,
            },
            PageBoundary {
                byte_start: 100,
                byte_end: 200,
                page_number: 4,
            },
            PageBoundary {
                byte_start: 200,
                byte_end: 300,
                page_number: 5,
            },
        ];

        let spans = calculate_page_spans(50, 250, &boundaries).unwrap();
        let pages: Vec<u32> = spans.iter().map(|s| s.page).collect();
        assert_eq!(
            pages,
            vec![3, 4, 5],
            "chunk spanning pages 3-5 must produce one span per page in order"
        );
        assert!(spans.iter().all(|s| s.bbox.is_none()));
    }

    #[test]
    fn should_return_empty_spans_when_boundaries_are_empty() {
        let spans = calculate_page_spans(0, 50, &[]).unwrap();
        assert!(spans.is_empty());
    }

    #[test]
    fn should_return_empty_spans_when_chunk_does_not_overlap_any_page() {
        let boundaries = vec![PageBoundary {
            byte_start: 0,
            byte_end: 100,
            page_number: 1,
        }];

        let spans = calculate_page_spans(200, 250, &boundaries).unwrap();
        assert!(spans.is_empty());
    }

    #[test]
    fn should_match_calculate_page_range_first_and_last_page() {
        let boundaries = vec![
            PageBoundary {
                byte_start: 0,
                byte_end: 100,
                page_number: 1,
            },
            PageBoundary {
                byte_start: 100,
                byte_end: 200,
                page_number: 2,
            },
            PageBoundary {
                byte_start: 200,
                byte_end: 300,
                page_number: 3,
            },
        ];

        let (first_page, last_page) = calculate_page_range(50, 250, &boundaries).unwrap();
        let spans = calculate_page_spans(50, 250, &boundaries).unwrap();

        assert_eq!(spans.first().map(|s| s.page), first_page);
        assert_eq!(spans.last().map(|s| s.page), last_page);
    }

    /// The pre-index algorithms: validate and scan every boundary for every query.
    fn legacy_range(start: usize, end: usize, boundaries: &[PageBoundary]) -> (Option<u32>, Option<u32>, Vec<u32>) {
        let hit: Vec<u32> = boundaries
            .iter()
            .filter(|b| start < b.byte_end && end > b.byte_start)
            .map(|b| b.page_number)
            .collect();
        (hit.first().copied(), hit.last().copied(), hit)
    }

    fn legacy_page_start(offset: usize, boundaries: &[PageBoundary]) -> Option<usize> {
        boundaries
            .iter()
            .find(|b| offset >= b.byte_start && offset < b.byte_end)
            .or_else(|| boundaries.iter().rev().find(|b| b.byte_start <= offset))
            .map(|b| b.byte_start)
    }

    /// Deterministic boundary sets with gaps, zero-width pages and adjacent pages.
    fn boundary_sets() -> Vec<Vec<PageBoundary>> {
        let mut state = 0x2085_u64;
        let mut next = move |bound: usize| {
            state = state
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            ((state >> 33) as usize) % bound
        };
        (0..40)
            .map(|_| {
                let mut cursor = next(5);
                (1..=next(30) as u32 + 1)
                    .map(|page_number| {
                        cursor += next(4);
                        let byte_start = cursor;
                        cursor += next(9);
                        PageBoundary {
                            byte_start,
                            byte_end: cursor,
                            page_number,
                        }
                    })
                    .collect()
            })
            .collect()
    }

    #[test]
    fn page_index_matches_linear_scan_for_every_range() {
        for boundaries in boundary_sets() {
            let index = PageIndex::new(&boundaries).unwrap();
            let limit = boundaries.last().map_or(0, |b| b.byte_end) + 3;
            for start in 0..=limit {
                assert_eq!(
                    index.page_start(start),
                    legacy_page_start(start, &boundaries),
                    "page_start({start}) over {boundaries:?}"
                );
                for end in start..=limit {
                    let (first, last, pages) = legacy_range(start, end, &boundaries);
                    assert_eq!(
                        index.range(start, end),
                        (first, last),
                        "range({start},{end}) over {boundaries:?}"
                    );
                    let spans: Vec<u32> = index.spans(start, end).iter().map(|s| s.page).collect();
                    assert_eq!(spans, pages, "spans({start},{end}) over {boundaries:?}");
                }
            }
        }
    }

    #[test]
    fn page_index_rejects_invalid_boundaries_at_construction() {
        let unsorted = [
            PageBoundary {
                byte_start: 10,
                byte_end: 20,
                page_number: 1,
            },
            PageBoundary {
                byte_start: 0,
                byte_end: 5,
                page_number: 2,
            },
        ];
        assert!(PageIndex::new(&unsorted).is_err());
    }
}