use std::io::Read;
use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use crate::{
excel::XmlReader,
raw::drawing::{
non_visual_properties::{
application_non_visual_drawing_properties::XlsxApplicationNonVisualDrawingProperties,
excel_non_visual_content_part_shape_properties::XlsxExcelNonVisualContentPartShapeProperties,
},
shape::transform_2d::XlsxTransform2D,
},
};
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxContentPart {
pub application_non_visual_drawing_properties:
Option<XlsxApplicationNonVisualDrawingProperties>,
pub excel_non_visual_content_part_shape_properties:
Option<XlsxExcelNonVisualContentPartShapeProperties>,
pub transform_2d: Option<XlsxTransform2D>,
pub black_white_mode: Option<String>,
pub id: Option<String>,
}
impl XlsxContentPart {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let mut buf = Vec::new();
let mut properties = Self {
application_non_visual_drawing_properties: None,
excel_non_visual_content_part_shape_properties: None,
transform_2d: None,
black_white_mode: None,
id: 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"bwMode" => {
properties.black_white_mode = Some(string_value);
}
b"id" => {
properties.id = Some(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"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"nvPr" => {
properties.application_non_visual_drawing_properties =
Some(XlsxApplicationNonVisualDrawingProperties::load(e)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"nvContentPartPr" => {
properties.excel_non_visual_content_part_shape_properties =
Some(XlsxExcelNonVisualContentPartShapeProperties::load(reader)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"xfrm" => {
properties.transform_2d = Some(XlsxTransform2D::load(reader, e)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"contentPart" => break,
Ok(Event::Eof) => {
bail!("unexpected end of file at XlsxContentPart: `contentPart`.")
}
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(properties)
}
}