use super::scheme::{
color_scheme::XlsxColorScheme, font_scheme::XlsxFontScheme, format_scheme::XlsxFormatScheme,
};
use crate::excel::XmlReader;
use std::io::Read;
use anyhow::bail;
use quick_xml::events::Event;
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxThemeElements {
pub color_scheme: Option<XlsxColorScheme>,
pub format_scheme: Option<XlsxFormatScheme>,
pub font_scheme: Option<XlsxFontScheme>,
}
impl XlsxThemeElements {
pub(crate) fn load(reader: &mut XmlReader<impl Read>) -> anyhow::Result<Self> {
let mut buf = Vec::new();
let mut scheme = Self {
color_scheme: None,
format_scheme: None,
font_scheme: None,
};
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"extLst" => {
let _ = reader.read_to_end_into(e.to_end().to_owned().name(), &mut Vec::new());
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"clrScheme" => {
let color_scheme = XlsxColorScheme::load(reader, e)?;
scheme.color_scheme = Some(color_scheme);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"fmtScheme" => {
let format_scheme = XlsxFormatScheme::load(reader, e)?;
scheme.format_scheme = Some(format_scheme);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"fontScheme" => {
let font_scheme = XlsxFontScheme::load(reader, e)?;
scheme.font_scheme = Some(font_scheme);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"themeElements" => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(scheme)
}
}