use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use std::io::Read;
use crate::{
excel::XmlReader,
helper::{string_to_bool, string_to_int},
};
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxTableStyles {
pub table_style: Option<Vec<XlsxTableStyle>>,
pub default_pivot_style: Option<String>,
pub default_table_style: Option<String>,
}
impl XlsxTableStyles {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let mut table_styles = Self {
table_style: None,
default_pivot_style: None,
default_table_style: None,
};
let mut table_style: Vec<XlsxTableStyle> = vec![];
let attributes = e.attributes();
for a in attributes {
match a {
Ok(a) => {
let string_value = String::from_utf8(a.value.to_vec())?;
match a.key.local_name().as_ref() {
b"defaultPivotStyle" => {
table_styles.default_pivot_style = Some(string_value);
}
b"defaultTableStyle" => {
table_styles.default_table_style = Some(string_value);
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
let mut buf = Vec::new();
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"tableStyle" => {
let style = XlsxTableStyle::load(reader, e)?;
table_style.push(style);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"tableStyles" => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
table_styles.table_style = Some(table_style);
Ok(table_styles)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxTableStyle {
pub table_style_element: Option<Vec<TableStyleElement>>,
name: Option<String>,
pivot: Option<bool>,
table: Option<bool>,
}
impl XlsxTableStyle {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let attributes = e.attributes();
let mut style = Self {
table_style_element: None,
name: None,
pivot: None,
table: None,
};
let mut table_style_elements: Vec<TableStyleElement> = vec![];
for a in attributes {
match a {
Ok(a) => {
let string_value = String::from_utf8(a.value.to_vec())?;
match a.key.local_name().as_ref() {
b"name" => {
style.name = Some(string_value);
}
b"pivot" => {
style.pivot = string_to_bool(&string_value);
}
b"table" => {
style.table = string_to_bool(&string_value);
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
let mut buf = Vec::new();
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"tableStyleElement" => {
let style_element = TableStyleElement::load(e)?;
table_style_elements.push(style_element);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"tableStyle" => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
style.table_style_element = Some(table_style_elements);
Ok(style)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TableStyleElement {
pub dxf_id: Option<i64>,
pub size: Option<i64>,
pub r#type: Option<String>,
}
impl TableStyleElement {
pub(crate) fn load(e: &BytesStart) -> anyhow::Result<Self> {
let attributes = e.attributes();
let mut style = Self {
dxf_id: None,
size: None,
r#type: None,
};
for a in attributes {
match a {
Ok(a) => {
let string_value = String::from_utf8(a.value.to_vec())?;
match a.key.local_name().as_ref() {
b"dxfId" => style.dxf_id = string_to_int(&string_value),
b"size" => style.size = string_to_int(&string_value),
b"type" => style.r#type = Some(string_value),
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
Ok(style)
}
}