formualizer-workbook 0.7.1

Ergonomic workbook API over the Formualizer engine (sheets, loaders, staging, undo/redo)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use formualizer_common::{LiteralValue, RangeAddress};
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::io::{Read, Write};
use std::path::Path;

#[derive(Clone, Debug)]
pub struct CellData {
    pub value: Option<LiteralValue>,
    pub formula: Option<String>,
    pub style: Option<StyleId>,
}

impl CellData {
    pub fn from_value<V: IntoLiteral>(value: V) -> Self {
        Self {
            value: Some(value.into_literal()),
            formula: None,
            style: None,
        }
    }

    pub fn from_formula(formula: impl Into<String>) -> Self {
        Self {
            value: None,
            formula: Some(formula.into()),
            style: None,
        }
    }
}

/// Local conversion trait so tests and callers can pass primitives directly
pub trait IntoLiteral {
    fn into_literal(self) -> LiteralValue;
}

impl IntoLiteral for LiteralValue {
    fn into_literal(self) -> LiteralValue {
        self
    }
}

impl IntoLiteral for f64 {
    fn into_literal(self) -> LiteralValue {
        LiteralValue::Number(self)
    }
}

impl IntoLiteral for i64 {
    fn into_literal(self) -> LiteralValue {
        LiteralValue::Int(self)
    }
}

impl IntoLiteral for i32 {
    fn into_literal(self) -> LiteralValue {
        LiteralValue::Int(self as i64)
    }
}

impl IntoLiteral for bool {
    fn into_literal(self) -> LiteralValue {
        LiteralValue::Boolean(self)
    }
}

impl IntoLiteral for String {
    fn into_literal(self) -> LiteralValue {
        LiteralValue::Text(self)
    }
}

impl IntoLiteral for &str {
    fn into_literal(self) -> LiteralValue {
        LiteralValue::Text(self.to_string())
    }
}

pub type StyleId = u32;

#[derive(Clone, Debug, Default)]
pub struct BackendCaps {
    pub read: bool,
    pub write: bool,
    pub streaming: bool,
    pub tables: bool,
    pub named_ranges: bool,
    pub formulas: bool,
    pub styles: bool,
    pub lazy_loading: bool,
    pub random_access: bool,
    pub bytes_input: bool,

    // Excel-specific nuances
    pub date_system_1904: bool,
    pub merged_cells: bool,
    pub rich_text: bool,
    pub hyperlinks: bool,
    pub data_validations: bool,
    pub shared_formulas: bool,
}

#[derive(Clone, Debug)]
pub struct SheetData {
    pub cells: BTreeMap<(u32, u32), CellData>,
    pub dimensions: Option<(u32, u32)>,
    pub tables: Vec<TableDefinition>,
    pub named_ranges: Vec<NamedRange>,
    pub date_system_1904: bool,
    pub merged_cells: Vec<MergedRange>,
    pub hidden: bool,
    pub row_hidden_manual: Vec<u32>,
    pub row_hidden_filter: Vec<u32>,
}

#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "json", serde(rename_all = "lowercase"))]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
pub enum NamedRangeScope {
    #[default]
    Workbook,
    Sheet,
}

#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct NamedRange {
    pub name: String,
    #[cfg_attr(feature = "json", serde(default))]
    pub scope: NamedRangeScope,
    pub address: RangeAddress,
}

/// Stable representation of workbook/sheet scoped defined names.
///
/// Stage 1 supports only range-backed and literal-backed names.
#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "json", serde(rename_all = "lowercase"))]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
pub enum DefinedNameScope {
    #[default]
    Workbook,
    Sheet,
}

#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "json", serde(tag = "type", rename_all = "lowercase"))]
#[derive(Clone, Debug, PartialEq)]
pub enum DefinedNameDefinition {
    Range { address: RangeAddress },
    Literal { value: LiteralValue },
}

#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct DefinedName {
    pub name: String,

    #[cfg_attr(feature = "json", serde(default))]
    pub scope: DefinedNameScope,

    /// Sheet name for sheet-scoped names.
    ///
    /// For workbook-scoped names, this must be None.
    #[cfg_attr(
        feature = "json",
        serde(default, skip_serializing_if = "Option::is_none")
    )]
    pub scope_sheet: Option<String>,

    pub definition: DefinedNameDefinition,
}

#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct TableDefinition {
    pub name: String,
    pub range: (u32, u32, u32, u32),
    /// Whether the first row of `range` is a headers row.
    ///
    /// Deterministic resize rule:
    /// - Tables are metadata-only; writing values just below/next to a table does NOT auto-expand
    ///   the table. Callers must explicitly update table metadata (range/flags) if they want a
    ///   resize.
    #[cfg_attr(feature = "json", serde(default = "default_true"))]
    pub header_row: bool,
    pub headers: Vec<String>,
    pub totals_row: bool,
}

#[cfg(feature = "json")]
fn default_true() -> bool {
    true
}

#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct MergedRange {
    pub start_row: u32,
    pub start_col: u32,
    pub end_row: u32,
    pub end_col: u32,
}

impl MergedRange {
    pub fn contains(&self, row: u32, col: u32) -> bool {
        row >= self.start_row && row <= self.end_row && col >= self.start_col && col <= self.end_col
    }
}

#[derive(Clone, Copy, Debug)]
pub enum AccessGranularity {
    Cell,     // Random cell access (mmap)
    Range,    // Range-based access (columnar)
    Sheet,    // Sheet-at-a-time (umya, Calamine)
    Workbook, // All-or-nothing (JSON)
}

#[derive(Clone, Debug)]
pub enum LoadStrategy {
    /// Load entire workbook immediately (small files, testing)
    EagerAll,

    /// Load sheet when first accessed (Calamine, umya default)
    EagerSheet,

    /// Load row/column chunks on access (columnar formats)
    LazyRange { row_chunk: usize, col_chunk: usize },

    /// Load individual cells on access (mmap, remote APIs)
    LazyCell,

    /// Never load - write-only mode
    WriteOnly,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct AdapterLoadStats {
    pub formula_cells_observed: Option<u64>,
    pub value_cells_observed: Option<u64>,
    pub value_slots_handed_to_engine: Option<u64>,
    pub formula_cells_handed_to_engine: Option<u64>,
    pub shared_formula_tags_observed: Option<u64>,
}

/// Workbook-level calculation properties parsed from `xl/workbook.xml`'s
/// `<calcPr .../>` element (spec §9, RFC #113).
///
/// This mirrors the OOXML attributes verbatim — it is a *transport* struct
/// (parsed values, not yet mapped to engine semantics). The mapping to
/// [`formualizer_eval::engine::CycleConfig`] is applied during
/// [`crate::Workbook::from_reader`] (see
/// `CalcSettings::apply_to_cycle_config`), keeping the backend free of engine
/// dependencies.
///
/// `calc_mode` / `full_calc_on_load` are captured for round-trip fidelity only
/// and are not interpreted semantically (spec §9: "out of scope semantically").
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CalcSettings {
    /// `iterate` attribute: `true` when iterative calculation is enabled
    /// (`iterate="1"` or `iterate="true"`).
    pub iterate: bool,
    /// `iterateCount` attribute (Excel default 100 when iterate is on but the
    /// attribute is absent).
    pub iterate_count: Option<u32>,
    /// `iterateDelta` attribute (Excel default 0.001 when iterate is on but the
    /// attribute is absent).
    pub iterate_delta: Option<f64>,
    /// `calcMode` attribute (e.g. "auto", "manual"). Preserved for round-trip
    /// only.
    pub calc_mode: Option<String>,
    /// `fullCalcOnLoad` attribute. Preserved for round-trip only.
    pub full_calc_on_load: Option<bool>,
}

pub trait SpreadsheetReader: Send + Sync {
    type Error: std::error::Error + Send + Sync + 'static;

    fn access_granularity(&self) -> AccessGranularity;
    fn capabilities(&self) -> BackendCaps;
    fn sheet_names(&self) -> Result<Vec<String>, Self::Error>;

    fn load_stats(&self) -> Option<AdapterLoadStats> {
        None
    }

    /// Workbook-level defined names (workbook scoped or sheet scoped).
    ///
    /// Default: no defined names.
    fn defined_names(&mut self) -> Result<Vec<DefinedName>, Self::Error> {
        Ok(Vec::new())
    }

    /// Workbook-level calculation properties (`<calcPr>`), spec §9.
    ///
    /// `None` means the backend does not surface calc settings (no `<calcPr>`
    /// or no support); callers must leave the engine cycle config untouched in
    /// that case. Only the XLSX backends populate this today.
    fn calc_settings(&self) -> Option<CalcSettings> {
        None
    }

    /// Constructor variants for different environments
    fn open_path<P: AsRef<Path>>(path: P) -> Result<Self, Self::Error>
    where
        Self: Sized;

    fn open_reader(reader: Box<dyn Read + Send + Sync>) -> Result<Self, Self::Error>
    where
        Self: Sized;

    fn open_bytes(data: Vec<u8>) -> Result<Self, Self::Error>
    where
        Self: Sized;

    fn read_cell(
        &mut self,
        sheet: &str,
        row: u32,
        col: u32,
    ) -> Result<Option<CellData>, Self::Error> {
        // Default: fallback to range read
        let mut range = self.read_range(sheet, (row, col), (row, col))?;
        Ok(range.remove(&(row, col)))
    }

    fn read_range(
        &mut self,
        sheet: &str,
        start: (u32, u32),
        end: (u32, u32),
    ) -> Result<BTreeMap<(u32, u32), CellData>, Self::Error>;

    fn read_sheet(&mut self, sheet: &str) -> Result<SheetData, Self::Error>;

    fn sheet_bounds(&self, sheet: &str) -> Option<(u32, u32)>;
    fn is_loaded(&self, sheet: &str, row: Option<u32>, col: Option<u32>) -> bool;
}

pub trait SpreadsheetWriter: Send + Sync {
    type Error: std::error::Error + Send + Sync + 'static;

    fn write_cell(
        &mut self,
        sheet: &str,
        row: u32,
        col: u32,
        data: CellData,
    ) -> Result<(), Self::Error>;

    fn write_range(
        &mut self,
        sheet: &str,
        cells: BTreeMap<(u32, u32), CellData>,
    ) -> Result<(), Self::Error>;

    fn clear_range(
        &mut self,
        sheet: &str,
        start: (u32, u32),
        end: (u32, u32),
    ) -> Result<(), Self::Error>;

    fn create_sheet(&mut self, name: &str) -> Result<(), Self::Error>;
    fn delete_sheet(&mut self, name: &str) -> Result<(), Self::Error>;
    fn rename_sheet(&mut self, old: &str, new: &str) -> Result<(), Self::Error>;

    fn flush(&mut self) -> Result<(), Self::Error>;
    fn save(&mut self) -> Result<(), Self::Error> {
        self.save_to(SaveDestination::InPlace).map(|_| ())
    }

    /// Advanced save: specify destination (in place, path, writer, or bytes in memory).
    /// Returns Ok(Some(bytes)) only for Bytes destination, else Ok(None).
    fn save_to<'a>(&mut self, dest: SaveDestination<'a>) -> Result<Option<Vec<u8>>, Self::Error> {
        let _ = dest;
        unreachable!("save_to must be implemented by writer backends that expose persistence");
    }

    fn save_as_path<P: AsRef<std::path::Path>>(&mut self, path: P) -> Result<(), Self::Error> {
        self.save_to(SaveDestination::Path(path.as_ref()))
            .map(|_| ())
    }

    fn save_to_bytes(&mut self) -> Result<Vec<u8>, Self::Error> {
        self.save_to(SaveDestination::Bytes)
            .map(|opt| opt.unwrap_or_default())
    }

    fn write_to<W: Write>(&mut self, writer: &mut W) -> Result<(), Self::Error> {
        self.save_to(SaveDestination::Writer(writer)).map(|_| ())
    }
}

/// Enum describing where a workbook should be saved.
pub enum SaveDestination<'a> {
    InPlace,                   // Use original path, if known
    Path(&'a std::path::Path), // Write to provided filesystem path
    Writer(&'a mut dyn Write), // Stream to arbitrary writer
    Bytes,                     // Return bytes in memory
}

pub trait SpreadsheetIO: SpreadsheetReader + SpreadsheetWriter {}