use std::io::Read;
use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use crate::{
excel::XmlReader,
helper::string_to_bool,
raw::drawing::{
non_visual_properties::non_visual_shape_properties::XlsxNonVisualShapeProperties,
shape::{shape_properties::XlsxShapeProperties, shape_style::XlsxShapeStyle},
text::shape_text_body::XlsxShapeTextBody,
},
};
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxShape {
pub non_visual_shape_properties: Option<XlsxNonVisualShapeProperties>,
pub shape_properties: Option<XlsxShapeProperties>,
pub shape_style: Option<XlsxShapeStyle>,
pub text_body: Option<XlsxShapeTextBody>,
pub lock_text: Option<bool>,
pub r#macro: Option<String>,
pub published: Option<bool>,
pub text_link: Option<String>,
}
impl XlsxShape {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let mut shape = Self {
non_visual_shape_properties: None,
shape_properties: None,
shape_style: None,
text_body: None,
lock_text: None,
r#macro: None,
published: None,
text_link: 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"textlink" => {
shape.text_link = Some(string_value);
}
b"fLocksText" => {
shape.lock_text = string_to_bool(&string_value);
}
b"macro" => {
shape.r#macro = Some(string_value);
}
b"fPublished" => {
shape.published = string_to_bool(&string_value);
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
let mut buf: Vec<u8> = 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"nvSpPr" => {
shape.non_visual_shape_properties =
Some(XlsxNonVisualShapeProperties::load(reader)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"spPr" => {
shape.shape_properties = Some(XlsxShapeProperties::load(reader, e)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"style" => {
shape.shape_style = Some(XlsxShapeStyle::load(reader)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"txBody" => {
shape.text_body = Some(XlsxShapeTextBody::load(reader)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"sp" => break,
Ok(Event::Eof) => bail!("unexpected end of file at `sp`."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
return Ok(shape);
}
}