excel_cli/excel/
cell.rs

1#[derive(Clone)]
2pub struct Cell {
3    pub value: String,
4    pub is_formula: bool,
5    pub cell_type: CellType,
6    pub original_type: Option<DataTypeInfo>,
7}
8
9#[derive(Clone, PartialEq)]
10pub enum CellType {
11    Text,
12    Number,
13    Date,
14    Boolean,
15    Empty,
16}
17
18#[derive(Clone, PartialEq)]
19pub enum DataTypeInfo {
20    Empty,
21    String,
22    Float(f64),
23    Int(i64),
24    Bool(bool),
25    DateTime(f64),
26    Duration(f64),
27    DateTimeIso(String),
28    DurationIso(String),
29    Error,
30}
31
32impl Cell {
33    pub fn new(value: String, is_formula: bool) -> Self {
34        let cell_type = if value.is_empty() {
35            CellType::Empty
36        } else if is_formula {
37            CellType::Text
38        } else if value.parse::<f64>().is_ok() {
39            CellType::Number
40        } else if (value.contains('/') && value.split('/').count() == 3)
41            || (value.contains('-') && value.split('-').count() == 3)
42        {
43            CellType::Date
44        } else if value == "true" || value == "false" {
45            CellType::Boolean
46        } else {
47            CellType::Text
48        };
49
50        Self::new_with_type(value, is_formula, cell_type, None)
51    }
52
53    pub fn new_with_type(
54        value: String,
55        is_formula: bool,
56        cell_type: CellType,
57        original_type: Option<DataTypeInfo>,
58    ) -> Self {
59        Self {
60            value,
61            is_formula,
62            cell_type,
63            original_type,
64        }
65    }
66
67    pub fn empty() -> Self {
68        Self {
69            value: String::new(),
70            is_formula: false,
71            cell_type: CellType::Empty,
72            original_type: Some(DataTypeInfo::Empty),
73        }
74    }
75}