use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use std::io::Read;
use crate::excel::XmlReader;
use crate::helper::string_to_int;
use crate::raw::drawing::st_types::STCoordinate;
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxBackDrop {
pub anchor: Option<XlsxAnchor>,
pub norm: Option<XlsxNormalVector>,
pub up: Option<XlsxUpVector>,
}
impl XlsxBackDrop {
pub(crate) fn load(reader: &mut XmlReader<impl Read>) -> anyhow::Result<Self> {
let mut list = Self {
anchor: None,
norm: None,
up: None,
};
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"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"anchor" => {
list.anchor = Some(XlsxAnchor::load(e)?)
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"norm" => {
list.norm = Some(XlsxNormalVector::load(e)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"up" => {
list.up = Some(XlsxUpVector::load(e)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"backdrop" => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(list)
}
}
pub type XlsxNormalVector = XlsxVector;
pub type XlsxUpVector = XlsxVector;
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxVector {
pub dx: Option<STCoordinate>,
pub dy: Option<STCoordinate>,
pub dz: Option<STCoordinate>,
}
impl XlsxVector {
pub(crate) fn load(e: &BytesStart) -> anyhow::Result<Self> {
let attributes = e.attributes();
let mut vector = Self {
dx: None,
dy: None,
dz: 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"dx" => vector.dx = string_to_int(&string_value),
b"dy" => vector.dy = string_to_int(&string_value),
b"dz" => vector.dz = string_to_int(&string_value),
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
Ok(vector)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxAnchor {
pub x: Option<STCoordinate>,
pub y: Option<STCoordinate>,
pub z: Option<STCoordinate>,
}
impl XlsxAnchor {
pub(crate) fn load(e: &BytesStart) -> anyhow::Result<Self> {
let attributes = e.attributes();
let mut anchor = Self {
x: None,
y: None,
z: 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"x" => anchor.x = string_to_int(&string_value),
b"y" => anchor.y = string_to_int(&string_value),
b"z" => anchor.z = string_to_int(&string_value),
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
Ok(anchor)
}
}