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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
//! BIFF record parsing for XLS files
//!
//! This module handles the parsing of BIFF (Binary Interchange File Format)
//! records used in Excel XLS files. BIFF records contain various types of
//! data including cell values, formatting, formulas, and metadata.

use std::io::{Read, Seek, SeekFrom};
use zerocopy::{FromBytes, LE, U16};

use crate::ole::binary;
use crate::ole::xls::error::{XlsError, XlsResult};
use crate::ole::xls::utils;

/// BIFF record header (4 bytes: type + length)
#[derive(Debug, Clone)]
pub struct RecordHeader {
    pub record_type: u16,
    pub data_len: u16,
}

impl RecordHeader {
    /// Parse record header from stream
    pub fn read<R: Read>(reader: &mut R) -> XlsResult<Self> {
        let mut buf = [0u8; 4];
        reader.read_exact(&mut buf)?;
        let record_type = U16::<LE>::read_from_bytes(&buf[0..2])
            .map(|v| v.get())
            .unwrap_or(0);
        let data_len = U16::<LE>::read_from_bytes(&buf[2..4])
            .map(|v| v.get())
            .unwrap_or(0);

        Ok(RecordHeader {
            record_type,
            data_len,
        })
    }
}

/// Iterator over BIFF records in a stream
pub struct RecordIter<R> {
    reader: R,
    stream_len: u64,
    current_pos: u64,
}

impl<R: Read + Seek> RecordIter<R> {
    pub fn new(mut reader: R) -> XlsResult<Self> {
        let stream_len = reader.seek(SeekFrom::End(0))?;
        reader.seek(SeekFrom::Start(0))?;

        Ok(RecordIter {
            reader,
            stream_len,
            current_pos: 0,
        })
    }

    /// Seek to a specific position in the stream
    pub fn seek(&mut self, pos: u64) -> XlsResult<()> {
        self.reader.seek(SeekFrom::Start(pos))?;
        self.current_pos = pos;
        Ok(())
    }
}

impl<R: Read + Seek> Iterator for RecordIter<R> {
    type Item = XlsResult<Record>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_pos >= self.stream_len {
            return None;
        }

        match Record::read(&mut self.reader) {
            Ok(record) => {
                self.current_pos += 4 + record.header.data_len as u64;
                Some(Ok(record))
            }
            Err(e) => Some(Err(e)),
        }
    }
}

/// A BIFF record with header and data
#[derive(Debug, Clone)]
pub struct Record {
    pub header: RecordHeader,
    pub data: Vec<u8>,
}

impl Record {
    /// Read a complete record from the stream
    pub fn read<R: Read>(reader: &mut R) -> XlsResult<Self> {
        let header = RecordHeader::read(reader)?;

        let mut data = vec![0u8; header.data_len as usize];
        reader.read_exact(&mut data)?;

        Ok(Record { header, data })
    }
}

/// BIFF versions supported
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BiffVersion {
    Biff2 = 0x0200,
    Biff3 = 0x0300,
    Biff4 = 0x0400,
    Biff5 = 0x0500,
    Biff8 = 0x0600,
}

impl BiffVersion {
    pub fn from_bof_version(version: u16) -> Option<Self> {
        match version {
            0x0200 | 0x0002 | 0x0007 => Some(BiffVersion::Biff2),
            0x0300 => Some(BiffVersion::Biff3),
            0x0400 => Some(BiffVersion::Biff4),
            0x0500 => Some(BiffVersion::Biff5),
            0x0600 => Some(BiffVersion::Biff8),
            _ => None,
        }
    }

    #[allow(dead_code)]
    pub fn supports_unicode(&self) -> bool {
        matches!(self, BiffVersion::Biff8)
    }
}

/// BOF (Beginning of File) record
#[derive(Debug, Clone)]
pub struct BofRecord {
    pub version: BiffVersion,
    pub is_1904_date_system: bool,
}

impl BofRecord {
    pub fn parse(data: &[u8]) -> XlsResult<Self> {
        if data.len() < 4 {
            return Err(XlsError::InvalidLength {
                expected: 4,
                found: data.len(),
            });
        }

        let biff_version = binary::read_u16_le_at(data, 0)?;
        let dt = if data.len() >= 6 {
            binary::read_u16_le_at(data, 4)?
        } else {
            0
        };

        let version = BiffVersion::from_bof_version(biff_version)
            .ok_or(XlsError::UnsupportedBiffVersion(biff_version))?;

        let is_1904_date_system = dt == 1;

        Ok(BofRecord {
            version,
            is_1904_date_system,
        })
    }
}

/// Dimensions record (worksheet bounds)
#[derive(Debug, Clone)]
pub struct DimensionsRecord {
    pub first_row: u32,
    pub last_row: u32,
    pub first_col: u32,
    pub last_col: u32,
}

impl DimensionsRecord {
    pub fn parse(data: &[u8]) -> XlsResult<Self> {
        match data.len() {
            10 => {
                // BIFF5-BIFF8
                Ok(DimensionsRecord {
                    first_row: binary::read_u16_le_at(data, 0)? as u32,
                    last_row: binary::read_u16_le_at(data, 2)? as u32,
                    first_col: binary::read_u16_le_at(data, 4)? as u32,
                    last_col: binary::read_u16_le_at(data, 6)? as u32,
                })
            }
            14 => {
                // BIFF8 with 32-bit row indices
                Ok(DimensionsRecord {
                    first_row: binary::read_u32_le_at(data, 0)?,
                    last_row: binary::read_u32_le_at(data, 4)?,
                    first_col: binary::read_u16_le_at(data, 8)? as u32,
                    last_col: binary::read_u16_le_at(data, 10)? as u32,
                })
            }
            _ => Err(XlsError::InvalidLength {
                expected: 10,
                found: data.len(),
            }),
        }
    }
}

/// Sheet visibility types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SheetVisible {
    Visible = 0x00,
    Hidden = 0x01,
    VeryHidden = 0x02,
}

impl SheetVisible {
    pub fn from_u8(value: u8) -> XlsResult<Self> {
        match value & 0x3 {
            0x00 => Ok(SheetVisible::Visible),
            0x01 => Ok(SheetVisible::Hidden),
            0x02 => Ok(SheetVisible::VeryHidden),
            v => Err(XlsError::InvalidRecord {
                record_type: 0x0085, // BoundSheet8
                message: format!("Invalid visibility value: {}", v),
            }),
        }
    }
}

/// Sheet types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SheetType {
    WorkSheet,
    MacroSheet,
    ChartSheet,
    VBModule,
}

impl SheetType {
    pub fn from_u8(value: u8) -> XlsResult<Self> {
        match value {
            0x00 => Ok(SheetType::WorkSheet),
            0x01 => Ok(SheetType::MacroSheet),
            0x02 => Ok(SheetType::ChartSheet),
            0x06 => Ok(SheetType::VBModule),
            v => Err(XlsError::InvalidRecord {
                record_type: 0x0085, // BoundSheet8
                message: format!("Invalid sheet type: {}", v),
            }),
        }
    }
}

/// BoundSheet8 record (worksheet metadata)
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct BoundSheetRecord {
    pub position: u32,
    pub visible: SheetVisible,
    pub sheet_type: SheetType,
    pub name: String,
}

impl BoundSheetRecord {
    pub fn parse(data: &[u8], encoding: &XlsEncoding) -> XlsResult<Self> {
        if data.len() < 8 {
            return Err(XlsError::InvalidLength {
                expected: 8,
                found: data.len(),
            });
        }

        let position = binary::read_u32_le_at(data, 0)?;
        let visible = SheetVisible::from_u8(data[4])?;
        let sheet_type = SheetType::from_u8(data[5])?;

        // Skip 2 bytes and parse the name
        let name_data = &data[6..];
        let name = utils::parse_short_string(name_data, encoding)?;

        Ok(BoundSheetRecord {
            position,
            visible,
            sheet_type,
            name,
        })
    }
}

/// Codepage/encoding information
#[derive(Debug, Clone)]
pub enum XlsEncoding {
    /// Single-byte encoding with codepage
    Codepage(u16),
    /// UTF-16 little endian (BIFF8+)
    Utf16Le,
}

impl XlsEncoding {
    pub fn from_codepage(codepage: u16) -> XlsResult<Self> {
        // Use codepage crate for proper encoding support
        // For now, we'll handle common codepages
        match codepage {
            1200 => Ok(XlsEncoding::Utf16Le),
            cp => Ok(XlsEncoding::Codepage(cp)),
        }
    }

    pub fn decode(&self, data: &[u8]) -> XlsResult<String> {
        match self {
            XlsEncoding::Utf16Le => {
                // UTF-16 LE decoding
                if !data.len().is_multiple_of(2) {
                    return Err(XlsError::Encoding("Invalid UTF-16 data length".to_string()));
                }
                let utf16_data: Vec<u16> = data.chunks_exact(2)
                    .map(|chunk| U16::<LE>::read_from_bytes(chunk)
                        .map(|v| v.get())
                        .unwrap_or(0))
                    .collect();
                String::from_utf16(&utf16_data)
                    .map_err(|e| XlsError::Encoding(format!("UTF-16 decoding error: {}", e)))
            }
            XlsEncoding::Codepage(cp) => {
                // For now, assume Latin-1 for common codepages
                // In production, use proper codepage conversion
                String::from_utf8(data.to_vec())
                    .or_else(|_| {
                        // Fallback to lossy conversion
                        Ok(String::from_utf8_lossy(data).into_owned())
                    })
                    .map_err(|e: std::string::FromUtf8Error| XlsError::Encoding(format!("Codepage {} decoding error: {}", cp, e)))
            }
        }
    }
}

/// SST (Shared String Table) record
#[derive(Debug, Clone)]
pub struct SharedStringTable {
    pub strings: Vec<String>,
}

impl SharedStringTable {
    /// Parse SST from potentially multiple records (SST + CONTINUE)
    pub fn parse_from_records(records: &[Record], encoding: &XlsEncoding) -> XlsResult<Self> {
        if records.is_empty() {
            return Ok(SharedStringTable { strings: Vec::new() });
        }

        // Combine all SST and CONTINUE record data
        let mut combined_data = Vec::new();
        let mut found_sst = false;

        for record in records {
            match record.header.record_type {
                0x00FC => { // SST
                    if found_sst {
                        // Multiple SST records? Shouldn't happen
                        break;
                    }
                    found_sst = true;
                    // Skip the SST record header (4 bytes record type + 4 bytes length = 8 bytes total)
                    // But we need to include the SST data header (cstTotal + cstUnique = 8 bytes)
                    combined_data.extend_from_slice(&record.data);
                }
                0x003C => { // CONTINUE
                    if found_sst {
                        combined_data.extend_from_slice(&record.data);
                    }
                }
                _ => {
                    if found_sst {
                        // Stop when we hit a non-CONTINUE record after SST
                        break;
                    }
                }
            }
        }

        if combined_data.is_empty() {
            return Ok(SharedStringTable { strings: Vec::new() });
        }

        Self::parse(&combined_data, encoding)
    }

    pub fn parse(data: &[u8], encoding: &XlsEncoding) -> XlsResult<Self> {
        if data.len() < 8 {
            return Err(XlsError::InvalidLength {
                expected: 8,
                found: data.len(),
            });
        }

        // Read SST header: cstTotal (4 bytes) and cstUnique (4 bytes)
        let _cst_total = binary::read_u32_le(data, 0)? as usize; // Total number of strings - kept for future validation
        let cst_unique = binary::read_u32_le(data, 4)? as usize;

        let mut strings = Vec::with_capacity(cst_unique.min(10000)); // Cap for safety
        let mut offset = 8;

        // Parse each string entry in SST format
        for _ in 0..cst_unique {
            if offset + 3 > data.len() {
                break;
            }

            // Parse SST string format: cch (2 bytes) + flags (1 byte) + optional data
            let cch = binary::read_u16_le(data, offset)? as usize;
            let flags = data[offset + 2];

            let mut consumed = 3; // cch + flags

            // Rich text formatting (optional)
            if (flags & 0x08) != 0 {
                if offset + consumed + 2 > data.len() {
                    break;
                }
                let c_run = binary::read_u16_le(data, offset + consumed)?;
                consumed += 2;
                // Skip the formatting runs (4 bytes each)
                consumed += c_run as usize * 4;
            }

            // Phonetic information (optional)
            if (flags & 0x04) != 0 {
                if offset + consumed + 4 > data.len() {
                    break;
                }
                let cb_phonetic = binary::read_u32_le(data, offset + consumed)?;
                consumed += 4;
                // Skip the phonetic data
                consumed += cb_phonetic as usize;
            }

            // String data
            let is_unicode = (flags & 0x01) != 0;
            let string_len = if is_unicode { cch * 2 } else { cch };

            if offset + consumed + string_len > data.len() {
                break;
            }

            let string_data = &data[offset + consumed..offset + consumed + string_len];

            let string = if is_unicode {
                // UTF-16 LE
                String::from_utf16(&string_data
                    .chunks_exact(2)
                    .map(|chunk| U16::<LE>::read_from_bytes(chunk)
                        .map(|v| v.get())
                        .unwrap_or(0))
                    .collect::<Vec<_>>())
                    .unwrap_or_default()
            } else {
                // 8-bit characters
                encoding.decode(string_data).unwrap_or_default()
            };

            strings.push(string);
            offset += consumed + string_len;
        }

        Ok(SharedStringTable { strings })
    }

    /// Parse a single string entry from SST data
    #[allow(dead_code)]
    fn parse_string_entry(data: &[u8], encoding: &XlsEncoding) -> XlsResult<(String, usize)> {
        if data.len() < 3 {
            return Err(XlsError::InvalidLength {
                expected: 3,
                found: data.len(),
            });
        }

        // Read string header: cch (2 bytes) and flags (1 byte)
        let cch = binary::read_u16_le(data, 0)? as usize;
        let flags = data[2];

        let mut offset = 3;
        let mut consumed = 3;

        // Rich text formatting (optional)
        if (flags & 0x08) != 0 {
            if offset + 2 > data.len() {
                return Err(XlsError::InvalidData("Incomplete rich text header".to_string()));
            }
            let _c_run = binary::read_u16_le(data, offset)?;
            offset += 2;
            consumed += 2;
        }

        // Phonetic information (optional)
        if (flags & 0x04) != 0 {
            if offset + 4 > data.len() {
                return Err(XlsError::InvalidData("Incomplete phonetic header".to_string()));
            }
            let _cch_phonetic = binary::read_u32_le(data, offset)?;
            offset += 4;
            consumed += 4;
        }

        // String data
        let is_unicode = (flags & 0x01) != 0;
        let string_data;
        let string_consumed;

        if is_unicode {
            // UTF-16 LE
            let expected_bytes = cch * 2;
            if offset + expected_bytes > data.len() {
                return Err(XlsError::InvalidData("Incomplete Unicode string".to_string()));
            }
            string_data = &data[offset..offset + expected_bytes];
            string_consumed = expected_bytes;

            // Convert UTF-16 LE to String
            let utf16_words: Vec<u16> = string_data
                .chunks_exact(2)
                .map(|chunk| U16::<LE>::read_from_bytes(chunk)
                    .map(|v| v.get())
                    .unwrap_or(0))
                .collect();

            match String::from_utf16(&utf16_words) {
                Ok(s) => Ok((s, consumed + string_consumed)),
                Err(e) => Err(XlsError::InvalidData(format!("Invalid UTF-16: {}", e))),
            }
        } else {
            // Compressed (8-bit characters)
            if offset + cch > data.len() {
                return Err(XlsError::InvalidData("Incomplete compressed string".to_string()));
            }
            string_data = &data[offset..offset + cch];
            string_consumed = cch;

            // Convert using the specified encoding
            match encoding.decode(string_data) {
                Ok(s) => Ok((s, consumed + string_consumed)),
                Err(e) => Err(XlsError::InvalidData(format!("Encoding error: {}", e))),
            }
        }
    }
}

/// XF (Extended Format) record - cell formatting
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct ExtendedFormat {
    pub font_index: u16,
    pub format_index: u16,
}

#[allow(dead_code)]
impl ExtendedFormat {
    pub fn parse(data: &[u8]) -> XlsResult<Self> {
        if data.len() < 4 {
            return Err(XlsError::InvalidLength {
                expected: 4,
                found: data.len(),
            });
        }

        let font_index = binary::read_u16_le_at(data, 0)?;
        let format_index = binary::read_u16_le_at(data, 2)?;

        Ok(ExtendedFormat {
            font_index,
            format_index,
        })
    }
}

/// Cell records
#[derive(Debug, Clone)]
pub enum CellRecord {
    Blank {
        row: u16,
        col: u16,
        xf_index: u16,
    },
    Number {
        row: u16,
        col: u16,
        xf_index: u16,
        value: f64,
    },
    Label {
        row: u16,
        col: u16,
        xf_index: u16,
        value: String,
    },
    BoolErr {
        row: u16,
        col: u16,
        xf_index: u16,
        value: BoolErrValue,
    },
    Rk {
        row: u16,
        col: u16,
        xf_index: u16,
        value: f64,
    },
    LabelSst {
        row: u16,
        col: u16,
        xf_index: u16,
        sst_index: u32,
    },
    Formula {
        row: u16,
        col: u16,
        xf_index: u16,
        value: FormulaValue,
        formula: Vec<u8>,
    },
}

#[derive(Debug, Clone)]
pub enum BoolErrValue {
    Bool(bool),
    Error(u8),
}

#[derive(Debug, Clone)]
pub enum FormulaValue {
    Number(f64),
    String(String),
    Bool(bool),
    Error(u8),
    Empty,
}

impl CellRecord {
    pub fn row(&self) -> u16 {
        match self {
            CellRecord::Blank { row, .. } => *row,
            CellRecord::Number { row, .. } => *row,
            CellRecord::Label { row, .. } => *row,
            CellRecord::BoolErr { row, .. } => *row,
            CellRecord::Rk { row, .. } => *row,
            CellRecord::LabelSst { row, .. } => *row,
            CellRecord::Formula { row, .. } => *row,
        }
    }

    pub fn col(&self) -> u16 {
        match self {
            CellRecord::Blank { col, .. } => *col,
            CellRecord::Number { col, .. } => *col,
            CellRecord::Label { col, .. } => *col,
            CellRecord::BoolErr { col, .. } => *col,
            CellRecord::Rk { col, .. } => *col,
            CellRecord::LabelSst { col, .. } => *col,
            CellRecord::Formula { col, .. } => *col,
        }
    }

    pub fn parse(record_type: u16, data: &[u8], encoding: &XlsEncoding) -> XlsResult<Self> {
        match record_type {
            0x0201 => Self::parse_blank(data), // Blank
            0x0203 => Self::parse_number(data), // Number
            0x0204 => Self::parse_label(data, encoding), // Label
            0x0205 => Self::parse_bool_err(data), // BoolErr
            0x027E => Self::parse_rk(data), // RK
            0x00FD => Self::parse_label_sst(data), // LabelSst
            0x0006 => Self::parse_formula(data), // Formula
            _ => Err(XlsError::InvalidRecord {
                record_type,
                message: "Unknown cell record type".to_string(),
            }),
        }
    }

    fn parse_blank(data: &[u8]) -> XlsResult<Self> {
        if data.len() < 6 {
            return Err(XlsError::InvalidLength {
                expected: 6,
                found: data.len(),
            });
        }

        Ok(CellRecord::Blank {
            row: binary::read_u16_le_at(data, 0)?,
            col: binary::read_u16_le_at(data, 2)?,
            xf_index: binary::read_u16_le_at(data, 4)?,
        })
    }

    fn parse_number(data: &[u8]) -> XlsResult<Self> {
        if data.len() < 14 {
            return Err(XlsError::InvalidLength {
                expected: 14,
                found: data.len(),
            });
        }

        Ok(CellRecord::Number {
            row: binary::read_u16_le_at(data, 0)?,
            col: binary::read_u16_le_at(data, 2)?,
            xf_index: binary::read_u16_le_at(data, 4)?,
            value: binary::read_f64_le_at(data, 6)?,
        })
    }

    fn parse_label(data: &[u8], encoding: &XlsEncoding) -> XlsResult<Self> {
        if data.len() < 8 {
            return Err(XlsError::InvalidLength {
                expected: 8,
                found: data.len(),
            });
        }

        let row = binary::read_u16_le_at(data, 0)?;
        let col = binary::read_u16_le_at(data, 2)?;
        let xf_index = binary::read_u16_le_at(data, 4)?;
        let value = utils::parse_string_record(&data[6..], encoding)?;

        Ok(CellRecord::Label {
            row,
            col,
            xf_index,
            value,
        })
    }

    fn parse_bool_err(data: &[u8]) -> XlsResult<Self> {
        if data.len() < 8 {
            return Err(XlsError::InvalidLength {
                expected: 8,
                found: data.len(),
            });
        }

        let row = binary::read_u16_le_at(data, 0)?;
        let col = binary::read_u16_le_at(data, 2)?;
        let xf_index = binary::read_u16_le_at(data, 4)?;
        let value = if data[7] == 0 {
            BoolErrValue::Bool(data[6] != 0)
        } else {
            BoolErrValue::Error(data[6])
        };

        Ok(CellRecord::BoolErr {
            row,
            col,
            xf_index,
            value,
        })
    }

    fn parse_rk(data: &[u8]) -> XlsResult<Self> {
        if data.len() < 10 {
            return Err(XlsError::InvalidLength {
                expected: 10,
                found: data.len(),
            });
        }

        let row = binary::read_u16_le_at(data, 0)?;
        let col = binary::read_u16_le_at(data, 2)?;
        let xf_index = binary::read_u16_le_at(data, 4)?;
        let rk_value = binary::read_u32_le_at(data, 6)?;
        let value = utils::rk_to_f64(rk_value);

        Ok(CellRecord::Rk {
            row,
            col,
            xf_index,
            value,
        })
    }

    fn parse_label_sst(data: &[u8]) -> XlsResult<Self> {
        if data.len() < 10 {
            return Err(XlsError::InvalidLength {
                expected: 10,
                found: data.len(),
            });
        }

        Ok(CellRecord::LabelSst {
            row: binary::read_u16_le_at(data, 0)?,
            col: binary::read_u16_le_at(data, 2)?,
            xf_index: binary::read_u16_le_at(data, 4)?,
            sst_index: binary::read_u32_le_at(data, 6)?,
        })
    }

    fn parse_formula(data: &[u8]) -> XlsResult<Self> {
        if data.len() < 20 {
            return Err(XlsError::InvalidLength {
                expected: 20,
                found: data.len(),
            });
        }

        let row = binary::read_u16_le_at(data, 0)?;
        let col = binary::read_u16_le_at(data, 2)?;
        let xf_index = binary::read_u16_le_at(data, 4)?;
        let value = utils::parse_formula_value(&data[6..14])?;
        let formula = data[20..].to_vec();

        Ok(CellRecord::Formula {
            row,
            col,
            xf_index,
            value,
            formula,
        })
    }
}