rs-chunks 0.6.0

Fast, high-fidelity document chunking for RAG — a pure-Rust engine covering 36 file formats (Office, OpenDocument, PDF, email, ebooks, notebooks, and more).
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
use std::io::Read;

use calamine::{Data, Reader};
use quick_xml::events::Event;
use quick_xml::name::QName;
use quick_xml::Reader as XmlReader;
use serde_json::json;
use zip::ZipArchive;

use super::common::{
    cell_to_string, detect_contiguous_regions, detect_header_row, parse_range_ref,
    row_is_empty_public, serialize_row_kv, serialize_row_values_public, XlsxChunkRecord, CT_TABLE,
};

#[derive(Debug, Clone)]
pub struct TableInfo {
    pub name: Option<String>,
    pub is_named_table: bool,
    /// Whether the source *declares* where the header row is.
    ///
    /// An OOXML table part does (`headerRowCount`), so its first row is the
    /// header. An ODF named range does not — it is a label over a rectangle and
    /// makes no claim about headers — so an ODS-named region keeps whatever the
    /// heuristic decided and gains only its name (TECH_DEBT #20).
    pub declares_header: bool,
    pub start_row: usize,
    pub end_row: usize,
    pub start_col: usize,
    pub end_col: usize,
    pub headers: Vec<String>,
}

fn read_zip_entry(archive: &mut ZipArchive<std::io::Cursor<Vec<u8>>>, name: &str) -> Result<Option<Vec<u8>>, String> {
    match archive.by_name(name) {
        Ok(mut entry) => {
            let mut buf = Vec::new();
            entry
                .read_to_end(&mut buf)
                .map_err(|e| format!("Failed to read '{name}': {e}"))?;
            Ok(Some(buf))
        }
        Err(zip::result::ZipError::FileNotFound) => Ok(None),
        Err(e) => Err(format!("Failed to open '{name}' in xlsx archive: {e}")),
    }
}

fn local_name(name: QName<'_>) -> Vec<u8> {
    let bytes = name.as_ref();
    let idx = bytes
        .iter()
        .rposition(|b| *b == b':')
        .map(|i| i + 1)
        .unwrap_or(0);
    bytes[idx..].to_vec()
}

fn attr_value(attr: &quick_xml::events::attributes::Attribute<'_>) -> String {
    String::from_utf8_lossy(attr.value.as_ref()).into_owned()
}

fn resolve_target(base_dir: &str, target: &str) -> String {
    if target.starts_with('/') {
        return target.trim_start_matches('/').to_string();
    }
    let mut parts: Vec<&str> = base_dir.split('/').collect();
    for segment in target.split('/') {
        match segment {
            ".." => {
                parts.pop();
            }
            "." | "" => {}
            s => parts.push(s),
        }
    }
    parts.join("/")
}

fn parse_table_relationship_targets(rels_xml: &[u8]) -> Result<Vec<String>, String> {
    let mut reader = XmlReader::from_reader(rels_xml);
    let mut buf = Vec::new();
    let mut targets = Vec::new();

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Eof) => break,
            Err(e) => return Err(format!("Failed to parse worksheet relationships XML: {e}")),
            Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e)) => {
                if local_name(e.name()).as_slice() == b"Relationship" {
                    let mut rel_type = String::new();
                    let mut target = String::new();
                    for attr in e.attributes().flatten() {
                        let key = local_name(QName(attr.key.as_ref()));
                        if key.as_slice() == b"Type" {
                            rel_type = attr_value(&attr);
                        } else if key.as_slice() == b"Target" {
                            target = attr_value(&attr);
                        }
                    }
                    if rel_type.ends_with("/table") && !target.is_empty() {
                        targets.push(target);
                    }
                }
            }
            _ => {}
        }
        buf.clear();
    }

    Ok(targets)
}

fn parse_table_definition(table_xml: &[u8]) -> Result<Option<TableInfo>, String> {
    let mut reader = XmlReader::from_reader(table_xml);
    let mut buf = Vec::new();

    let mut table_name: Option<String> = None;
    let mut display_name: Option<String> = None;
    let mut range_ref: Option<String> = None;
    let mut headers: Vec<String> = Vec::new();

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Eof) => break,
            Err(e) => return Err(format!("Failed to parse table XML: {e}")),
            Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e)) => {
                let lname = local_name(e.name());
                if lname.as_slice() == b"table" {
                    for attr in e.attributes().flatten() {
                        let key = local_name(QName(attr.key.as_ref()));
                        let value = attr_value(&attr);
                        if key.as_slice() == b"name" {
                            table_name = Some(value);
                        } else if key.as_slice() == b"displayName" {
                            display_name = Some(value);
                        } else if key.as_slice() == b"ref" {
                            range_ref = Some(value);
                        }
                    }
                } else if lname.as_slice() == b"tableColumn" {
                    for attr in e.attributes().flatten() {
                        let key = local_name(QName(attr.key.as_ref()));
                        if key.as_slice() == b"name" {
                            headers.push(attr_value(&attr));
                        }
                    }
                }
            }
            _ => {}
        }
        buf.clear();
    }

    let Some(range_ref) = range_ref else {
        return Ok(None);
    };
    let Some((start_row, start_col, end_row, end_col)) = parse_range_ref(&range_ref) else {
        return Ok(None);
    };

    Ok(Some(TableInfo {
        name: table_name.or(display_name),
        is_named_table: true,
        declares_header: true,
        start_row,
        end_row,
        start_col,
        end_col,
        headers,
    }))
}

fn get_row_values(row: &[Data], start_col: usize, end_col: usize) -> Vec<Data> {
    (start_col..=end_col)
        .map(|col| row.get(col).cloned().unwrap_or(Data::Empty))
        .collect()
}

fn serialize_table_row(values: &[Data], headers: &[String], include_headers: bool) -> String {
    if include_headers {
        serialize_row_kv(headers, values)
    } else {
        serialize_row_values_public(values, headers.len())
    }
}

fn split_lines_by_max_chars(
    lines: &[(usize, String)],
    max_chunk_chars: usize,
) -> Vec<Vec<(usize, String)>> {
    if lines.is_empty() {
        return Vec::new();
    }

    let mut parts: Vec<Vec<(usize, String)>> = Vec::new();
    let mut current: Vec<(usize, String)> = Vec::new();
    let mut current_len = 0usize;

    for (row_index, line) in lines {
        let line_len = line.len();
        let sep_len = if current.is_empty() { 0 } else { 1 };
        let proposed = current_len + sep_len + line_len;
        if !current.is_empty() && proposed > max_chunk_chars {
            parts.push(current);
            current = vec![(*row_index, line.clone())];
            current_len = line_len;
        } else {
            current.push((*row_index, line.clone()));
            current_len = proposed;
        }
    }

    if !current.is_empty() {
        parts.push(current);
    }
    parts
}

fn build_headers_from_cells(row_cells: &[Data], col_count: usize) -> Vec<String> {
    (0..col_count)
        .map(|idx| {
            let value = row_cells.get(idx).map(cell_to_string).unwrap_or_default();
            if value.trim().is_empty() {
                format!("Column {}", idx + 1)
            } else {
                value
            }
        })
        .collect()
}

fn read_named_tables_for_sheet(
    archive: &mut ZipArchive<std::io::Cursor<Vec<u8>>>,
    sheet_index_1based: usize,
) -> Result<Vec<TableInfo>, String> {
    // `.xlsx`/`.xlsm`/`.xltx`/`.xltm` name the part `sheetN.xml.rels`; `.xlsb`
    // names it `sheetN.bin.rels`. The referenced table parts are XML either way.
    //
    // This lookup is a second implementation of the one in
    // `common.rs::get_named_table_names_for_sheet`, and it had drifted: only
    // that one knew about `.bin.rels`, so a `.xlsb` workbook with real tables
    // would have `sheet` mode list them while `table` mode reported every
    // region unnamed (TECH_DEBT #20).
    let xml_rels = format!("xl/worksheets/_rels/sheet{}.xml.rels", sheet_index_1based);
    let bin_rels = format!("xl/worksheets/_rels/sheet{}.bin.rels", sheet_index_1based);
    let rels_xml = match read_zip_entry(archive, &xml_rels)? {
        Some(x) => x,
        None => match read_zip_entry(archive, &bin_rels)? {
            Some(x) => x,
            None => return Ok(Vec::new()),
        },
    };

    let targets = parse_table_relationship_targets(&rels_xml)?;
    let mut tables = Vec::new();
    for target in targets {
        let full_path = resolve_target("xl/worksheets", &target);
        let Some(table_xml) = read_zip_entry(archive, &full_path)? else {
            continue;
        };
        if let Some(info) = parse_table_definition(&table_xml)? {
            tables.push(info);
        }
    }
    Ok(tables)
}

/// ODF names that describe the *page*, not a table. `Print_Area` is written by
/// every spreadsheet that has a print range set, and calling it a named table
/// would label an arbitrary rectangle as the user's data.
const ODS_BUILTIN_NAMES: [&str; 4] = ["Print_Area", "Print_Range", "Repeat_Row", "Repeat_Column"];

/// Label each detected region with the named range that covers it, if any.
///
/// "Covers" rather than "equals": a range usually includes the header row that
/// region detection consumes, so requiring an exact match would never fire.
fn apply_ods_names(
    tables: &mut [TableInfo],
    archive: &mut Option<ZipArchive<std::io::Cursor<Vec<u8>>>>,
    sheet_name: &str,
) -> Result<(), String> {
    let Some(archive) = archive.as_mut() else {
        return Ok(());
    };
    let Some(content) = read_zip_entry(archive, "content.xml")? else {
        return Ok(());
    };
    let ranges: Vec<_> = super::common::get_ods_named_ranges_for_sheet(&content, sheet_name)
        .into_iter()
        .filter(|r| {
            !ODS_BUILTIN_NAMES.contains(&r.name.as_str()) && !r.name.starts_with('_')
        })
        .collect();
    if ranges.is_empty() {
        return Ok(());
    }
    for table in tables.iter_mut() {
        if let Some(range) = ranges.iter().find(|r| {
            r.start_row <= table.start_row
                && r.end_row >= table.end_row
                && r.start_col <= table.start_col
                && r.end_col >= table.end_col
        }) {
            table.name = Some(range.name.clone());
            table.is_named_table = true;
        }
    }
    Ok(())
}

fn build_heuristic_tables(rows: &[&[Data]], base_row: usize) -> Vec<TableInfo> {
    detect_contiguous_regions(rows, base_row)
        .into_iter()
        .map(|region| TableInfo {
            name: None,
            is_named_table: false,
            declares_header: false,
            start_row: region.start_row,
            end_row: region.end_row,
            start_col: region.start_col,
            end_col: region.end_col,
            headers: Vec::new(),
        })
        .collect()
}

pub fn build_table_chunks(
    data: &[u8],
    ext: &str,
    include_headers: bool,
    sheet_names: Vec<String>,
    skip_empty_rows: bool,
    max_chunk_chars: usize,
) -> Result<Vec<XlsxChunkRecord>, String> {
    if max_chunk_chars == 0 {
        return Err("max_chunk_chars must be > 0".to_string());
    }

    let mut workbook =
        super::common::open_spreadsheet_from_bytes(data, ext)?;

    let workbook_sheet_names = workbook.sheet_names().to_vec();
    let selected_sheets = if sheet_names.is_empty() {
        workbook_sheet_names.clone()
    } else {
        for sheet_name in &sheet_names {
            if !workbook_sheet_names.iter().any(|name| name == sheet_name) {
                return Err(format!("Sheet '{sheet_name}' not found"));
            }
        }
        sheet_names
    };

    // Try to open as a ZIP for named-table discovery. For XLS (BIFF binary), this
    // silently yields None and we fall back to heuristic region detection per sheet.
    let mut maybe_archive: Option<ZipArchive<std::io::Cursor<Vec<u8>>>> =
        ZipArchive::new(std::io::Cursor::new(data.to_vec())).ok();

    let mut chunks = Vec::new();
    let mut readable_sheets = 0usize;
    let mut first_sheet_error: Option<String> = None;
    let mut skipped_sheets: Vec<String> = Vec::new();
    for sheet_name in selected_sheets {
        let sheet_index = workbook_sheet_names
            .iter()
            .position(|name| name == &sheet_name)
            .unwrap_or(0);

        // A sheet calamine cannot read (chart sheets, XLM macro sheets) must not
        // take the whole workbook down with it — skip it and keep going.
        let range = match super::common::read_worksheet_range(&mut workbook, &sheet_name) {
            Ok(range) => {
                readable_sheets += 1;
                range
            }
            Err(e) => {
                first_sheet_error.get_or_insert(e);
                skipped_sheets.push(sheet_name.clone());
                continue;
            }
        };
        let base_row_index = range.start().map(|(row, _)| row as usize).unwrap_or(0);
        let rows: Vec<&[Data]> = range.rows().collect();
        if rows.is_empty() {
            continue;
        }

        let mut tables = if ext == "ods" {
            Vec::new()
        } else if let Some(ref mut archive) = maybe_archive {
            read_named_tables_for_sheet(archive, sheet_index + 1)?
        } else {
            Vec::new()
        };
        if tables.is_empty() {
            tables = build_heuristic_tables(&rows, base_row_index);
        }
        // `read_named_tables_for_sheet` reads OOXML *table parts*, which ODS
        // does not have — its named ranges live in `content.xml`. So `table`
        // mode reported every ODS region unnamed while `sheet` mode listed the
        // names correctly: the same drift #20 closed for `.xlsb`, still open
        // for `.ods`. It stayed invisible until a fixture existed whose named
        // range spans a data region rather than a single cell.
        //
        // ODS names *annotate* the detected regions rather than replace them.
        // An OOXML table part declares what the tables are; an ODF named range
        // is an arbitrary label over arbitrary cells, so emitting one chunk per
        // range would drop every cell outside a range from `table` mode.
        if ext == "ods" {
            apply_ods_names(&mut tables, &mut maybe_archive, &sheet_name)?;
        }

        let mut chunk_index = 0usize;
        for mut table in tables {
            if table.start_col > table.end_col || table.start_row > table.end_row {
                continue;
            }

            let col_count = table.end_col - table.start_col + 1;

            let mut header_row_abs = table.start_row;
            if table.declares_header {
                if table.headers.is_empty() {
                    let header_local = table.start_row.saturating_sub(base_row_index);
                    if let Some(header_row) = rows.get(header_local) {
                        let header_values =
                            get_row_values(header_row, table.start_col, table.end_col);
                        table.headers = build_headers_from_cells(&header_values, col_count);
                    } else {
                        table.headers = (0..col_count)
                            .map(|i| format!("Column {}", i + 1))
                            .collect();
                    }
                }
            } else {
                let local_start = table.start_row.saturating_sub(base_row_index);
                let local_end = table.end_row.saturating_sub(base_row_index);
                if local_start >= rows.len() || local_end >= rows.len() || local_start > local_end {
                    continue;
                }

                let region_rows_owned: Vec<Vec<Data>> = (local_start..=local_end)
                    .map(|idx| get_row_values(rows[idx], table.start_col, table.end_col))
                    .collect();
                let region_row_refs: Vec<&[Data]> =
                    region_rows_owned.iter().map(|r| r.as_slice()).collect();
                let header_rel = detect_header_row(&region_row_refs);
                if let Some(rel) = header_rel {
                    header_row_abs = table.start_row + rel;
                }

                let header_cells = header_rel
                    .and_then(|rel| region_rows_owned.get(rel))
                    .cloned()
                    .unwrap_or_else(|| vec![Data::Empty; col_count]);
                table.headers = build_headers_from_cells(&header_cells, col_count);
            }

            if table.headers.len() < col_count {
                table
                    .headers
                    .extend((table.headers.len()..col_count).map(|i| format!("Column {}", i + 1)));
            }
            if table.headers.len() > col_count {
                table.headers.truncate(col_count);
            }

            let data_start_row = if table.declares_header {
                table.start_row + 1
            } else {
                header_row_abs + 1
            };

            let mut row_lines: Vec<(usize, String)> = Vec::new();
            // `data_start_row > end_row` means the header consumed the region;
            // fall through to the fallback below rather than dropping it here.
            if data_start_row <= table.end_row {
                for abs_row in data_start_row..=table.end_row {
                    let local_row = abs_row.saturating_sub(base_row_index);
                    let Some(row) = rows.get(local_row) else {
                        continue;
                    };
                    let values = get_row_values(row, table.start_col, table.end_col);
                    if skip_empty_rows && row_is_empty_public(&values) {
                        continue;
                    }
                    let line = serialize_table_row(&values, &table.headers, include_headers);
                    row_lines.push((abs_row, line));
                }
            }

            // Header fallback, same rule the row-consuming modes apply via
            // `data_start_with_header_fallback`: a region whose only content was
            // taken as its header still holds that content (TECH_DEBT #80).
            if row_lines.is_empty() {
                let local_header = header_row_abs.saturating_sub(base_row_index);
                if let Some(header_row) = rows.get(local_header) {
                    let values = get_row_values(header_row, table.start_col, table.end_col);
                    if !row_is_empty_public(&values) {
                        let line = serialize_table_row(&values, &table.headers, include_headers);
                        row_lines.push((header_row_abs, line));
                    }
                }
            }

            if row_lines.is_empty() {
                continue;
            }

            let split_parts = split_lines_by_max_chars(&row_lines, max_chunk_chars);
            let is_split = split_parts.len() > 1;
            for (split_part, part_rows) in split_parts.into_iter().enumerate() {
                if part_rows.is_empty() {
                    continue;
                }
                let content = part_rows
                    .iter()
                    .map(|(_, line)| line.as_str())
                    .collect::<Vec<_>>()
                    .join("\n");
                let row_count = part_rows.len();
                let part_start_row = part_rows
                    .first()
                    .map(|(r, _)| *r)
                    .unwrap_or(table.start_row);
                let part_end_row = part_rows.last().map(|(r, _)| *r).unwrap_or(table.end_row);

                chunks.push(XlsxChunkRecord {
                    content,
                    content_type: CT_TABLE.to_string(),
                    metadata: json!({
                        "sheet_name": sheet_name,
                        "sheet_index": sheet_index,
                        "table_name": table.name,
                        "is_named_table": table.is_named_table,
                        "header_row": table.headers,
                        "start_row": part_start_row,
                        "end_row": part_end_row,
                        "start_col": table.start_col,
                        "end_col": table.end_col,
                        "row_count": row_count,
                        "col_count": col_count,
                        "chunk_index": chunk_index,
                        "is_split": is_split,
                        "split_part": if is_split { Some(split_part) } else { None::<usize> },
                    }),
                });
                chunk_index += 1;
            }
        }
    }
    // Every selected sheet failed to read: this is not an empty workbook,
    // it is an unreadable one — surface the first failure rather than
    // returning success with no chunks.
    if readable_sheets == 0 {
        if let Some(e) = first_sheet_error {
            return Err(e);
        }
    }

    super::common::stamp_skipped_sheets(&mut chunks, &skipped_sheets);
    Ok(chunks)
}