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_connection_shape_properties::XlsxNonVisualConnectionShapeProperties,
};
use super::{shape_properties::XlsxShapeProperties, shape_style::XlsxShapeStyle};
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxConnectionShape {
pub non_visual_connection_shape_properties: Option<XlsxNonVisualConnectionShapeProperties>,
pub shape_properties: Option<XlsxShapeProperties>,
pub shape_style: Option<XlsxShapeStyle>,
pub r#macro: Option<String>,
pub published: Option<bool>,
}
impl XlsxConnectionShape {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let mut connection_shape = Self {
non_visual_connection_shape_properties: None,
shape_properties: None,
shape_style: None,
r#macro: None,
published: 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"macro" => {
connection_shape.r#macro = Some(string_value);
break;
}
b"fPublished" => {
connection_shape.published = string_to_bool(&string_value);
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
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"nvCxnSpPr" => {
connection_shape.non_visual_connection_shape_properties =
Some(XlsxNonVisualConnectionShapeProperties::load(reader)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"spPr" => {
connection_shape.shape_properties = Some(XlsxShapeProperties::load(reader, e)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"style" => {
connection_shape.shape_style = Some(XlsxShapeStyle::load(reader)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"cxnSp" => break,
Ok(Event::Eof) => bail!("unexpected end of file at XlsxConnectionShape: `cxnSp`."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(connection_shape)
}
}