ferroxl 0.2.0

ferroxl: read and write Excel 2007 xlsx/xlsm files with an openpyxl-compatible API
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
405
//! Conditional formatting rule constructors (`openpyxl/formatting/rules.py`).

use std::collections::BTreeMap;

use crate::formatting::{ColorScale, DataBar, DxfStyle, IconSet};
use crate::styles::borders::Borders;
use crate::styles::colors::Color;
use crate::styles::fills::Fill;
use crate::styles::fonts::Font;

/// A conditional formatting rule.
///
/// The Python class is a dictionary wrapper restricted to a known key set. Rust models it
/// as a struct with an ordered attribute map plus typed payloads, which serialises the
/// same XML while keeping the data inspectable.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Rule {
    /// The rule type: `expression`, `cellIs`, `colorScale`, `iconSet`, …
    pub rule_type: String,
    /// Rule attributes written to `<cfRule>`, in insertion order.
    pub attributes: BTreeMap<String, String>,
    /// Formula expressions.
    pub formula: Vec<String>,
    /// Colour-scale payload.
    pub color_scale: Option<ColorScale>,
    /// Icon-set payload.
    pub icon_set: Option<IconSet>,
    /// Data-bar payload.
    pub data_bar: Option<DataBar>,
    /// Differential style, replaced by `dxfId` once written.
    pub dxf: Option<DxfStyle>,
}

impl Rule {
    /// Build a rule of the given type.
    pub fn new(rule_type: &str) -> Self {
        Rule {
            rule_type: rule_type.to_string(),
            ..Rule::default()
        }
    }

    /// Set a rule attribute.
    pub fn with_attribute(mut self, key: &str, value: &str) -> Self {
        self.attributes.insert(key.to_string(), value.to_string());
        self
    }

    /// Set the priority attribute.
    pub fn with_priority(self, priority: u32) -> Self {
        self.with_attribute("priority", &priority.to_string())
    }

    /// Set the differential style.
    pub fn with_dxf(mut self, dxf: DxfStyle) -> Self {
        self.dxf = Some(dxf);
        self
    }

    /// Whether this is a data-bar rule.
    ///
    /// Kept because it names the one payload that is not a colour scale or an icon set, and a
    /// caller walking a rule list wants to ask that without matching on a string.
    pub fn is_data_bar(&self) -> bool {
        self.rule_type == "dataBar"
    }
}

/// A conditional formatting rule based on a colour scale.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColorScaleRule {
    /// Start threshold type.
    pub start_type: Option<String>,
    /// Start threshold value.
    pub start_value: Option<String>,
    /// Start colour.
    pub start_color: Option<Color>,
    /// Mid threshold type.
    pub mid_type: Option<String>,
    /// Mid threshold value.
    pub mid_value: Option<String>,
    /// Mid colour.
    pub mid_color: Option<Color>,
    /// End threshold type.
    pub end_type: Option<String>,
    /// End threshold value.
    pub end_value: Option<String>,
    /// End colour.
    pub end_color: Option<Color>,
}

/// The threshold types a colour-scale rule accepts.
pub const COLOR_SCALE_VALID_TYPES: [&str; 6] =
    ["min", "max", "num", "percent", "percentile", "formula"];

impl ColorScaleRule {
    /// Build a rule from its nine components.
    ///
    /// The three thresholds are the rule's whole shape, so they are passed positionally
    /// rather than as a struct, matching openpyxl's `ColorScaleRule` signature.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        start_type: Option<&str>,
        start_value: Option<&str>,
        start_color: Option<Color>,
        mid_type: Option<&str>,
        mid_value: Option<&str>,
        mid_color: Option<Color>,
        end_type: Option<&str>,
        end_value: Option<&str>,
        end_color: Option<Color>,
    ) -> Self {
        ColorScaleRule {
            start_type: start_type.map(|t| t.to_string()),
            start_value: start_value.map(|v| v.to_string()),
            start_color,
            mid_type: mid_type.map(|t| t.to_string()),
            mid_value: mid_value.map(|v| v.to_string()),
            mid_color,
            end_type: end_type.map(|t| t.to_string()),
            end_value: end_value.map(|v| v.to_string()),
            end_color,
        }
    }

    /// The threshold stops, skipping unset positions.
    pub fn cfvo(&self) -> Vec<crate::formatting::Cfvo> {
        let mut vals = Vec::new();
        for (t, v) in [
            (&self.start_type, &self.start_value),
            (&self.mid_type, &self.mid_value),
            (&self.end_type, &self.end_value),
        ] {
            let Some(t) = t else { continue };
            vals.push(match v {
                Some(v) => crate::formatting::Cfvo::new(t, v),
                None => crate::formatting::Cfvo::with_type(t),
            });
        }
        vals
    }

    /// The start, mid and end colours that are set.
    pub fn colors(&self) -> Vec<Color> {
        [
            self.start_color.clone(),
            self.mid_color.clone(),
            self.end_color.clone(),
        ]
        .into_iter()
        .flatten()
        .collect()
    }

    /// Convert to the serialisable rule form.
    pub fn to_rule(&self) -> Rule {
        Rule {
            rule_type: "colorScale".to_string(),
            color_scale: Some(ColorScale {
                cfvo: self.cfvo(),
                color: self.colors(),
            }),
            ..Rule::default()
        }
    }
}

impl Default for ColorScaleRule {
    fn default() -> Self {
        ColorScaleRule::new(None, None, None, None, None, None, None, None, None)
    }
}

/// A conditional formatting rule based on a formula.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct FormulaRule {
    /// The formula(s) to evaluate.
    pub formula: Option<String>,
    /// Whether later rules are skipped when this one matches.
    pub stop_if_true: bool,
    /// Differential font.
    pub font: Option<Font>,
    /// Differential border.
    pub border: Option<Borders>,
    /// Differential fill.
    pub fill: Option<Fill>,
}

impl FormulaRule {
    /// Build a formula rule.
    pub fn new(formula: Option<&str>, stop_if_true: bool) -> Self {
        FormulaRule {
            formula: formula.map(|f| f.to_string()),
            stop_if_true,
            ..FormulaRule::default()
        }
    }

    /// Attach a differential font.
    pub fn with_font(mut self, font: Font) -> Self {
        self.font = Some(font);
        self
    }

    /// Attach a differential fill.
    pub fn with_fill(mut self, fill: Fill) -> Self {
        self.fill = Some(fill);
        self
    }

    /// Attach a differential border.
    pub fn with_border(mut self, border: Borders) -> Self {
        self.border = Some(border);
        self
    }

    /// Convert to the serialisable rule form.
    pub fn to_rule(&self) -> Rule {
        let mut rule = Rule::new("expression");
        rule.formula = self.formula.iter().cloned().collect();
        rule.dxf = Some(DxfStyle {
            font: self.font.clone(),
            border: self.border.clone(),
            fill: self.fill.clone(),
        });
        if self.stop_if_true {
            rule.attributes
                .insert("stopIfTrue".to_string(), "1".to_string());
        }
        rule
    }
}

/// A conditional formatting rule based on cell contents.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CellIsRule {
    /// The comparison operator, in OOXML form.
    pub operator: Option<String>,
    /// The comparison formula.
    pub formula: Option<String>,
    /// Whether later rules are skipped when this one matches.
    pub stop_if_true: bool,
    /// Differential font.
    pub font: Option<Font>,
    /// Differential border.
    pub border: Option<Borders>,
    /// Differential fill.
    pub fill: Option<Fill>,
}

impl CellIsRule {
    /// Build a cell-is rule.
    ///
    /// Symbolic operators (`>`, `>=`, `<`, `<=`, `=`, `==`, `!=`) are expanded to their
    /// OOXML equivalents, as `CellIsRule.operator` does.
    pub fn new(operator: Option<&str>, formula: Option<&str>, stop_if_true: bool) -> Self {
        CellIsRule {
            operator: operator.map(expand_operator).map(|o| o.to_string()),
            formula: formula.map(|f| f.to_string()),
            stop_if_true,
            ..CellIsRule::default()
        }
    }

    /// Attach a differential font.
    pub fn with_font(mut self, font: Font) -> Self {
        self.font = Some(font);
        self
    }

    /// Attach a differential fill.
    pub fn with_fill(mut self, fill: Fill) -> Self {
        self.fill = Some(fill);
        self
    }

    /// Attach a differential border.
    pub fn with_border(mut self, border: Borders) -> Self {
        self.border = Some(border);
        self
    }

    /// Convert to the serialisable rule form.
    pub fn to_rule(&self) -> Rule {
        let mut rule = Rule::new("cellIs");
        if let Some(operator) = &self.operator {
            rule.attributes
                .insert("operator".to_string(), operator.clone());
        }
        rule.formula = self.formula.iter().cloned().collect();
        rule.dxf = Some(DxfStyle {
            font: self.font.clone(),
            border: self.border.clone(),
            fill: self.fill.clone(),
        });
        if self.stop_if_true {
            rule.attributes
                .insert("stopIfTrue".to_string(), "1".to_string());
        }
        rule
    }
}

/// The symbolic → OOXML operator map used by `CellIsRule`.
pub const OPERATOR_EXPANSION: [(&str, &str); 7] = [
    (">", "greaterThan"),
    (">=", "greaterThanOrEqual"),
    ("<", "lessThan"),
    ("<=", "lessThanOrEqual"),
    ("=", "equal"),
    ("==", "equal"),
    ("!=", "notEqual"),
];

fn expand_operator(operator: &str) -> &str {
    OPERATOR_EXPANSION
        .iter()
        .find(|(symbol, _)| *symbol == operator)
        .map(|(_, expanded)| *expanded)
        .unwrap_or(operator)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::styles::colors::Color;

    #[test]
    fn color_scale_rule_shape() {
        let rule = ColorScaleRule::new(
            Some("min"),
            None,
            Some(Color::new("FFFF7128")),
            Some("percentile"),
            Some("50"),
            Some(Color::new("FFFFEF9C")),
            Some("max"),
            None,
            Some(Color::new("FF4F81BD")),
        );
        let cfvo = rule.cfvo();
        assert_eq!(cfvo.len(), 3);
        assert_eq!(cfvo[0].cfvo_type.as_deref(), Some("min"));
        assert_eq!(cfvo[0].val, None);
        assert_eq!(cfvo[1].cfvo_type.as_deref(), Some("percentile"));
        assert_eq!(cfvo[1].val.as_deref(), Some("50"));
        assert_eq!(cfvo[2].cfvo_type.as_deref(), Some("max"));
        assert_eq!(rule.colors().len(), 3);

        let serialised = rule.to_rule();
        assert_eq!(serialised.rule_type, "colorScale");
        let scale = serialised.color_scale.unwrap();
        assert_eq!(scale.cfvo.len(), 3);
        assert_eq!(scale.color.len(), 3);
    }

    #[test]
    fn colour_scale_skips_unset_midpoint() {
        let rule = ColorScaleRule::new(
            Some("min"),
            None,
            Some(Color::new("FFFF7128")),
            None,
            None,
            None,
            Some("max"),
            None,
            Some(Color::new("FF4F81BD")),
        );
        assert_eq!(rule.cfvo().len(), 2);
        assert_eq!(rule.colors().len(), 2);
    }

    #[test]
    fn formula_rule_shape() {
        let rule = FormulaRule::new(Some("A1>5"), true)
            .with_font(Font::new().with_bold(true))
            .to_rule();
        assert_eq!(rule.rule_type, "expression");
        assert_eq!(rule.formula, vec!["A1>5".to_string()]);
        assert_eq!(rule.attributes.get("stopIfTrue").unwrap(), "1");
        assert!(rule.dxf.unwrap().font.is_some());
    }

    #[test]
    fn cell_is_rule_expands_operators() {
        for (symbol, expected) in OPERATOR_EXPANSION {
            let rule = CellIsRule::new(Some(symbol), Some("5"), false).to_rule();
            assert_eq!(rule.attributes.get("operator").unwrap(), expected);
        }
        let passthrough = CellIsRule::new(Some("between"), Some("1"), false).to_rule();
        assert_eq!(passthrough.attributes.get("operator").unwrap(), "between");
    }

    #[test]
    fn rule_defaults_and_helpers() {
        let rule = Rule::new("cellIs")
            .with_priority(3)
            .with_attribute("operator", "equal");
        assert_eq!(rule.rule_type, "cellIs");
        assert_eq!(rule.attributes.get("priority").unwrap(), "3");
        assert!(!rule.is_data_bar());
        assert!(Rule::new("dataBar").is_data_bar());
    }
}