use std::io::Read;
use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use crate::excel::XmlReader;
use crate::raw::drawing::{
effect::effect_style_list::{load_effect_style_list, XlsxEffectStyleList},
fill::{
load_bg_fill_style_lst, load_fill_style_lst, XlsxBackgroundFillStyleList, XlsxFillStyleList,
},
line::{load_line_style_list, XlsxLineStyleList},
};
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxFormatScheme {
pub name: Option<String>,
pub fill_style_lst: Option<XlsxFillStyleList>,
pub bg_fill_style_lst: Option<XlsxBackgroundFillStyleList>,
pub effect_style_lst: Option<XlsxEffectStyleList>,
pub line_style_lst: Option<XlsxLineStyleList>,
}
impl XlsxFormatScheme {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let mut buf = Vec::new();
let mut scheme = Self {
name: None,
fill_style_lst: None,
bg_fill_style_lst: None,
effect_style_lst: None,
line_style_lst: None,
};
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"name" => {
scheme.name = Some(string_value);
break;
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"fillStyleLst" => {
scheme.fill_style_lst = Some(load_fill_style_lst(reader)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"bgFillStyleLst" => {
scheme.bg_fill_style_lst = Some(load_bg_fill_style_lst(reader)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"effectStyleLst" => {
let style_list = load_effect_style_list(reader)?;
scheme.effect_style_lst = Some(style_list);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"lnStyleLst" => {
let style_list = load_line_style_list(reader)?;
scheme.line_style_lst = Some(style_list);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"fmtScheme" => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(scheme)
}
}