1use serde::{Deserialize, Serialize};
2
3pub fn generate_unique_id() -> u64 {
7 let mut buf = [0u8; 8];
8 let val = if getrandom::getrandom(&mut buf).is_err() {
9 let now = web_time::SystemTime::now()
10 .duration_since(web_time::SystemTime::UNIX_EPOCH)
11 .map(|d| d.as_nanos())
12 .unwrap_or(0);
13 now as u64
14 } else {
15 u64::from_le_bytes(buf)
16 };
17 val & 0x001F_FFFF_FFFF_FFFF
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
22pub enum CellType {
23 #[default]
25 Empty,
26 Int,
28 Float,
30 String,
32 Bool,
34 DateTime,
36 DateTimeIso,
38 DurationIso,
40 Error,
42}
43
44impl CellType {
45 pub fn is_string(&self) -> bool {
47 matches!(self, CellType::String)
48 }
49
50 pub fn as_str(&self) -> &'static str {
52 match self {
53 CellType::Empty => "empty",
54 CellType::Int => "int",
55 CellType::Float => "float",
56 CellType::String => "string",
57 CellType::Bool => "bool",
58 CellType::DateTime => "date-time",
59 CellType::DateTimeIso => "date-time-iso",
60 CellType::DurationIso => "duration-iso",
61 CellType::Error => "error",
62 }
63 }
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
68pub enum RefType {
69 Relative,
72 Absolute,
74}
75
76impl std::fmt::Display for RefType {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 match self {
79 RefType::Relative => write!(f, ""),
80 RefType::Absolute => write!(f, "$"),
81 }
82 }
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
87pub struct CellRef {
88 pub row: usize,
90 pub col: usize,
92 pub row_ref_type: RefType,
94 pub col_ref_type: RefType,
96}
97
98impl std::fmt::Display for CellRef {
99 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100 write!(
101 f,
102 "CELL({}{}, {}{})",
103 self.row_ref_type, self.row, self.col_ref_type, self.col
104 )
105 }
106}
107
108impl CellRef {
109 pub fn new(row: usize, col: usize) -> CellRef {
111 Self {
112 row,
113 col,
114 row_ref_type: RefType::Relative,
115 col_ref_type: RefType::Relative,
116 }
117 }
118}
119
120#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
123pub enum Dependency {
124 Local(CellRef),
126 LocalColumn(usize),
128 Remote {
130 sheet: String,
132 cell: CellRef,
134 },
135 RemoteColumn {
137 sheet: String,
139 col: usize,
141 },
142}
143
144#[derive(Debug, Clone, Default)]
146pub struct TextCellRef {
147 pub row: usize,
149 pub col: usize,
151 pub char_offset: usize,
153}
154
155#[derive(Debug, Clone, PartialEq, Eq)]
157pub enum EvalError {
158 UnknownFunction(String),
160}
161
162impl std::error::Error for EvalError {}
163
164impl std::fmt::Display for EvalError {
165 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
166 match self {
167 EvalError::UnknownFunction(func) => write!(f, "{}", func),
168 }
169 }
170}
171
172#[derive(Debug, Clone, PartialEq, Eq)]
174pub enum EngineError {
175 EvalError(EvalError),
177}
178
179impl std::error::Error for EngineError {
180 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
181 match self {
182 EngineError::EvalError(err) => Some(err),
183 }
184 }
185}
186
187impl std::fmt::Display for EngineError {
188 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
189 match self {
190 EngineError::EvalError(err) => write!(f, "{}", err),
191 }
192 }
193}
194
195impl From<EvalError> for EngineError {
196 fn from(err: EvalError) -> Self {
197 EngineError::EvalError(err)
198 }
199}