use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use std::io::Read;
use crate::excel::XmlReader;
use crate::helper::string_to_unsignedint;
use crate::raw::drawing::st_types::STPositivePercentage;
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxCustomDash {
pub ds: Option<Vec<XlsxDashStop>>,
}
impl XlsxCustomDash {
pub(crate) fn load(reader: &mut XmlReader<impl Read>) -> anyhow::Result<Self> {
let mut buf = Vec::new();
let mut stops: Vec<XlsxDashStop> = vec![];
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"ds" => {
stops.push(XlsxDashStop::load(e)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"custDash" => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(Self { ds: Some(stops) })
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxDashStop {
pub d: Option<STPositivePercentage>,
pub sp: Option<STPositivePercentage>,
}
impl XlsxDashStop {
pub(crate) fn load(e: &BytesStart) -> anyhow::Result<Self> {
let attributes = e.attributes();
let mut dash_stop = Self { d: None, sp: 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"d" => dash_stop.d = string_to_unsignedint(&string_value),
b"sp" => dash_stop.sp = string_to_unsignedint(&string_value),
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
Ok(dash_stop)
}
}