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
use crate::draw::{Annotation, DrawFrame};
use crate::validation::ValidationRef;
use crate::value_::Value;
use crate::CellStyleRef;
use std::fmt::{Display, Formatter};

/// A cell can span multiple rows/columns.
#[derive(Debug, Clone, Copy)]
pub struct CellSpan {
    pub(crate) row_span: u32,
    pub(crate) col_span: u32,
}

impl Default for CellSpan {
    fn default() -> Self {
        Self {
            row_span: 1,
            col_span: 1,
        }
    }
}

impl Display for CellSpan {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "(+{}+{})", self.row_span, self.col_span)
    }
}

impl From<CellSpan> for (u32, u32) {
    fn from(span: CellSpan) -> Self {
        (span.row_span, span.col_span)
    }
}

impl From<&CellSpan> for (u32, u32) {
    fn from(span: &CellSpan) -> Self {
        (span.row_span, span.col_span)
    }
}

impl CellSpan {
    /// Default span 1,1
    pub fn new() -> Self {
        Self::default()
    }

    /// Is this empty? Defined as row_span==1 and col_span==1.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.row_span == 1 && self.col_span == 1
    }

    /// Sets the row span of this cell.
    /// Cells below with values will be lost when writing.
    #[inline]
    pub fn set_row_span(&mut self, rows: u32) {
        assert!(rows > 0);
        self.row_span = rows;
    }

    /// Returns the row span.
    #[inline]
    pub fn row_span(&self) -> u32 {
        self.row_span
    }

    /// Sets the column span of this cell.
    /// Cells to the right with values will be lost when writing.
    #[inline]
    pub fn set_col_span(&mut self, cols: u32) {
        assert!(cols > 0);
        self.col_span = cols;
    }

    /// Returns the col span.
    #[inline]
    pub fn col_span(&self) -> u32 {
        self.col_span
    }
}

/// One Cell of the spreadsheet.
#[derive(Debug, Clone)]
pub(crate) struct CellData {
    pub(crate) value: Value,
    // Unparsed formula string.
    pub(crate) formula: Option<String>,
    // Cell style name.
    pub(crate) style: Option<String>,
    // Cell repeated.
    pub(crate) repeat: u32,
    // Scarcely used extra data.
    pub(crate) extra: Option<Box<CellDataExt>>,
}

/// Extra cell data.
#[derive(Debug, Clone, Default)]
pub(crate) struct CellDataExt {
    // Content validation name.
    pub(crate) validation_name: Option<String>,
    // Row/Column span.
    pub(crate) span: CellSpan,
    // Matrix span.
    pub(crate) matrix_span: CellSpan,
    // Annotation
    pub(crate) annotation: Option<Annotation>,
    // Draw
    pub(crate) draw_frames: Vec<DrawFrame>,
}

impl Default for CellData {
    #[inline]
    fn default() -> Self {
        Self {
            value: Default::default(),
            formula: None,
            style: None,
            repeat: 1,
            extra: None,
        }
    }
}

impl CellData {
    /// Holds no value and no formula.
    pub(crate) fn is_empty(&self) -> bool {
        self.value == Value::Empty && self.formula.is_none()
    }

    /// Holds no useful data at all.
    pub(crate) fn is_void(&self) -> bool {
        self.value == Value::Empty
            && self.formula.is_none()
            && self.style.is_none()
            // repeated nothing is still nothing: && self.repeat == 1
            && (self.extra.is_none()
                || self.extra.as_ref().is_some_and(|v| {
                    v.validation_name.is_none()
                        && v.span.is_empty()
                        && v.matrix_span.is_empty()
                        && v.annotation.is_none()
                        && v.draw_frames.is_empty()
                }))
    }

    pub(crate) fn extra_mut(&mut self) -> &mut CellDataExt {
        if self.extra.is_none() {
            self.extra = Some(Box::default());
        }
        self.extra.as_mut().expect("celldataext")
    }

    pub(crate) fn cloned_cell_content(&self) -> CellContent {
        let (validation_name, span, matrix_span, annotation, draw_frames) =
            if let Some(extra) = &self.extra {
                (
                    extra.validation_name.clone(),
                    extra.span,
                    extra.matrix_span,
                    extra.annotation.clone(),
                    extra.draw_frames.clone(),
                )
            } else {
                (
                    None,
                    Default::default(),
                    Default::default(),
                    None,
                    Vec::new(),
                )
            };

        CellContent {
            value: self.value.clone(),
            style: self.style.clone(),
            formula: self.formula.clone(),
            repeat: self.repeat,
            validation_name,
            span,
            matrix_span,
            annotation,
            draw_frames,
        }
    }

    pub(crate) fn into_cell_content(self) -> CellContent {
        let (validation_name, span, matrix_span, annotation, draw_frames) =
            if let Some(extra) = self.extra {
                (
                    extra.validation_name,
                    extra.span,
                    extra.matrix_span,
                    extra.annotation,
                    extra.draw_frames,
                )
            } else {
                (
                    None,
                    Default::default(),
                    Default::default(),
                    None,
                    Vec::new(),
                )
            };

        CellContent {
            value: self.value,
            style: self.style,
            formula: self.formula,
            repeat: self.repeat,
            validation_name,
            span,
            matrix_span,
            annotation,
            draw_frames,
        }
    }

    pub(crate) fn cell_content_ref(&self) -> CellContentRef<'_> {
        let (validation_name, span, matrix_span, annotation, draw_frames) =
            if let Some(extra) = &self.extra {
                (
                    extra.validation_name.as_ref(),
                    Some(&extra.span),
                    Some(&extra.matrix_span),
                    extra.annotation.as_ref(),
                    Some(&extra.draw_frames),
                )
            } else {
                (None, None, None, None, None)
            };

        CellContentRef {
            value: &self.value,
            style: self.style.as_ref(),
            formula: self.formula.as_ref(),
            repeat: &self.repeat,
            validation_name,
            span,
            matrix_span,
            annotation,
            draw_frames,
        }
    }
}

/// Holds references to the combined content of a cell.
/// A temporary to hold the data when iterating over a sheet.
#[derive(Debug, Clone, Copy)]
pub struct CellContentRef<'a> {
    /// Reference to the cell value.
    pub value: &'a Value,
    /// Reference to the stylename.
    pub style: Option<&'a String>,
    /// Reference to the cell formula.
    pub formula: Option<&'a String>,
    /// Reference to the repeat count.
    pub repeat: &'a u32,
    /// Reference to a cell validation.
    pub validation_name: Option<&'a String>,
    /// Reference to the cellspan.
    pub span: Option<&'a CellSpan>,
    /// Reference to a matrix cellspan.
    pub matrix_span: Option<&'a CellSpan>,
    /// Reference to an annotation.
    pub annotation: Option<&'a Annotation>,
    /// Reference to draw-frames.
    pub draw_frames: Option<&'a Vec<DrawFrame>>,
}

impl<'a> CellContentRef<'a> {
    /// Returns the value.
    #[inline]
    pub fn value(&self) -> &'a Value {
        self.value
    }

    /// Returns the formula.
    #[inline]
    pub fn formula(&self) -> Option<&'a String> {
        self.formula
    }

    /// Returns the cell style.
    #[inline]
    pub fn style(&self) -> Option<&'a String> {
        self.style
    }

    /// Returns the repeat count.
    #[inline]
    pub fn repeat(&self) -> &'a u32 {
        self.repeat
    }

    /// Returns the validation name.
    #[inline]
    pub fn validation(&self) -> Option<&'a String> {
        self.validation_name
    }

    /// Returns the row span.
    #[inline]
    pub fn row_span(&self) -> u32 {
        if let Some(span) = self.span {
            span.row_span
        } else {
            1
        }
    }

    /// Returns the col span.
    #[inline]
    pub fn col_span(&self) -> u32 {
        if let Some(span) = self.span {
            span.col_span
        } else {
            1
        }
    }

    /// Returns the row span for a matrix.
    #[inline]
    pub fn matrix_row_span(&self) -> u32 {
        if let Some(matrix_span) = self.matrix_span {
            matrix_span.row_span
        } else {
            1
        }
    }

    /// Returns the col span for a matrix.
    #[inline]
    pub fn matrix_col_span(&self) -> u32 {
        if let Some(matrix_span) = self.matrix_span {
            matrix_span.col_span
        } else {
            1
        }
    }

    /// Returns the validation name.
    #[inline]
    pub fn annotation(&self) -> Option<&'a Annotation> {
        self.annotation
    }

    /// Returns draw frames.
    #[inline]
    pub fn draw_frames(&self) -> Option<&'a Vec<DrawFrame>> {
        self.draw_frames
    }
}

/// A copy of the relevant data for a spreadsheet cell.
#[derive(Debug, Clone, Default)]
pub struct CellContent {
    /// Cell value.
    pub value: Value,
    /// Cell stylename.
    pub style: Option<String>,
    /// Cell formula.
    pub formula: Option<String>,
    /// Cell repeat count.
    pub repeat: u32,
    /// Reference to a validation rule.
    pub validation_name: Option<String>,
    /// Cellspan.
    pub span: CellSpan,
    /// Matrix span.
    pub matrix_span: CellSpan,
    /// Annotation
    pub annotation: Option<Annotation>,
    /// DrawFrames
    pub draw_frames: Vec<DrawFrame>,
}

impl CellContent {
    /// Empty.
    #[inline]
    pub fn new() -> Self {
        Default::default()
    }

    ///
    pub(crate) fn into_celldata(mut self) -> CellData {
        let extra = self.into_celldata_ext();
        CellData {
            value: self.value,
            formula: self.formula,
            style: self.style,
            repeat: self.repeat,
            extra,
        }
    }

    /// Move stuff into a CellDataExt.
    #[allow(clippy::wrong_self_convention)]
    pub(crate) fn into_celldata_ext(&mut self) -> Option<Box<CellDataExt>> {
        if self.validation_name.is_some()
            || !self.span.is_empty()
            || !self.matrix_span.is_empty()
            || self.annotation.is_some()
            || !self.draw_frames.is_empty()
        {
            Some(Box::new(CellDataExt {
                validation_name: self.validation_name.take(),
                span: self.span,
                matrix_span: self.matrix_span,
                annotation: self.annotation.take(),
                draw_frames: std::mem::take(&mut self.draw_frames),
            }))
        } else {
            None
        }
    }

    /// Returns the value.
    #[inline]
    pub fn value(&self) -> &Value {
        &self.value
    }

    /// Sets the value.
    #[inline]
    pub fn set_value<V: Into<Value>>(&mut self, value: V) {
        self.value = value.into();
    }

    /// Returns the formula.
    #[inline]
    pub fn formula(&self) -> Option<&String> {
        self.formula.as_ref()
    }

    /// Sets the formula.
    #[inline]
    pub fn set_formula<V: Into<String>>(&mut self, formula: V) {
        self.formula = Some(formula.into());
    }

    /// Resets the formula.
    #[inline]
    pub fn clear_formula(&mut self) {
        self.formula = None;
    }

    /// Returns the cell style.
    #[inline]
    pub fn style(&self) -> Option<&String> {
        self.style.as_ref()
    }

    /// Sets the cell style.
    #[inline]
    pub fn set_style(&mut self, style: &CellStyleRef) {
        self.style = Some(style.to_string());
    }

    /// Removes the style.
    #[inline]
    pub fn clear_style(&mut self) {
        self.style = None;
    }

    /// Sets the repeat count for the cell.
    /// Value must be > 0.
    #[inline]
    pub fn set_repeat(&mut self, repeat: u32) {
        assert!(repeat > 0);
        self.repeat = repeat;
    }

    /// Returns the repeat count for the cell.
    #[inline]
    pub fn get_repeat(&mut self) -> u32 {
        self.repeat
    }

    /// Returns the validation name.
    #[inline]
    pub fn validation(&self) -> Option<&String> {
        self.validation_name.as_ref()
    }

    /// Sets the validation name.
    #[inline]
    pub fn set_validation(&mut self, validation: &ValidationRef) {
        self.validation_name = Some(validation.to_string());
    }

    /// No validation.
    #[inline]
    pub fn clear_validation(&mut self) {
        self.validation_name = None;
    }

    /// Sets the row span of this cell.
    /// Cells below with values will be lost when writing.
    #[inline]
    pub fn set_row_span(&mut self, rows: u32) {
        assert!(rows > 0);
        self.span.row_span = rows;
    }

    /// Returns the row span.
    #[inline]
    pub fn row_span(&self) -> u32 {
        self.span.row_span
    }

    /// Sets the column span of this cell.
    /// Cells to the right with values will be lost when writing.
    #[inline]
    pub fn set_col_span(&mut self, cols: u32) {
        assert!(cols > 0);
        self.span.col_span = cols;
    }

    /// Returns the col span.
    #[inline]
    pub fn col_span(&self) -> u32 {
        self.span.col_span
    }

    /// Sets the row span of this cell.
    /// Cells below with values will be lost when writing.
    #[inline]
    pub fn set_matrix_row_span(&mut self, rows: u32) {
        assert!(rows > 0);
        self.matrix_span.row_span = rows;
    }

    /// Returns the row span.
    #[inline]
    pub fn matrix_row_span(&self) -> u32 {
        self.matrix_span.row_span
    }

    /// Sets the column span of this cell.
    /// Cells to the right with values will be lost when writing.
    #[inline]
    pub fn set_matrix_col_span(&mut self, cols: u32) {
        assert!(cols > 0);
        self.matrix_span.col_span = cols;
    }

    /// Returns the col span.
    #[inline]
    pub fn matrix_col_span(&self) -> u32 {
        self.matrix_span.col_span
    }

    /// Annotation
    #[inline]
    pub fn set_annotation(&mut self, annotation: Annotation) {
        self.annotation = Some(annotation);
    }

    /// Annotation
    #[inline]
    pub fn clear_annotation(&mut self) {
        self.annotation = None;
    }

    /// Returns the Annotation
    #[inline]
    pub fn annotation(&self) -> Option<&Annotation> {
        self.annotation.as_ref()
    }

    /// Draw Frames
    #[inline]
    pub fn set_draw_frames(&mut self, draw_frames: Vec<DrawFrame>) {
        self.draw_frames = draw_frames;
    }

    /// Draw Frames
    #[inline]
    pub fn draw_frames(&self) -> &Vec<DrawFrame> {
        &self.draw_frames
    }
}