litchi 0.0.1

High-performance parser for Microsoft Office, OpenDocument, and Apple iWork file formats with unified API
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
//! Table-related ODF elements.
//!
//! This module provides classes for table elements like tables, rows, cells,
//! and other table-related content.

use super::element::{Element, ElementBase};
use crate::common::{Error, Result};
use crate::odf::spreadsheet::CellValue;

/// A table element
#[derive(Debug, Clone)]
pub struct Table {
    element: Element,
}

impl Default for Table {
    fn default() -> Self {
        Self::new()
    }
}

impl Table {
    /// Create a new table
    pub fn new() -> Self {
        Self {
            element: Element::new("table:table"),
        }
    }

    /// Create table from element
    pub fn from_element(element: Element) -> Result<Self> {
        if element.tag_name() != "table:table" {
            return Err(Error::InvalidFormat("Element is not a table".to_string()));
        }
        Ok(Self { element })
    }

    /// Get the table name
    pub fn name(&self) -> Option<&str> {
        self.element.get_attribute("table:name")
    }

    /// Set the table name
    pub fn set_name(&mut self, name: &str) {
        self.element.set_attribute("table:name", name);
    }

    /// Get the style name
    pub fn style_name(&self) -> Option<&str> {
        self.element.get_attribute("table:style-name")
    }

    /// Set the style name
    pub fn set_style_name(&mut self, name: &str) {
        self.element.set_attribute("table:style-name", name);
    }

    /// Get all rows in the table
    pub fn rows(&self) -> Result<Vec<TableRow>> {
        let mut rows = Vec::new();
        for child in self.element.children() {
            if child.tag_name() == "table:table-row"
                && let Ok(row) = TableRow::from_element(unsafe { &*(child as *const _ as *const Element) }.clone()) {
                    rows.push(row);
                }
        }
        Ok(rows)
    }

    /// Get the number of rows
    pub fn row_count(&self) -> Result<usize> {
        Ok(self.rows()?.len())
    }

    /// Get a row by index
    pub fn row(&self, index: usize) -> Result<Option<TableRow>> {
        let rows = self.rows()?;
        Ok(rows.into_iter().nth(index))
    }

    /// Add a row to the table
    pub fn add_row(&mut self, row: TableRow) {
        self.element.add_child(Box::new(row.element));
    }

    /// Get the number of columns (based on the widest row)
    pub fn column_count(&self) -> Result<usize> {
        let rows = self.rows()?;
        let max_cols = rows.iter()
            .map(|row| row.cells().map(|cells| cells.len()).unwrap_or(0))
            .max()
            .unwrap_or(0);
        Ok(max_cols)
    }
}

impl From<Table> for Element {
    fn from(table: Table) -> Element {
        table.element
    }
}

/// A table row element
#[derive(Debug, Clone)]
pub struct TableRow {
    element: Element,
}

impl Default for TableRow {
    fn default() -> Self {
        Self::new()
    }
}

impl TableRow {
    /// Create a new table row
    pub fn new() -> Self {
        Self {
            element: Element::new("table:table-row"),
        }
    }

    /// Create table row from element
    pub fn from_element(element: Element) -> Result<Self> {
        if element.tag_name() != "table:table-row" {
            return Err(Error::InvalidFormat("Element is not a table row".to_string()));
        }
        Ok(Self { element })
    }

    /// Get all cells in the row
    pub fn cells(&self) -> Result<Vec<TableCell>> {
        let mut cells = Vec::new();
        for child in self.element.children() {
            if child.tag_name() == "table:table-cell"
                && let Ok(cell) = TableCell::from_element(unsafe { &*(child as *const _ as *const Element) }.clone()) {
                    cells.push(cell);
                }
        }
        Ok(cells)
    }

    /// Get a cell by column index
    pub fn cell(&self, index: usize) -> Result<Option<TableCell>> {
        let cells = self.cells()?;
        Ok(cells.into_iter().nth(index))
    }

    /// Add a cell to the row
    pub fn add_cell(&mut self, cell: TableCell) {
        self.element.add_child(Box::new(cell.element));
    }

    /// Get the style name
    pub fn style_name(&self) -> Option<&str> {
        self.element.get_attribute("table:style-name")
    }

    /// Set the style name
    pub fn set_style_name(&mut self, name: &str) {
        self.element.set_attribute("table:style-name", name);
    }
}

impl From<TableRow> for Element {
    fn from(row: TableRow) -> Element {
        row.element
    }
}

/// A table cell element
#[derive(Debug, Clone)]
pub struct TableCell {
    element: Element,
}

impl Default for TableCell {
    fn default() -> Self {
        Self::new()
    }
}

impl TableCell {
    /// Create a new table cell
    pub fn new() -> Self {
        Self {
            element: Element::new("table:table-cell"),
        }
    }

    /// Create table cell from element
    pub fn from_element(element: Element) -> Result<Self> {
        if element.tag_name() != "table:table-cell" {
            return Err(Error::InvalidFormat("Element is not a table cell".to_string()));
        }
        Ok(Self { element })
    }

    /// Get the text content of the cell
    pub fn text(&self) -> Result<String> {
        Ok(self.element.get_text_recursive().trim().to_string())
    }

    /// Set the text content of the cell
    pub fn set_text(&mut self, text: &str) {
        self.element.set_text(text);
    }

    /// Get the cell value (parsed from attributes and content)
    pub fn value(&self) -> Result<CellValue> {
        // Check for value type
        let value_type = self.element.get_attribute("office:value-type");

        match value_type {
            Some("float") | Some("double") | Some("decimal") => {
                if let Some(val_str) = self.element.get_attribute("office:value")
                    && let Ok(num) = val_str.parse::<f64>() {
                        return Ok(CellValue::Number(num));
                    }
            }
            Some("currency") => {
                if let Some(val_str) = self.element.get_attribute("office:value")
                    && let Ok(num) = val_str.parse::<f64>() {
                        let currency = self.element.get_attribute("office:currency")
                            .unwrap_or("USD");
                        return Ok(CellValue::Currency(num, currency.to_string()));
                    }
            }
            Some("percentage") => {
                if let Some(val_str) = self.element.get_attribute("office:value")
                    && let Ok(num) = val_str.parse::<f64>() {
                        return Ok(CellValue::Percentage(num));
                    }
            }
            Some("boolean") => {
                if let Some(val_str) = self.element.get_attribute("office:value") {
                    match val_str {
                        "true" => return Ok(CellValue::Boolean(true)),
                        "false" => return Ok(CellValue::Boolean(false)),
                        _ => {}
                    }
                }
            }
            Some("date") => {
                if let Some(val_str) = self.element.get_attribute("office:value") {
                    return Ok(CellValue::Date(val_str.to_string()));
                }
            }
            Some("time") => {
                if let Some(val_str) = self.element.get_attribute("office:value") {
                    return Ok(CellValue::Time(val_str.to_string()));
                }
            }
            _ => {
                let text = self.text()?;
                if text.trim().is_empty() {
                    return Ok(CellValue::Empty);
                } else {
                    return Ok(CellValue::Text(text));
                }
            }
        }

        // Fallback to text parsing
        let text = self.text()?;
        if text.trim().is_empty() {
            Ok(CellValue::Empty)
        } else {
            Ok(CellValue::Text(text))
        }
    }

    /// Get the formula in the cell
    pub fn formula(&self) -> Option<&str> {
        self.element.get_attribute("table:formula")
    }

    /// Set the formula in the cell
    pub fn set_formula(&mut self, formula: &str) {
        self.element.set_attribute("table:formula", formula);
    }

    /// Get the style name
    pub fn style_name(&self) -> Option<&str> {
        self.element.get_attribute("table:style-name")
    }

    /// Set the style name
    pub fn set_style_name(&mut self, name: &str) {
        self.element.set_attribute("table:style-name", name);
    }

    /// Get the number of columns this cell spans
    pub fn colspan(&self) -> usize {
        self.element.get_int_attribute("table:number-columns-spanned")
            .map(|n| n as usize)
            .unwrap_or(1)
    }

    /// Set the number of columns this cell spans
    pub fn set_colspan(&mut self, span: usize) {
        self.element.set_attribute("table:number-columns-spanned", &span.to_string());
    }

    /// Get the number of rows this cell spans
    pub fn rowspan(&self) -> usize {
        self.element.get_int_attribute("table:number-rows-spanned")
            .map(|n| n as usize)
            .unwrap_or(1)
    }

    /// Set the number of rows this cell spans
    pub fn set_rowspan(&mut self, span: usize) {
        self.element.set_attribute("table:number-rows-spanned", &span.to_string());
    }

    /// Check if the cell is empty
    pub fn is_empty(&self) -> bool {
        matches!(self.value(), Ok(CellValue::Empty))
    }
}

impl From<TableCell> for Element {
    fn from(cell: TableCell) -> Element {
        cell.element
    }
}

/// A table column element
#[derive(Debug, Clone)]
pub struct TableColumn {
    element: Element,
}

impl Default for TableColumn {
    fn default() -> Self {
        Self::new()
    }
}

impl TableColumn {
    /// Create a new table column
    pub fn new() -> Self {
        Self {
            element: Element::new("table:table-column"),
        }
    }

    /// Create table column from element
    pub fn from_element(element: Element) -> Result<Self> {
        if element.tag_name() != "table:table-column" {
            return Err(Error::InvalidFormat("Element is not a table column".to_string()));
        }
        Ok(Self { element })
    }

    /// Get the style name
    pub fn style_name(&self) -> Option<&str> {
        self.element.get_attribute("table:style-name")
    }

    /// Set the style name
    pub fn set_style_name(&mut self, name: &str) {
        self.element.set_attribute("table:style-name", name);
    }

    /// Get the default cell style name
    pub fn default_cell_style_name(&self) -> Option<&str> {
        self.element.get_attribute("table:default-cell-style-name")
    }

    /// Set the default cell style name
    pub fn set_default_cell_style_name(&mut self, name: &str) {
        self.element.set_attribute("table:default-cell-style-name", name);
    }

    /// Get the number of columns this column definition represents
    pub fn repeated(&self) -> usize {
        self.element.get_int_attribute("table:number-columns-repeated")
            .map(|n| n as usize)
            .unwrap_or(1)
    }

    /// Set the number of columns this column definition represents
    pub fn set_repeated(&mut self, count: usize) {
        self.element.set_attribute("table:number-columns-repeated", &count.to_string());
    }
}

impl From<TableColumn> for Element {
    fn from(col: TableColumn) -> Element {
        col.element
    }
}

/// Collection of table elements for easy parsing
pub struct TableElements;

impl TableElements {
    /// Parse all tables from document content (content.xml)
    pub fn parse_tables_from_content(xml_content: &str) -> Result<Vec<Table>> {
        Self::parse_tables(xml_content)
    }

    /// Parse all tables from XML content
    pub fn parse_tables(xml_content: &str) -> Result<Vec<Table>> {
        let mut reader = quick_xml::Reader::from_str(xml_content);
        let mut buf = Vec::new();
        let mut tables = Vec::new();
        let mut stack: Vec<Element> = Vec::new();

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(quick_xml::events::Event::Start(ref e)) => {
                    let tag_name = String::from_utf8(e.name().as_ref().to_vec())
                        .unwrap_or_default();

                    if tag_name == "table:table" {
                        let mut element = Element::new(&tag_name);

                        // Parse attributes
                        for attr_result in e.attributes() {
                            if let Ok(attr) = attr_result
                                && let (Ok(key), Ok(value)) = (
                                    String::from_utf8(attr.key.as_ref().to_vec()),
                                    String::from_utf8(attr.value.to_vec())
                                ) {
                                    element.set_attribute(&key, &value);
                                }
                        }

                        stack.push(element);
                    } else if !stack.is_empty() {
                        // Handle nested elements within table
                        let mut element = Element::new(&tag_name);

                        // Parse attributes
                        for attr_result in e.attributes() {
                            if let Ok(attr) = attr_result
                                && let (Ok(key), Ok(value)) = (
                                    String::from_utf8(attr.key.as_ref().to_vec()),
                                    String::from_utf8(attr.value.to_vec())
                                ) {
                                    element.set_attribute(&key, &value);
                                }
                        }

                        stack.push(element);
                    }
                }
                Ok(quick_xml::events::Event::Text(ref t)) => {
                    if let Some(current) = stack.last_mut()
                        && let Ok(text) = String::from_utf8(t.to_vec()) {
                            let current_text = current.text().to_string();
                            current.set_text(&format!("{}{}", current_text, text));
                        }
                }
                Ok(quick_xml::events::Event::End(ref e)) => {
                    let tag_name = String::from_utf8(e.name().as_ref().to_vec())
                        .unwrap_or_default();

                    if tag_name == "table:table" {
                        if let Some(table_element) = stack.pop()
                            && let Ok(table) = Table::from_element(table_element) {
                                tables.push(table);
                            }
                    } else if !stack.is_empty() {
                        let element = stack.pop().unwrap();
                        if let Some(parent) = stack.last_mut() {
                            parent.add_child(Box::new(element));
                        }
                    }
                }
                Ok(quick_xml::events::Event::Eof) => break,
                Err(_) => break,
                _ => {}
            }
            buf.clear();
        }

        Ok(tables)
    }

    /// Parse table from XML content with proper handling of repeated cells
    #[allow(dead_code)]
    pub fn parse_table_with_expansion(xml_content: &str, table_name: Option<&str>) -> Result<Option<Table>> {
        let tables = Self::parse_tables(xml_content)?;

        for table in tables {
            if table_name.is_none() || table.name() == table_name {
                // Expand repeated cells
                let mut expanded_table = Table::new();
                if let Some(name) = table.name() {
                    expanded_table.set_name(name);
                }
                if let Some(style) = table.style_name() {
                    expanded_table.set_style_name(style);
                }

                for row in table.rows()? {
                    let mut expanded_row = TableRow::new();
                    if let Some(style) = row.style_name() {
                        expanded_row.set_style_name(style);
                    }

                    for cell in row.cells()? {
                        let repeated = cell.element.get_int_attribute("table:number-columns-repeated")
                            .map(|n| n as usize)
                            .unwrap_or(1);

                        for _ in 0..repeated {
                            let mut new_cell = TableCell::new();
                            new_cell.set_text(cell.text()?.as_str());

                            // Copy other attributes
                            if let Some(formula) = cell.formula() {
                                new_cell.set_formula(formula);
                            }
                            if let Some(style) = cell.style_name() {
                                new_cell.set_style_name(style);
                            }
                            if cell.colspan() > 1 {
                                new_cell.set_colspan(cell.colspan());
                            }
                            if cell.rowspan() > 1 {
                                new_cell.set_rowspan(cell.rowspan());
                            }

                            // Copy value attributes
                            for (key, value) in cell.element.attributes() {
                                if key.starts_with("office:") {
                                    new_cell.element.set_attribute(key, value);
                                }
                            }

                            expanded_row.add_cell(new_cell);
                        }
                    }

                    expanded_table.add_row(expanded_row);
                }

                return Ok(Some(expanded_table));
            }
        }

        Ok(None)
    }
}