#[derive(Clone, Debug, Default, PartialEq)]
pub struct Color {
pub rgb: Option<String>,
pub theme: Option<u32>,
pub indexed: Option<u32>,
pub tint: Option<f64>,
}
impl Color {
pub fn rgb<S: Into<String>>(rgb: S) -> Self {
Color {
rgb: Some(rgb.into()),
..Default::default()
}
}
pub fn theme(theme: u32) -> Self {
Color {
theme: Some(theme),
..Default::default()
}
}
pub fn indexed(indexed: u32) -> Self {
Color {
indexed: Some(indexed),
..Default::default()
}
}
pub fn with_tint(mut self, tint: f64) -> Self {
self.tint = Some(tint);
self
}
pub fn is_empty(&self) -> bool {
self.rgb.is_none() && self.theme.is_none() && self.indexed.is_none()
}
pub fn argb(&self) -> Option<String> {
let hex = self.rgb.as_deref()?;
let hex = hex.strip_prefix('#').unwrap_or(hex);
Some(if hex.len() >= 8 {
hex.to_string()
} else {
format!("FF{}", hex)
})
}
}
impl<S: AsRef<str>> From<S> for Color {
fn from(value: S) -> Self {
let value = value.as_ref();
if let Some(theme) = value.strip_prefix("theme:") {
if let Ok(theme) = theme.parse() {
return Color::theme(theme);
}
}
if let Some(indexed) = value.strip_prefix("indexed:") {
if let Ok(indexed) = indexed.parse() {
return Color::indexed(indexed);
}
}
Color::rgb(value)
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Font {
pub name: Option<String>,
pub size: Option<f64>,
pub bold: bool,
pub italic: bool,
pub underline: Option<String>,
pub strike: bool,
pub color: Option<Color>,
pub vert_align: Option<String>,
}
impl Font {
pub fn new() -> Self {
Self::default()
}
pub fn with_name<S: Into<String>>(mut self, name: S) -> Self {
self.name = Some(name.into());
self
}
pub fn with_size(mut self, size: f64) -> Self {
self.size = Some(size);
self
}
pub fn with_bold(mut self, bold: bool) -> Self {
self.bold = bold;
self
}
pub fn with_italic(mut self, italic: bool) -> Self {
self.italic = italic;
self
}
pub fn with_underline<S: Into<String>>(mut self, underline: S) -> Self {
self.underline = Some(underline.into());
self
}
pub fn with_strike(mut self, strike: bool) -> Self {
self.strike = strike;
self
}
pub fn with_color<C: Into<Color>>(mut self, color: C) -> Self {
self.color = Some(color.into());
self
}
pub fn with_vert_align<S: Into<String>>(mut self, vert_align: S) -> Self {
self.vert_align = Some(vert_align.into());
self
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Alignment {
pub horizontal: Option<String>,
pub vertical: Option<String>,
pub wrap_text: bool,
pub text_rotation: Option<i32>,
pub indent: Option<u32>,
pub shrink_to_fit: bool,
}
impl Alignment {
pub fn new() -> Self {
Self::default()
}
pub fn with_horizontal<S: Into<String>>(mut self, align: S) -> Self {
self.horizontal = Some(align.into());
self
}
pub fn with_vertical<S: Into<String>>(mut self, align: S) -> Self {
self.vertical = Some(align.into());
self
}
pub fn with_wrap_text(mut self, wrap: bool) -> Self {
self.wrap_text = wrap;
self
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct BorderStyle {
pub style: String,
pub color: Option<Color>,
}
impl BorderStyle {
pub fn new<S: Into<String>>(style: S) -> Self {
BorderStyle {
style: style.into(),
color: None,
}
}
pub fn thin() -> Self {
Self::new("thin")
}
pub fn medium() -> Self {
Self::new("medium")
}
pub fn thick() -> Self {
Self::new("thick")
}
pub fn with_color<C: Into<Color>>(mut self, color: C) -> Self {
self.color = Some(color.into());
self
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Border {
pub left: Option<BorderStyle>,
pub right: Option<BorderStyle>,
pub top: Option<BorderStyle>,
pub bottom: Option<BorderStyle>,
pub diagonal: Option<BorderStyle>,
}
impl Border {
pub fn new() -> Self {
Self::default()
}
pub fn all(style: BorderStyle) -> Self {
Border {
left: Some(style.clone()),
right: Some(style.clone()),
top: Some(style.clone()),
bottom: Some(style),
diagonal: None,
}
}
pub fn with_left(mut self, style: BorderStyle) -> Self {
self.left = Some(style);
self
}
pub fn with_right(mut self, style: BorderStyle) -> Self {
self.right = Some(style);
self
}
pub fn with_top(mut self, style: BorderStyle) -> Self {
self.top = Some(style);
self
}
pub fn with_bottom(mut self, style: BorderStyle) -> Self {
self.bottom = Some(style);
self
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Fill {
pub pattern_type: Option<String>,
pub fg_color: Option<Color>,
pub bg_color: Option<Color>,
}
impl Fill {
pub fn new() -> Self {
Self::default()
}
pub fn solid<C: Into<Color>>(color: C) -> Self {
Fill {
pattern_type: Some("solid".to_string()),
fg_color: Some(color.into()),
bg_color: None,
}
}
pub fn with_pattern<S: Into<String>>(mut self, pattern: S) -> Self {
self.pattern_type = Some(pattern.into());
self
}
pub fn with_fg_color<C: Into<Color>>(mut self, color: C) -> Self {
self.fg_color = Some(color.into());
self
}
pub fn with_bg_color<C: Into<Color>>(mut self, color: C) -> Self {
self.bg_color = Some(color.into());
self
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct GradientStop {
pub position: f64,
pub color: String,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct GradientFill {
pub gradient_type: Option<String>,
pub degree: Option<f64>,
pub left: Option<f64>,
pub right: Option<f64>,
pub top: Option<f64>,
pub bottom: Option<f64>,
pub stops: Vec<GradientStop>,
}
impl GradientFill {
pub fn new() -> Self {
Self::default()
}
pub fn linear<S1: Into<String>, S2: Into<String>>(start_color: S1, end_color: S2) -> Self {
GradientFill {
gradient_type: Some("linear".to_string()),
degree: Some(90.0),
left: None,
right: None,
top: None,
bottom: None,
stops: vec![
GradientStop {
position: 0.0,
color: start_color.into(),
},
GradientStop {
position: 1.0,
color: end_color.into(),
},
],
}
}
pub fn with_type<S: Into<String>>(mut self, gradient_type: S) -> Self {
self.gradient_type = Some(gradient_type.into());
self
}
pub fn with_degree(mut self, degree: f64) -> Self {
self.degree = Some(degree);
self
}
pub fn with_stop<S: Into<String>>(mut self, position: f64, color: S) -> Self {
self.stops.push(GradientStop {
position,
color: color.into(),
});
self
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Protection {
pub locked: bool,
pub hidden: bool,
}
impl Default for Protection {
fn default() -> Self {
Protection {
locked: true,
hidden: false,
}
}
}
impl Protection {
pub fn new() -> Self {
Self::default()
}
pub fn unlocked() -> Self {
Protection {
locked: false,
hidden: false,
}
}
pub fn with_locked(mut self, locked: bool) -> Self {
self.locked = locked;
self
}
pub fn with_hidden(mut self, hidden: bool) -> Self {
self.hidden = hidden;
self
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CellStyle {
pub font: Option<Font>,
pub alignment: Option<Alignment>,
pub border: Option<Border>,
pub fill: Option<Fill>,
pub gradient_fill: Option<GradientFill>,
pub number_format: Option<crate::cell::InternedString>,
pub protection: Option<Protection>,
}
impl CellStyle {
pub fn new() -> Self {
Self::default()
}
pub fn with_font(mut self, font: Font) -> Self {
self.font = Some(font);
self
}
pub fn with_alignment(mut self, alignment: Alignment) -> Self {
self.alignment = Some(alignment);
self
}
pub fn with_border(mut self, border: Border) -> Self {
self.border = Some(border);
self
}
pub fn with_fill(mut self, fill: Fill) -> Self {
self.fill = Some(fill);
self
}
pub fn with_gradient_fill(mut self, gradient_fill: GradientFill) -> Self {
self.gradient_fill = Some(gradient_fill);
self
}
pub fn with_number_format<S: AsRef<str>>(mut self, format: S) -> Self {
self.number_format = Some(std::sync::Arc::from(format.as_ref()));
self
}
pub fn with_protection(mut self, protection: Protection) -> Self {
self.protection = Some(protection);
self
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CellXf {
pub font_id: usize,
pub fill_id: usize,
pub border_id: usize,
pub num_fmt_id: usize,
pub alignment: Option<Alignment>,
pub protection: Option<Protection>,
pub apply_font: bool,
pub apply_fill: bool,
pub apply_border: bool,
pub apply_number_format: bool,
pub apply_alignment: bool,
pub apply_protection: bool,
}
#[derive(Clone, Debug, Default)]
pub struct StyleRegistry {
pub fonts: Vec<Font>,
pub fills: Vec<Fill>,
pub borders: Vec<Border>,
pub num_fmts: Vec<(usize, String)>,
pub cell_xfs: Vec<CellXf>,
pub dxfs: Vec<crate::conditional::ConditionalFormat>,
}
impl StyleRegistry {
pub fn new() -> Self {
let mut registry = StyleRegistry::default();
registry.fonts.push(Font {
name: Some("Calibri".to_string()),
size: Some(11.0),
..Default::default()
});
registry.fills.push(Fill::default()); registry.fills.push(Fill {
pattern_type: Some("gray125".to_string()),
..Default::default()
});
registry.borders.push(Border::default());
registry.cell_xfs.push(CellXf::default());
registry
}
pub fn get_or_add_font(&mut self, font: &Font) -> usize {
if let Some(idx) = self.fonts.iter().position(|f| f == font) {
idx
} else {
let idx = self.fonts.len();
self.fonts.push(font.clone());
idx
}
}
pub fn get_or_add_fill(&mut self, fill: &Fill) -> usize {
if let Some(idx) = self.fills.iter().position(|f| f == fill) {
idx
} else {
let idx = self.fills.len();
self.fills.push(fill.clone());
idx
}
}
pub fn get_or_add_border(&mut self, border: &Border) -> usize {
if let Some(idx) = self.borders.iter().position(|b| b == border) {
idx
} else {
let idx = self.borders.len();
self.borders.push(border.clone());
idx
}
}
pub fn get_or_add_num_fmt(&mut self, format: &str) -> usize {
if let Some(id) = Self::builtin_num_fmt_id(format) {
return id;
}
if let Some((id, _)) = self.num_fmts.iter().find(|(_, f)| f == format) {
return *id;
}
let id = self
.num_fmts
.iter()
.map(|(id, _)| *id + 1)
.max()
.unwrap_or(164)
.max(164);
self.num_fmts.push((id, format.to_string()));
id
}
pub fn builtin_num_fmt_code(id: u32) -> Option<&'static str> {
Some(match id {
0 => "General",
1 => "0",
2 => "0.00",
3 => "#,##0",
4 => "#,##0.00",
9 => "0%",
10 => "0.00%",
11 => "0.00E+00",
12 => "# ?/?",
13 => "# ??/??",
14 => "mm-dd-yy",
15 => "d-mmm-yy",
16 => "d-mmm",
17 => "mmm-yy",
18 => "h:mm AM/PM",
19 => "h:mm:ss AM/PM",
20 => "h:mm",
21 => "h:mm:ss",
22 => "m/d/yy h:mm",
37 => "#,##0 ;(#,##0)",
38 => "#,##0 ;[Red](#,##0)",
39 => "#,##0.00;(#,##0.00)",
40 => "#,##0.00;[Red](#,##0.00)",
45 => "mm:ss",
46 => "[h]:mm:ss",
47 => "mmss.0",
48 => "##0.0E+0",
49 => "@",
_ => return None,
})
}
pub fn builtin_num_fmt_id(format: &str) -> Option<usize> {
match format {
"General" => Some(0),
"0" => Some(1),
"0.00" => Some(2),
"#,##0" => Some(3),
"#,##0.00" => Some(4),
"0%" => Some(9),
"0.00%" => Some(10),
"0.00E+00" => Some(11),
"mm-dd-yy" => Some(14),
"d-mmm-yy" => Some(15),
"d-mmm" => Some(16),
"mmm-yy" => Some(17),
"h:mm AM/PM" => Some(18),
"h:mm:ss AM/PM" => Some(19),
"h:mm" => Some(20),
"h:mm:ss" => Some(21),
"m/d/yy h:mm" => Some(22),
"#,##0 ;(#,##0)" => Some(37),
"#,##0 ;[Red](#,##0)" => Some(38),
"#,##0.00;(#,##0.00)" => Some(39),
"#,##0.00;[Red](#,##0.00)" => Some(40),
"mm:ss" => Some(45),
"[h]:mm:ss" => Some(46),
"mmss.0" => Some(47),
"##0.0E+0" => Some(48),
"@" => Some(49),
_ => None,
}
}
pub fn get_or_add_cell_xf(&mut self, style: &CellStyle) -> usize {
let font_id = style
.font
.as_ref()
.map(|f| self.get_or_add_font(f))
.unwrap_or(0);
let fill_id = style
.fill
.as_ref()
.map(|f| self.get_or_add_fill(f))
.unwrap_or(0);
let border_id = style
.border
.as_ref()
.map(|b| self.get_or_add_border(b))
.unwrap_or(0);
let num_fmt_id = style
.number_format
.as_ref()
.map(|nf| self.get_or_add_num_fmt(nf))
.unwrap_or(0);
let xf = CellXf {
font_id,
fill_id,
border_id,
num_fmt_id,
alignment: style.alignment.clone(),
protection: style.protection.clone(),
apply_font: style.font.is_some(),
apply_fill: style.fill.is_some(),
apply_border: style.border.is_some(),
apply_number_format: style.number_format.is_some(),
apply_alignment: style.alignment.is_some(),
apply_protection: style.protection.is_some(),
};
if let Some(idx) = self.cell_xfs.iter().position(|x| x == &xf) {
idx
} else {
let idx = self.cell_xfs.len();
self.cell_xfs.push(xf);
idx
}
}
pub fn get_cell_style(&self, xf_index: usize) -> Option<CellStyle> {
let xf = self.cell_xfs.get(xf_index)?;
let font = if xf.apply_font && xf.font_id < self.fonts.len() {
Some(self.fonts[xf.font_id].clone())
} else {
None
};
let fill = if xf.apply_fill && xf.fill_id < self.fills.len() {
Some(self.fills[xf.fill_id].clone())
} else {
None
};
let border = if xf.apply_border && xf.border_id < self.borders.len() {
Some(self.borders[xf.border_id].clone())
} else {
None
};
let number_format = if xf.apply_number_format {
self.get_num_fmt_string(xf.num_fmt_id)
} else {
None
};
let protection = if xf.apply_protection {
xf.protection.clone()
} else {
None
};
Some(CellStyle {
font,
alignment: xf.alignment.clone(),
border,
fill,
gradient_fill: None, number_format,
protection,
})
}
fn get_num_fmt_string(&self, id: usize) -> Option<crate::cell::InternedString> {
if let Some((_, fmt)) = self.num_fmts.iter().find(|(i, _)| *i == id) {
return Some(std::sync::Arc::from(fmt.as_str()));
}
Self::builtin_num_fmt_code(id as u32).map(std::sync::Arc::from)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_custom_num_fmt_ids_skip_loaded_ids() {
let mut reg = StyleRegistry::new();
reg.num_fmts.push((165, "yyyy".to_string()));
assert_eq!(reg.get_or_add_num_fmt("0.000%"), 166);
assert_eq!(reg.get_or_add_num_fmt("yyyy"), 165);
assert_eq!(reg.get_or_add_num_fmt("0.00%"), 10);
}
#[test]
fn test_builtin_num_fmt_tables_are_inverse() {
for id in [0u32, 1, 2, 3, 4, 9, 10, 11, 14, 20, 21, 22, 45, 49] {
let code = StyleRegistry::builtin_num_fmt_code(id).unwrap();
assert_eq!(
StyleRegistry::builtin_num_fmt_id(code),
Some(id as usize),
"{}",
code
);
}
}
#[test]
fn test_font_builder() {
let font = Font::new()
.with_name("Arial")
.with_size(12.0)
.with_bold(true)
.with_color("#FF0000");
assert_eq!(font.name, Some("Arial".to_string()));
assert_eq!(font.size, Some(12.0));
assert!(font.bold);
assert_eq!(font.color, Some(Color::rgb("#FF0000")));
}
#[test]
fn test_color_kinds() {
assert_eq!(Color::from("FF0000"), Color::rgb("FF0000"));
assert_eq!(Color::from("theme:3"), Color::theme(3));
assert_eq!(Color::from("indexed:9"), Color::indexed(9));
let tinted = Color::theme(1).with_tint(-0.5);
assert_eq!(tinted.theme, Some(1));
assert_eq!(tinted.tint, Some(-0.5));
assert!(!tinted.is_empty());
assert!(Color::default().is_empty());
assert_eq!(Color::rgb("FF0000").argb().as_deref(), Some("FFFF0000"));
assert_eq!(Color::rgb("#00FF00").argb().as_deref(), Some("FF00FF00"));
assert_eq!(Color::rgb("8000FF00").argb().as_deref(), Some("8000FF00"));
assert_eq!(Color::theme(1).argb(), None);
}
#[test]
fn test_alignment_builder() {
let align = Alignment::new()
.with_horizontal("center")
.with_vertical("top")
.with_wrap_text(true);
assert_eq!(align.horizontal, Some("center".to_string()));
assert_eq!(align.vertical, Some("top".to_string()));
assert!(align.wrap_text);
}
#[test]
fn test_border_all() {
let border = Border::all(BorderStyle::thin());
assert!(border.left.is_some());
assert!(border.right.is_some());
assert!(border.top.is_some());
assert!(border.bottom.is_some());
}
#[test]
fn test_fill_solid() {
let fill = Fill::solid("#FFFF00");
assert_eq!(fill.pattern_type, Some("solid".to_string()));
assert_eq!(fill.fg_color, Some(Color::rgb("#FFFF00")));
}
#[test]
fn test_cell_style_builder() {
let style = CellStyle::new()
.with_font(Font::new().with_bold(true))
.with_alignment(Alignment::new().with_horizontal("center"))
.with_number_format("#,##0.00");
assert!(style.font.is_some());
assert!(style.alignment.is_some());
assert_eq!(style.number_format.as_deref(), Some("#,##0.00"));
}
}
#[cfg(test)]
mod coverage_tests {
use super::*;
#[test]
fn color_constructors_and_argb() {
assert_eq!(Color::rgb("FF0000").rgb.as_deref(), Some("FF0000"));
assert_eq!(Color::theme(4).theme, Some(4));
assert_eq!(Color::indexed(64).indexed, Some(64));
assert_eq!(Color::rgb("00FF00").with_tint(0.5).tint, Some(0.5));
assert!(Color {
rgb: None,
theme: None,
indexed: None,
tint: None
}
.is_empty());
assert!(!Color::rgb("000000").is_empty());
assert_eq!(Color::rgb("#FF0000").argb().as_deref(), Some("FFFF0000"));
assert_eq!(Color::rgb("FF00FF00").argb().as_deref(), Some("FF00FF00"));
assert_eq!(Color::theme(1).argb(), None);
}
#[test]
fn color_from_str_forms() {
assert_eq!(Color::from("theme:3").theme, Some(3));
assert_eq!(Color::from("indexed:9").indexed, Some(9));
assert_eq!(Color::from("ABCDEF").rgb.as_deref(), Some("ABCDEF"));
}
#[test]
fn font_builders() {
let f = Font::new()
.with_name("Arial")
.with_size(12.0)
.with_bold(true)
.with_italic(true)
.with_underline("single")
.with_strike(true)
.with_color("FF0000")
.with_vert_align("superscript");
assert_eq!(f.name.as_deref(), Some("Arial"));
assert_eq!(f.size, Some(12.0));
assert!(f.bold && f.italic && f.strike);
assert_eq!(f.underline.as_deref(), Some("single"));
assert_eq!(f.color.unwrap().rgb.as_deref(), Some("FF0000"));
assert_eq!(f.vert_align.as_deref(), Some("superscript"));
}
#[test]
fn alignment_builders() {
let a = Alignment::new()
.with_horizontal("center")
.with_vertical("top")
.with_wrap_text(true);
assert_eq!(a.horizontal.as_deref(), Some("center"));
assert_eq!(a.vertical.as_deref(), Some("top"));
assert!(a.wrap_text);
}
#[test]
fn border_builders() {
assert_eq!(BorderStyle::thin().style, "thin");
assert_eq!(BorderStyle::medium().style, "medium");
assert_eq!(BorderStyle::thick().style, "thick");
assert_eq!(
BorderStyle::new("dashed")
.with_color("000000")
.color
.unwrap()
.rgb
.as_deref(),
Some("000000")
);
let all = Border::all(BorderStyle::thin());
assert!(
all.left.is_some() && all.right.is_some() && all.top.is_some() && all.bottom.is_some()
);
let b = Border::new()
.with_left(BorderStyle::thin())
.with_right(BorderStyle::medium())
.with_top(BorderStyle::thick())
.with_bottom(BorderStyle::thin());
assert_eq!(b.left.unwrap().style, "thin");
assert_eq!(b.right.unwrap().style, "medium");
assert_eq!(b.top.unwrap().style, "thick");
assert_eq!(b.bottom.unwrap().style, "thin");
}
#[test]
fn fill_builders() {
let solid = Fill::solid("FFFF00");
assert_eq!(solid.pattern_type.as_deref(), Some("solid"));
assert_eq!(solid.fg_color.unwrap().rgb.as_deref(), Some("FFFF00"));
let f = Fill::new()
.with_pattern("gray125")
.with_fg_color("111111")
.with_bg_color("222222");
assert_eq!(f.pattern_type.as_deref(), Some("gray125"));
assert_eq!(f.fg_color.unwrap().rgb.as_deref(), Some("111111"));
assert_eq!(f.bg_color.unwrap().rgb.as_deref(), Some("222222"));
}
#[test]
fn gradient_fill_builders() {
let g = GradientFill::linear("FF0000", "0000FF")
.with_type("linear")
.with_degree(90.0)
.with_stop(0.5, "00FF00");
assert_eq!(g.gradient_type.as_deref(), Some("linear"));
assert_eq!(g.degree, Some(90.0));
assert_eq!(g.stops.len(), 3);
}
#[test]
fn protection_builders() {
assert!(Protection::new().locked);
assert!(!Protection::unlocked().locked);
let p = Protection::new().with_locked(false).with_hidden(true);
assert!(!p.locked && p.hidden);
}
#[test]
fn cell_style_builders() {
let s = CellStyle::new()
.with_font(Font::new().with_bold(true))
.with_alignment(Alignment::new().with_wrap_text(true))
.with_border(Border::all(BorderStyle::thin()))
.with_fill(Fill::solid("EEEEEE"))
.with_gradient_fill(GradientFill::linear("FFF", "000"))
.with_number_format("0.00%")
.with_protection(Protection::unlocked());
assert!(s.font.is_some() && s.alignment.is_some() && s.border.is_some());
assert!(s.fill.is_some() && s.gradient_fill.is_some() && s.protection.is_some());
assert_eq!(s.number_format.as_deref(), Some("0.00%"));
}
#[test]
fn style_registry_dedup_and_num_fmt() {
let mut reg = StyleRegistry::new();
let font = Font::new().with_bold(true);
let a = reg.get_or_add_font(&font);
let b = reg.get_or_add_font(&font);
assert_eq!(a, b, "identical fonts dedup to one index");
let f1 = reg.get_or_add_fill(&Fill::solid("FF0000"));
assert_eq!(f1, reg.get_or_add_fill(&Fill::solid("FF0000")));
let bd = reg.get_or_add_border(&Border::all(BorderStyle::thin()));
assert_eq!(bd, reg.get_or_add_border(&Border::all(BorderStyle::thin())));
assert_eq!(reg.get_or_add_num_fmt("0.00"), 2);
let custom = reg.get_or_add_num_fmt("0.000\"x\"");
assert!(custom >= 164);
assert_eq!(custom, reg.get_or_add_num_fmt("0.000\"x\""));
}
#[test]
fn builtin_num_fmt_roundtrip() {
assert_eq!(StyleRegistry::builtin_num_fmt_code(2), Some("0.00"));
assert_eq!(StyleRegistry::builtin_num_fmt_code(49), Some("@"));
assert_eq!(StyleRegistry::builtin_num_fmt_code(200), None);
assert_eq!(StyleRegistry::builtin_num_fmt_id("0.00"), Some(2));
assert_eq!(StyleRegistry::builtin_num_fmt_id("General"), Some(0));
}
#[test]
fn cell_xf_roundtrip_through_registry() {
let mut reg = StyleRegistry::new();
let style = CellStyle::new()
.with_font(Font::new().with_size(14.0))
.with_number_format("0.00%");
let xf = reg.get_or_add_cell_xf(&style);
let recovered = reg.get_cell_style(xf).expect("xf resolves back to a style");
assert_eq!(recovered.number_format.as_deref(), Some("0.00%"));
assert_eq!(recovered.font.and_then(|f| f.size), Some(14.0));
}
}