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_graphic_frame_properties::XlsxNonVisualGraphicFrameProperties,
shape::transform_2d::XlsxTransform2D,
},
};
use super::XlsxGraphic;
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxGraphicFrame {
pub graphic: Option<XlsxGraphic>,
pub non_visual_graphic_frame_properties: Option<XlsxNonVisualGraphicFrameProperties>,
pub transform2d: Option<XlsxTransform2D>,
pub r#macro: Option<String>,
pub published: Option<bool>,
}
impl XlsxGraphicFrame {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let mut buf = Vec::new();
let mut properties = Self {
graphic: None,
non_visual_graphic_frame_properties: None,
transform2d: 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" => {
properties.r#macro = Some(string_value);
}
b"fPublished" => {
properties.published = string_to_bool(&string_value);
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"graphic" => {
properties.graphic = Some(XlsxGraphic::load(reader)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"nvGraphicFramePr" => {
properties.non_visual_graphic_frame_properties =
Some(XlsxNonVisualGraphicFrameProperties::load(reader)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"xfrm" => {
properties.transform2d = Some(XlsxTransform2D::load(reader, e)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"graphicFrame" => break,
Ok(Event::Eof) => {
bail!("unexpected end of file at `graphicFrame`.")
}
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(properties)
}
}