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; 11] = [
"aboveAverage",
"bottom",
"dxfId",
"equalAverage",
"operator",
"percent",
"priority",
"rank",
"stdDev",
"stopIfTrue",
"text",
];
pub const ICON_ATTRIBUTES: [&str; 3] = ["iconSet", "showValue", "reverse"];
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Cfvo {
pub cfvo_type: Option<String>,
pub val: Option<String>,
}
impl Cfvo {
pub fn with_type(cfvo_type: &str) -> Self {
Cfvo {
cfvo_type: Some(cfvo_type.to_string()),
val: None,
}
}
pub fn new(cfvo_type: &str, val: &str) -> Self {
Cfvo {
cfvo_type: Some(cfvo_type.to_string()),
val: Some(val.to_string()),
}
}
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()));
}
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>,
}
#[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>,
}