1use crate::draw::{Annotation, DrawFrame};
2use crate::validation::ValidationRef;
3use crate::value_::Value;
4use crate::CellStyleRef;
5use get_size::GetSize;
6use get_size_derive::GetSize;
7use std::fmt::{Display, Formatter};
8
9#[derive(Debug, Clone, Copy, GetSize)]
11pub struct CellSpan {
12 pub(crate) row_span: u32,
13 pub(crate) col_span: u32,
14}
15
16impl Default for CellSpan {
17 fn default() -> Self {
18 Self {
19 row_span: 1,
20 col_span: 1,
21 }
22 }
23}
24
25impl Display for CellSpan {
26 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
27 write!(f, "(+{}+{})", self.row_span, self.col_span)
28 }
29}
30
31impl From<CellSpan> for (u32, u32) {
32 fn from(span: CellSpan) -> Self {
33 (span.row_span, span.col_span)
34 }
35}
36
37impl From<&CellSpan> for (u32, u32) {
38 fn from(span: &CellSpan) -> Self {
39 (span.row_span, span.col_span)
40 }
41}
42
43impl CellSpan {
44 pub fn new() -> Self {
46 Self::default()
47 }
48
49 #[inline]
51 pub fn is_empty(&self) -> bool {
52 self.row_span == 1 && self.col_span == 1
53 }
54
55 #[inline]
58 pub fn set_row_span(&mut self, rows: u32) {
59 assert!(rows > 0);
60 self.row_span = rows;
61 }
62
63 #[inline]
65 pub fn row_span(&self) -> u32 {
66 self.row_span
67 }
68
69 #[inline]
72 pub fn set_col_span(&mut self, cols: u32) {
73 assert!(cols > 0);
74 self.col_span = cols;
75 }
76
77 #[inline]
79 pub fn col_span(&self) -> u32 {
80 self.col_span
81 }
82}
83
84#[derive(Debug, Clone, GetSize)]
86pub(crate) struct CellData {
87 pub(crate) value: Value,
88 pub(crate) formula: Option<String>,
90 pub(crate) style: Option<CellStyleRef>,
92 pub(crate) repeat: u32,
94 pub(crate) extra: Option<Box<CellDataExt>>,
96}
97
98#[derive(Debug, Clone, Default, GetSize)]
100pub(crate) struct CellDataExt {
101 pub(crate) validation_name: Option<ValidationRef>,
103 pub(crate) span: CellSpan,
105 pub(crate) matrix_span: CellSpan,
107 pub(crate) annotation: Option<Box<Annotation>>,
109 pub(crate) draw_frames: Vec<DrawFrame>,
111}
112
113impl Default for CellData {
114 #[inline]
115 fn default() -> Self {
116 Self {
117 value: Default::default(),
118 formula: None,
119 style: None,
120 repeat: 1,
121 extra: None,
122 }
123 }
124}
125
126impl CellData {
127 pub(crate) fn is_empty(&self) -> bool {
129 if self.value != Value::Empty {
130 return false;
131 }
132 if self.formula.is_some() {
133 return false;
134 }
135 self.is_void_extra()
137 }
138
139 pub(crate) fn is_void(&self, default_cellstyle: Option<&CellStyleRef>) -> bool {
141 if self.value != Value::Empty {
142 return false;
143 }
144 if self.formula.is_some() {
145 return false;
146 }
147 if self.style.is_some() && self.style.as_ref() != default_cellstyle {
148 return false;
149 }
150 self.is_void_extra()
151 }
152
153 fn is_void_extra(&self) -> bool {
154 if let Some(extra) = &self.extra {
155 if !extra.span.is_empty() {
156 return false;
157 }
158 if extra.validation_name.is_some() {
159 return false;
160 }
161 if extra.annotation.is_some() {
162 return false;
163 }
164 if !extra.draw_frames.is_empty() {
165 return false;
166 }
167 if !extra.matrix_span.is_empty() {
168 return false;
169 }
170 }
171 true
172 }
173
174 pub(crate) fn has_annotation(&self) -> bool {
175 if let Some(extra) = &self.extra {
176 extra.annotation.is_some()
177 } else {
178 false
179 }
180 }
181
182 pub(crate) fn has_draw_frames(&self) -> bool {
183 if let Some(extra) = &self.extra {
184 !extra.draw_frames.is_empty()
185 } else {
186 false
187 }
188 }
189
190 pub(crate) fn extra_mut(&mut self) -> &mut CellDataExt {
191 if self.extra.is_none() {
192 self.extra = Some(Box::default());
193 }
194 self.extra.as_mut().expect("celldataext")
195 }
196
197 pub(crate) fn cloned_cell_content(&self) -> CellContent {
198 let (validation_name, span, matrix_span, annotation, draw_frames) =
199 if let Some(extra) = &self.extra {
200 (
201 extra.validation_name.clone(),
202 extra.span,
203 extra.matrix_span,
204 extra.annotation.clone(),
205 extra.draw_frames.clone(),
206 )
207 } else {
208 (
209 None,
210 Default::default(),
211 Default::default(),
212 None,
213 Vec::new(),
214 )
215 };
216
217 CellContent {
218 value: self.value.clone(),
219 style: self.style.clone(),
220 formula: self.formula.clone(),
221 repeat: self.repeat,
222 validation_name,
223 span,
224 matrix_span,
225 annotation,
226 draw_frames,
227 }
228 }
229
230 pub(crate) fn into_cell_content(self) -> CellContent {
231 let (validation_name, span, matrix_span, annotation, draw_frames) =
232 if let Some(extra) = self.extra {
233 (
234 extra.validation_name,
235 extra.span,
236 extra.matrix_span,
237 extra.annotation,
238 extra.draw_frames,
239 )
240 } else {
241 (
242 None,
243 Default::default(),
244 Default::default(),
245 None,
246 Vec::new(),
247 )
248 };
249
250 CellContent {
251 value: self.value,
252 style: self.style,
253 formula: self.formula,
254 repeat: self.repeat,
255 validation_name,
256 span,
257 matrix_span,
258 annotation,
259 draw_frames,
260 }
261 }
262
263 pub(crate) fn cell_content_ref(&self) -> CellContentRef<'_> {
264 let (validation_name, span, matrix_span, annotation, draw_frames) =
265 if let Some(extra) = &self.extra {
266 (
267 extra.validation_name.as_ref(),
268 extra.span,
269 extra.matrix_span,
270 extra.annotation.as_ref(),
271 Some(&extra.draw_frames),
272 )
273 } else {
274 (None, CellSpan::default(), CellSpan::default(), None, None)
275 };
276
277 CellContentRef {
278 value: &self.value,
279 style: self.style.as_ref(),
280 formula: self.formula.as_ref(),
281 repeat: self.repeat,
282 validation_name,
283 span,
284 matrix_span,
285 annotation: annotation.map(|v| v.as_ref()),
286 draw_frames,
287 }
288 }
289}
290
291#[derive(Debug, Clone, Copy)]
294pub struct CellContentRef<'a> {
295 pub value: &'a Value,
297 pub style: Option<&'a CellStyleRef>,
299 pub formula: Option<&'a String>,
301 pub repeat: u32,
303 pub validation_name: Option<&'a ValidationRef>,
305 pub span: CellSpan,
307 pub matrix_span: CellSpan,
309 pub annotation: Option<&'a Annotation>,
311 pub draw_frames: Option<&'a Vec<DrawFrame>>,
313}
314
315impl<'a> CellContentRef<'a> {
316 #[inline]
318 pub fn value(&self) -> &'a Value {
319 self.value
320 }
321
322 #[inline]
324 pub fn formula(&self) -> Option<&'a String> {
325 self.formula
326 }
327
328 #[inline]
330 pub fn style(&self) -> Option<&'a CellStyleRef> {
331 self.style
332 }
333
334 #[inline]
336 pub fn repeat(&self) -> u32 {
337 self.repeat
338 }
339
340 #[inline]
342 pub fn validation(&self) -> Option<&'a ValidationRef> {
343 self.validation_name
344 }
345
346 #[inline]
348 pub fn row_span(&self) -> u32 {
349 self.span.row_span
350 }
351
352 #[inline]
354 pub fn col_span(&self) -> u32 {
355 self.span.col_span
356 }
357
358 #[inline]
360 pub fn matrix_row_span(&self) -> u32 {
361 self.matrix_span.row_span
362 }
363
364 #[inline]
366 pub fn matrix_col_span(&self) -> u32 {
367 self.matrix_span.col_span
368 }
369
370 #[inline]
372 pub fn annotation(&self) -> Option<&'a Annotation> {
373 self.annotation
374 }
375
376 #[inline]
378 pub fn draw_frames(&self) -> Option<&'a Vec<DrawFrame>> {
379 self.draw_frames
380 }
381
382 pub fn to_owned(&self) -> CellContent {
384 CellContent {
385 value: self.value.clone(),
386 style: self.style.cloned(),
387 formula: self.formula.cloned(),
388 repeat: self.repeat,
389 validation_name: self.validation_name.cloned(),
390 span: self.span,
391 matrix_span: self.matrix_span,
392 annotation: self.annotation.map(|v| Box::new(v.clone())),
393 draw_frames: self.draw_frames.cloned().unwrap_or_default(),
394 }
395 }
396}
397
398#[derive(Debug, Clone, Default)]
400pub struct CellContent {
401 pub value: Value,
403 pub style: Option<CellStyleRef>,
405 pub formula: Option<String>,
407 pub repeat: u32,
409 pub validation_name: Option<ValidationRef>,
411 pub span: CellSpan,
413 pub matrix_span: CellSpan,
415 pub annotation: Option<Box<Annotation>>,
417 pub draw_frames: Vec<DrawFrame>,
419}
420
421impl CellContent {
422 #[inline]
424 pub fn new() -> Self {
425 Default::default()
426 }
427
428 pub(crate) fn into_celldata(mut self) -> CellData {
430 let extra = self.into_celldata_ext();
431 CellData {
432 value: self.value,
433 formula: self.formula,
434 style: self.style,
435 repeat: self.repeat,
436 extra,
437 }
438 }
439
440 #[allow(clippy::wrong_self_convention)]
442 pub(crate) fn into_celldata_ext(&mut self) -> Option<Box<CellDataExt>> {
443 if self.validation_name.is_some()
444 || !self.span.is_empty()
445 || !self.matrix_span.is_empty()
446 || self.annotation.is_some()
447 || !self.draw_frames.is_empty()
448 {
449 Some(Box::new(CellDataExt {
450 validation_name: self.validation_name.take(),
451 span: self.span,
452 matrix_span: self.matrix_span,
453 annotation: self.annotation.take(),
454 draw_frames: std::mem::take(&mut self.draw_frames),
455 }))
456 } else {
457 None
458 }
459 }
460
461 #[inline]
463 pub fn value(&self) -> &Value {
464 &self.value
465 }
466
467 #[inline]
469 pub fn set_value<V: Into<Value>>(&mut self, value: V) {
470 self.value = value.into();
471 }
472
473 #[inline]
475 pub fn formula(&self) -> Option<&String> {
476 self.formula.as_ref()
477 }
478
479 #[inline]
481 pub fn set_formula<V: Into<String>>(&mut self, formula: V) {
482 self.formula = Some(formula.into());
483 }
484
485 #[inline]
487 pub fn clear_formula(&mut self) {
488 self.formula = None;
489 }
490
491 #[inline]
493 pub fn style(&self) -> Option<&CellStyleRef> {
494 self.style.as_ref()
495 }
496
497 #[inline]
499 pub fn set_style(&mut self, style: &CellStyleRef) {
500 self.style = Some(style.clone());
501 }
502
503 #[inline]
505 pub fn clear_style(&mut self) {
506 self.style = None;
507 }
508
509 #[inline]
512 pub fn set_repeat(&mut self, repeat: u32) {
513 assert!(repeat > 0);
514 self.repeat = repeat;
515 }
516
517 #[inline]
519 pub fn get_repeat(&mut self) -> u32 {
520 self.repeat
521 }
522
523 #[inline]
525 pub fn validation(&self) -> Option<&ValidationRef> {
526 self.validation_name.as_ref()
527 }
528
529 #[inline]
531 pub fn set_validation(&mut self, validation: &ValidationRef) {
532 self.validation_name = Some(validation.clone());
533 }
534
535 #[inline]
537 pub fn clear_validation(&mut self) {
538 self.validation_name = None;
539 }
540
541 #[inline]
544 pub fn set_row_span(&mut self, rows: u32) {
545 assert!(rows > 0);
546 self.span.row_span = rows;
547 }
548
549 #[inline]
551 pub fn row_span(&self) -> u32 {
552 self.span.row_span
553 }
554
555 #[inline]
558 pub fn set_col_span(&mut self, cols: u32) {
559 assert!(cols > 0);
560 self.span.col_span = cols;
561 }
562
563 #[inline]
565 pub fn col_span(&self) -> u32 {
566 self.span.col_span
567 }
568
569 #[inline]
572 pub fn set_matrix_row_span(&mut self, rows: u32) {
573 assert!(rows > 0);
574 self.matrix_span.row_span = rows;
575 }
576
577 #[inline]
579 pub fn matrix_row_span(&self) -> u32 {
580 self.matrix_span.row_span
581 }
582
583 #[inline]
586 pub fn set_matrix_col_span(&mut self, cols: u32) {
587 assert!(cols > 0);
588 self.matrix_span.col_span = cols;
589 }
590
591 #[inline]
593 pub fn matrix_col_span(&self) -> u32 {
594 self.matrix_span.col_span
595 }
596
597 #[inline]
599 pub fn set_annotation(&mut self, annotation: Annotation) {
600 self.annotation = Some(Box::new(annotation));
601 }
602
603 #[inline]
605 pub fn clear_annotation(&mut self) {
606 self.annotation = None;
607 }
608
609 #[inline]
611 pub fn annotation(&self) -> Option<&Annotation> {
612 self.annotation.as_ref().map(|v| v.as_ref())
613 }
614
615 #[inline]
617 pub fn set_draw_frames(&mut self, draw_frames: Vec<DrawFrame>) {
618 self.draw_frames = draw_frames;
619 }
620
621 #[inline]
623 pub fn draw_frames(&self) -> &Vec<DrawFrame> {
624 &self.draw_frames
625 }
626}