umya_spreadsheet/structs/
cell_raw_value.rs1use super::RichText;
2use super::Text;
3use crate::CellErrorType;
4use std::fmt;
5
6#[derive(Clone, Debug, PartialEq, PartialOrd, Default)]
7pub enum CellRawValue {
8 String(Box<str>),
9 RichText(RichText),
10 Lazy(Box<str>),
11 Numeric(f64),
12 Bool(bool),
13 Error(CellErrorType),
14 #[default]
15 Empty,
16}
17impl fmt::Display for CellRawValue {
18 #[inline]
19 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20 match self {
21 Self::String(v) => write!(f, "{v}"),
22 Self::RichText(v) => write!(f, "{}", v.get_text()),
23 Self::Numeric(v) => write!(f, "{}", &v),
24 Self::Bool(v) => write!(f, "{}", if *v { "TRUE" } else { "FALSE" }),
25 Self::Error(e) => write!(f, "{e}"),
26 _ => write!(f, ""),
27 }
28 }
29}
30
31impl CellRawValue {
32 #[inline]
33 pub fn get_data_type(&self) -> &str {
34 match self {
35 Self::String(_) => "s",
36 Self::RichText(_) => "s",
37 Self::Numeric(_) => "n",
38 Self::Bool(_) => "b",
39 Self::Error(_) => "e",
40 _ => "",
41 }
42 }
43
44 #[inline]
45 pub(crate) fn get_text(&self) -> Option<Text> {
46 match self {
47 Self::String(_) | Self::Numeric(_) | Self::Bool(_) => {
50 let mut text = Text::default();
51 text.set_value(self.to_string());
52 Some(text)
53 }
54 _ => None,
55 }
56 }
57
58 #[inline]
59 pub(crate) fn get_number(&self) -> Option<f64> {
60 match self {
61 Self::Numeric(number) => Some(*number),
62 _ => None,
63 }
64 }
65
66 #[inline]
67 pub fn get_rich_text(&self) -> Option<RichText> {
68 match self {
69 Self::RichText(v) => Some(v.clone()),
70 _ => None,
71 }
72 }
73
74 #[inline]
75 pub fn is_error(&self) -> bool {
76 matches!(*self, CellRawValue::Error(_))
77 }
78
79 #[inline]
80 pub fn is_empty(&self) -> bool {
81 matches!(*self, CellRawValue::Empty)
82 }
83}