1use crate::draw::{Annotation, DrawFrame};
2use crate::validation::ValidationRef;
3use crate::value_::Value;
4use crate::CellStyleRef;
5use get_size2::GetSize;
6use std::fmt::{Display, Formatter};
7
8#[derive(Debug, Clone, Copy, GetSize)]
10pub struct CellSpan {
11 pub(crate) row_span: u32,
12 pub(crate) col_span: u32,
13}
14
15impl Default for CellSpan {
16 fn default() -> Self {
17 Self {
18 row_span: 1,
19 col_span: 1,
20 }
21 }
22}
23
24impl Display for CellSpan {
25 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26 write!(f, "(+{}+{})", self.row_span, self.col_span)
27 }
28}
29
30impl From<CellSpan> for (u32, u32) {
31 fn from(span: CellSpan) -> Self {
32 (span.row_span, span.col_span)
33 }
34}
35
36impl From<&CellSpan> for (u32, u32) {
37 fn from(span: &CellSpan) -> Self {
38 (span.row_span, span.col_span)
39 }
40}
41
42impl CellSpan {
43 pub fn new() -> Self {
45 Self::default()
46 }
47
48 #[inline]
50 pub fn is_empty(&self) -> bool {
51 self.row_span == 1 && self.col_span == 1
52 }
53
54 #[inline]
57 pub fn set_row_span(&mut self, rows: u32) {
58 assert!(rows > 0);
59 self.row_span = rows;
60 }
61
62 #[inline]
64 pub fn row_span(&self) -> u32 {
65 self.row_span
66 }
67
68 #[inline]
71 pub fn set_col_span(&mut self, cols: u32) {
72 assert!(cols > 0);
73 self.col_span = cols;
74 }
75
76 #[inline]
78 pub fn col_span(&self) -> u32 {
79 self.col_span
80 }
81}
82
83#[derive(Debug, Clone, GetSize)]
85pub(crate) struct CellData {
86 pub(crate) value: Value,
87 pub(crate) formula: Option<String>,
89 pub(crate) style: Option<CellStyleRef>,
91 pub(crate) repeat: u32,
93 pub(crate) extra: Option<Box<CellDataExt>>,
95}
96
97#[derive(Debug, Clone, Default, GetSize)]
99pub(crate) struct CellDataExt {
100 pub(crate) validation_name: Option<ValidationRef>,
102 pub(crate) span: CellSpan,
104 pub(crate) matrix_span: CellSpan,
106 pub(crate) annotation: Option<Box<Annotation>>,
108 pub(crate) draw_frames: Vec<DrawFrame>,
110}
111
112impl Default for CellData {
113 #[inline]
114 fn default() -> Self {
115 Self {
116 value: Default::default(),
117 formula: None,
118 style: None,
119 repeat: 1,
120 extra: None,
121 }
122 }
123}
124
125impl CellData {
126 pub(crate) fn is_empty(&self) -> bool {
128 if self.value != Value::Empty {
129 return false;
130 }
131 if self.formula.is_some() {
132 return false;
133 }
134 self.is_void_extra()
136 }
137
138 pub(crate) fn is_void(&self, default_cellstyle: Option<&CellStyleRef>) -> bool {
140 if self.value != Value::Empty {
141 return false;
142 }
143 if self.formula.is_some() {
144 return false;
145 }
146 if self.style.is_some() && self.style.as_ref() != default_cellstyle {
147 return false;
148 }
149 self.is_void_extra()
150 }
151
152 fn is_void_extra(&self) -> bool {
153 if let Some(extra) = &self.extra {
154 if !extra.span.is_empty() {
155 return false;
156 }
157 if extra.validation_name.is_some() {
158 return false;
159 }
160 if extra.annotation.is_some() {
161 return false;
162 }
163 if !extra.draw_frames.is_empty() {
164 return false;
165 }
166 if !extra.matrix_span.is_empty() {
167 return false;
168 }
169 }
170 true
171 }
172
173 pub(crate) fn has_annotation(&self) -> bool {
174 if let Some(extra) = &self.extra {
175 extra.annotation.is_some()
176 } else {
177 false
178 }
179 }
180
181 pub(crate) fn has_draw_frames(&self) -> bool {
182 if let Some(extra) = &self.extra {
183 !extra.draw_frames.is_empty()
184 } else {
185 false
186 }
187 }
188
189 pub(crate) fn extra_mut(&mut self) -> &mut CellDataExt {
190 if self.extra.is_none() {
191 self.extra = Some(Box::default());
192 }
193 self.extra.as_mut().expect("celldataext")
194 }
195
196 pub(crate) fn cloned_cell_content(&self) -> CellContent {
197 let (validation_name, span, matrix_span, annotation, draw_frames) =
198 if let Some(extra) = &self.extra {
199 (
200 extra.validation_name.clone(),
201 extra.span,
202 extra.matrix_span,
203 extra.annotation.clone(),
204 extra.draw_frames.clone(),
205 )
206 } else {
207 (
208 None,
209 Default::default(),
210 Default::default(),
211 None,
212 Vec::new(),
213 )
214 };
215
216 CellContent {
217 value: self.value.clone(),
218 style: self.style.clone(),
219 formula: self.formula.clone(),
220 repeat: self.repeat,
221 validation_name,
222 span,
223 matrix_span,
224 annotation,
225 draw_frames,
226 }
227 }
228
229 pub(crate) fn into_cell_content(self) -> CellContent {
230 let (validation_name, span, matrix_span, annotation, draw_frames) =
231 if let Some(extra) = self.extra {
232 (
233 extra.validation_name,
234 extra.span,
235 extra.matrix_span,
236 extra.annotation,
237 extra.draw_frames,
238 )
239 } else {
240 (
241 None,
242 Default::default(),
243 Default::default(),
244 None,
245 Vec::new(),
246 )
247 };
248
249 CellContent {
250 value: self.value,
251 style: self.style,
252 formula: self.formula,
253 repeat: self.repeat,
254 validation_name,
255 span,
256 matrix_span,
257 annotation,
258 draw_frames,
259 }
260 }
261
262 pub(crate) fn cell_content_ref(&self) -> CellContentRef<'_> {
263 let (validation_name, span, matrix_span, annotation, draw_frames) =
264 if let Some(extra) = &self.extra {
265 (
266 extra.validation_name.as_ref(),
267 extra.span,
268 extra.matrix_span,
269 extra.annotation.as_ref(),
270 Some(&extra.draw_frames),
271 )
272 } else {
273 (None, CellSpan::default(), CellSpan::default(), None, None)
274 };
275
276 CellContentRef {
277 value: &self.value,
278 style: self.style.as_ref(),
279 formula: self.formula.as_ref(),
280 repeat: self.repeat,
281 validation_name,
282 span,
283 matrix_span,
284 annotation: annotation.map(|v| v.as_ref()),
285 draw_frames,
286 }
287 }
288}
289
290#[derive(Debug, Clone, Copy)]
293pub struct CellContentRef<'a> {
294 pub value: &'a Value,
296 pub style: Option<&'a CellStyleRef>,
298 pub formula: Option<&'a String>,
300 pub repeat: u32,
302 pub validation_name: Option<&'a ValidationRef>,
304 pub span: CellSpan,
306 pub matrix_span: CellSpan,
308 pub annotation: Option<&'a Annotation>,
310 pub draw_frames: Option<&'a Vec<DrawFrame>>,
312}
313
314impl<'a> CellContentRef<'a> {
315 #[inline]
317 pub fn value(&self) -> &'a Value {
318 self.value
319 }
320
321 #[inline]
323 pub fn formula(&self) -> Option<&'a String> {
324 self.formula
325 }
326
327 #[inline]
329 pub fn style(&self) -> Option<&'a CellStyleRef> {
330 self.style
331 }
332
333 #[inline]
335 pub fn repeat(&self) -> u32 {
336 self.repeat
337 }
338
339 #[inline]
341 pub fn validation(&self) -> Option<&'a ValidationRef> {
342 self.validation_name
343 }
344
345 #[inline]
347 pub fn row_span(&self) -> u32 {
348 self.span.row_span
349 }
350
351 #[inline]
353 pub fn col_span(&self) -> u32 {
354 self.span.col_span
355 }
356
357 #[inline]
359 pub fn matrix_row_span(&self) -> u32 {
360 self.matrix_span.row_span
361 }
362
363 #[inline]
365 pub fn matrix_col_span(&self) -> u32 {
366 self.matrix_span.col_span
367 }
368
369 #[inline]
371 pub fn annotation(&self) -> Option<&'a Annotation> {
372 self.annotation
373 }
374
375 #[inline]
377 pub fn draw_frames(&self) -> Option<&'a Vec<DrawFrame>> {
378 self.draw_frames
379 }
380
381 pub fn to_owned(&self) -> CellContent {
383 CellContent {
384 value: self.value.clone(),
385 style: self.style.cloned(),
386 formula: self.formula.cloned(),
387 repeat: self.repeat,
388 validation_name: self.validation_name.cloned(),
389 span: self.span,
390 matrix_span: self.matrix_span,
391 annotation: self.annotation.map(|v| Box::new(v.clone())),
392 draw_frames: self.draw_frames.cloned().unwrap_or_default(),
393 }
394 }
395}
396
397#[derive(Debug, Clone, Default)]
399pub struct CellContent {
400 pub value: Value,
402 pub style: Option<CellStyleRef>,
404 pub formula: Option<String>,
406 pub repeat: u32,
408 pub validation_name: Option<ValidationRef>,
410 pub span: CellSpan,
412 pub matrix_span: CellSpan,
414 pub annotation: Option<Box<Annotation>>,
416 pub draw_frames: Vec<DrawFrame>,
418}
419
420impl CellContent {
421 #[inline]
423 pub fn new() -> Self {
424 Default::default()
425 }
426
427 pub(crate) fn into_celldata(mut self) -> CellData {
429 let extra = self.into_celldata_ext();
430 CellData {
431 value: self.value,
432 formula: self.formula,
433 style: self.style,
434 repeat: self.repeat,
435 extra,
436 }
437 }
438
439 #[allow(clippy::wrong_self_convention)]
441 pub(crate) fn into_celldata_ext(&mut self) -> Option<Box<CellDataExt>> {
442 if self.validation_name.is_some()
443 || !self.span.is_empty()
444 || !self.matrix_span.is_empty()
445 || self.annotation.is_some()
446 || !self.draw_frames.is_empty()
447 {
448 Some(Box::new(CellDataExt {
449 validation_name: self.validation_name.take(),
450 span: self.span,
451 matrix_span: self.matrix_span,
452 annotation: self.annotation.take(),
453 draw_frames: std::mem::take(&mut self.draw_frames),
454 }))
455 } else {
456 None
457 }
458 }
459
460 #[inline]
462 pub fn value(&self) -> &Value {
463 &self.value
464 }
465
466 #[inline]
468 pub fn set_value<V: Into<Value>>(&mut self, value: V) {
469 self.value = value.into();
470 }
471
472 #[inline]
474 pub fn formula(&self) -> Option<&String> {
475 self.formula.as_ref()
476 }
477
478 #[inline]
480 pub fn set_formula<V: Into<String>>(&mut self, formula: V) {
481 self.formula = Some(formula.into());
482 }
483
484 #[inline]
486 pub fn clear_formula(&mut self) {
487 self.formula = None;
488 }
489
490 #[inline]
492 pub fn style(&self) -> Option<&CellStyleRef> {
493 self.style.as_ref()
494 }
495
496 #[inline]
498 pub fn set_style(&mut self, style: &CellStyleRef) {
499 self.style = Some(style.clone());
500 }
501
502 #[inline]
504 pub fn clear_style(&mut self) {
505 self.style = None;
506 }
507
508 #[inline]
511 pub fn set_repeat(&mut self, repeat: u32) {
512 assert!(repeat > 0);
513 self.repeat = repeat;
514 }
515
516 #[inline]
518 pub fn get_repeat(&mut self) -> u32 {
519 self.repeat
520 }
521
522 #[inline]
524 pub fn validation(&self) -> Option<&ValidationRef> {
525 self.validation_name.as_ref()
526 }
527
528 #[inline]
530 pub fn set_validation(&mut self, validation: &ValidationRef) {
531 self.validation_name = Some(validation.clone());
532 }
533
534 #[inline]
536 pub fn clear_validation(&mut self) {
537 self.validation_name = None;
538 }
539
540 #[inline]
543 pub fn set_row_span(&mut self, rows: u32) {
544 assert!(rows > 0);
545 self.span.row_span = rows;
546 }
547
548 #[inline]
550 pub fn row_span(&self) -> u32 {
551 self.span.row_span
552 }
553
554 #[inline]
557 pub fn set_col_span(&mut self, cols: u32) {
558 assert!(cols > 0);
559 self.span.col_span = cols;
560 }
561
562 #[inline]
564 pub fn col_span(&self) -> u32 {
565 self.span.col_span
566 }
567
568 #[inline]
571 pub fn set_matrix_row_span(&mut self, rows: u32) {
572 assert!(rows > 0);
573 self.matrix_span.row_span = rows;
574 }
575
576 #[inline]
578 pub fn matrix_row_span(&self) -> u32 {
579 self.matrix_span.row_span
580 }
581
582 #[inline]
585 pub fn set_matrix_col_span(&mut self, cols: u32) {
586 assert!(cols > 0);
587 self.matrix_span.col_span = cols;
588 }
589
590 #[inline]
592 pub fn matrix_col_span(&self) -> u32 {
593 self.matrix_span.col_span
594 }
595
596 #[inline]
598 pub fn set_annotation(&mut self, annotation: Annotation) {
599 self.annotation = Some(Box::new(annotation));
600 }
601
602 #[inline]
604 pub fn clear_annotation(&mut self) {
605 self.annotation = None;
606 }
607
608 #[inline]
610 pub fn annotation(&self) -> Option<&Annotation> {
611 self.annotation.as_ref().map(|v| v.as_ref())
612 }
613
614 #[inline]
616 pub fn set_draw_frames(&mut self, draw_frames: Vec<DrawFrame>) {
617 self.draw_frames = draw_frames;
618 }
619
620 #[inline]
622 pub fn draw_frames(&self) -> &Vec<DrawFrame> {
623 &self.draw_frames
624 }
625}