pub mod rules;
use std::collections::BTreeMap;
use crate::styles::borders::Borders;
use crate::styles::colors::Color;
use crate::styles::fills::Fill;
use crate::styles::fonts::Font;
pub use rules::{CellIsRule, ColorScaleRule, FormulaRule, Rule};
pub const RULE_ATTRIBUTES: [&str; 12] = [
"aboveAverage",
"bottom",
"dxfId",
"equalAverage",
"operator",
"percent",
"priority",
"rank",
"stdDev",
"stopIfTrue",
"timePeriod",
"text",
];
pub const ICON_ATTRIBUTES: [&str; 4] = ["iconSet", "showValue", "reverse", "percent"];
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct DataBar {
pub cfvo: Vec<Cfvo>,
pub color: String,
pub show_value: Option<bool>,
pub min_length: Option<u32>,
pub max_length: Option<u32>,
}
impl DataBar {
pub fn new(start: Cfvo, end: Cfvo, color: &str) -> Self {
DataBar {
cfvo: vec![start, end],
color: color.to_string(),
show_value: None,
min_length: None,
max_length: None,
}
}
pub fn without_value(mut self) -> Self {
self.show_value = Some(false);
self
}
pub fn with_lengths(mut self, min: u32, max: u32) -> Self {
self.min_length = Some(min);
self.max_length = Some(max);
self
}
pub fn attributes(&self) -> Vec<(String, String)> {
let mut attrs = Vec::new();
if let Some(show) = self.show_value {
attrs.push(("showValue".to_string(), show.to_string()));
}
if let Some(min) = self.min_length {
attrs.push(("minLength".to_string(), min.to_string()));
}
if let Some(max) = self.max_length {
attrs.push(("maxLength".to_string(), max.to_string()));
}
attrs
}
}
impl Rule {
pub fn icon_set(
icon_style: &str,
values: &[(&str, &str)],
show_value: Option<bool>,
percent: Option<bool>,
reverse: Option<bool>,
) -> Rule {
let flag = |value: Option<bool>| value.map(|v| v.to_string());
Rule {
rule_type: "iconSet".to_string(),
icon_set: Some(IconSet {
cfvo: values
.iter()
.map(|(kind, value)| Cfvo::new(kind, value))
.collect(),
icon_set: Some(icon_style.to_string()),
show_value: flag(show_value),
reverse: flag(reverse),
percent: flag(percent),
}),
..Rule::default()
}
}
pub fn data_bar(
start: (&str, &str),
end: (&str, &str),
color: &str,
show_value: Option<bool>,
min_length: Option<u32>,
max_length: Option<u32>,
) -> Rule {
Rule {
rule_type: "dataBar".to_string(),
data_bar: Some(DataBar {
cfvo: vec![Cfvo::new(start.0, start.1), Cfvo::new(end.0, end.1)],
color: color.to_string(),
show_value,
min_length,
max_length,
}),
..Rule::default()
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Cfvo {
pub cfvo_type: Option<String>,
pub val: Option<String>,
pub gte: Option<bool>,
}
impl Cfvo {
pub fn with_type(cfvo_type: &str) -> Self {
Cfvo {
cfvo_type: Some(cfvo_type.to_string()),
val: None,
gte: None,
}
}
pub fn new(cfvo_type: &str, val: &str) -> Self {
Cfvo {
cfvo_type: Some(cfvo_type.to_string()),
val: Some(val.to_string()),
gte: None,
}
}
pub fn attributes(&self) -> Vec<(String, String)> {
let mut attrs = Vec::new();
if let Some(t) = &self.cfvo_type {
attrs.push(("type".to_string(), t.clone()));
}
if let Some(v) = &self.val {
attrs.push(("val".to_string(), v.clone()));
}
if self.gte == Some(false) {
attrs.push(("gte".to_string(), "0".to_string()));
}
attrs
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ColorScale {
pub cfvo: Vec<Cfvo>,
pub color: Vec<Color>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct IconSet {
pub cfvo: Vec<Cfvo>,
pub icon_set: Option<String>,
pub show_value: Option<String>,
pub reverse: Option<String>,
pub percent: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct DxfStyle {
pub font: Option<Font>,
pub fill: Option<Fill>,
pub border: Option<Borders>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ConditionalFormatting {
pub cf_rules: BTreeMap<String, Vec<Rule>>,
pub max_priority: u32,
pub parse_rules: BTreeMap<String, Vec<Rule>>,
}
impl ConditionalFormatting {
pub fn new() -> Self {
ConditionalFormatting::default()
}
pub fn add(&mut self, range_string: &str, mut rule: Rule) {
self.max_priority += 1;
rule.attributes
.insert("priority".to_string(), self.max_priority.to_string());
self.cf_rules
.entry(range_string.to_string())
.or_default()
.push(rule);
}
pub fn update(&mut self, cf_rules: BTreeMap<String, Vec<Rule>>) {
for (range_string, rules) in cf_rules {
let entry = self.cf_rules.entry(range_string).or_default();
entry.extend(rules);
}
self.max_priority = 0;
let mut priorities: Vec<u32> = self
.cf_rules
.values()
.flatten()
.filter_map(|rule| rule.attributes.get("priority").and_then(|p| p.parse().ok()))
.collect();
priorities.sort_unstable();
for rules in self.cf_rules.values_mut() {
for rule in rules.iter_mut() {
let Some(priority) = rule
.attributes
.get("priority")
.and_then(|p| p.parse::<u32>().ok())
else {
continue;
};
let index = priorities.iter().position(|p| *p == priority).unwrap_or(0);
let new_priority = (index + 1) as u32;
rule.attributes
.insert("priority".to_string(), new_priority.to_string());
if new_priority > self.max_priority {
self.max_priority = new_priority;
}
}
}
}
pub fn collect_dxf_styles(&mut self, dxf_list: &mut Vec<DxfStyle>) {
for rules in self.cf_rules.values_mut() {
for rule in rules.iter_mut() {
let Some(dxf) = rule.dxf.take() else {
continue;
};
let mut filtered = DxfStyle::default();
if dxf.font.is_some() {
filtered.font = dxf.font;
}
if dxf.border.is_some() {
filtered.border = dxf.border;
}
if dxf.fill.is_some() {
filtered.fill = dxf.fill;
}
dxf_list.push(filtered);
let index = dxf_list.len() - 1;
rule.attributes
.insert("dxfId".to_string(), index.to_string());
}
}
}
pub fn all_rules(&self) -> impl Iterator<Item = &Rule> {
self.cf_rules.values().flatten()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct StyleProperties {
pub color_index: Vec<String>,
pub dxf_list: Vec<DxfStyle>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_data_bar_rule_carries_its_thresholds_and_colour() {
let rule = Rule::data_bar(
("min", "0"),
("max", "0"),
"FF638EC6",
Some(true),
Some(10),
Some(90),
);
assert_eq!(rule.rule_type, "dataBar");
assert!(rule.is_data_bar());
let bar = rule.data_bar.expect("a data bar");
assert_eq!(bar.cfvo.len(), 2);
assert_eq!(bar.cfvo[0].cfvo_type.as_deref(), Some("min"));
assert_eq!(bar.cfvo[1].cfvo_type.as_deref(), Some("max"));
assert_eq!(bar.color, "FF638EC6");
assert_eq!(bar.min_length, Some(10));
assert_eq!(bar.max_length, Some(90));
}
#[test]
fn an_icon_set_rule_carries_every_threshold() {
let rule = Rule::icon_set(
"3TrafficLights1",
&[("percent", "0"), ("percent", "33"), ("percent", "67")],
Some(true),
Some(true),
Some(false),
);
assert_eq!(rule.rule_type, "iconSet");
let set = rule.icon_set.expect("an icon set");
assert_eq!(set.cfvo.len(), 3);
assert_eq!(set.icon_set.as_deref(), Some("3TrafficLights1"));
assert_eq!(set.percent.as_deref(), Some("true"));
assert_eq!(set.reverse.as_deref(), Some("false"));
}
#[test]
fn gte_is_only_written_when_it_is_false() {
let inclusive = Cfvo::new("percent", "0");
assert!(inclusive.attributes().iter().all(|(k, _)| k != "gte"));
let exclusive = Cfvo {
gte: Some(false),
..Cfvo::new("percent", "50")
};
assert!(exclusive
.attributes()
.contains(&("gte".to_string(), "0".to_string())));
}
#[test]
fn time_period_is_a_rule_attribute() {
assert!(RULE_ATTRIBUTES.contains(&"timePeriod"));
}
#[test]
fn percent_is_an_icon_set_attribute() {
assert!(ICON_ATTRIBUTES.contains(&"percent"));
}
}