use std::io::Read;
use anyhow::bail;
use quick_xml::events::Event;
use crate::{
excel::XmlReader,
raw::drawing::{
graphic::graphic_frame::XlsxGraphicFrame,
image::picture::XlsxPicture,
non_visual_properties::non_visual_group_shape_properties::XlsxNonVisualGroupShapeProperties,
shape::{
connection_shape::XlsxConnectionShape,
visual_group_shape_properties::XlsxVisualGroupShapeProperties,
},
worksheet_drawing::{
drawing_content_type::XlsxWorksheetDrawingContentType, spreadsheet_shape::XlsxShape,
},
},
};
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxGroupShape {
pub visual_group_shape_properties: Option<XlsxVisualGroupShapeProperties>,
pub non_visual_group_shape_properties: Option<XlsxNonVisualGroupShapeProperties>,
pub drawing_contents: Option<Box<Vec<XlsxWorksheetDrawingContentType>>>,
}
impl XlsxGroupShape {
pub(crate) fn load(reader: &mut XmlReader<impl Read>) -> anyhow::Result<Self> {
let mut shape = Self {
visual_group_shape_properties: None,
non_visual_group_shape_properties: None,
drawing_contents: None,
};
let mut shapes: Vec<XlsxWorksheetDrawingContentType> = vec![];
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"cxnSp" => {
shapes.push(XlsxWorksheetDrawingContentType::ConnectionShape(
XlsxConnectionShape::load(reader, e)?,
));
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"grpSp" => {
shapes.push(XlsxWorksheetDrawingContentType::GroupShape(
XlsxGroupShape::load(reader)?,
));
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"grpSpPr" => {
shape.visual_group_shape_properties =
Some(XlsxVisualGroupShapeProperties::load(reader, e)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"graphicFrame" => {
shapes.push(XlsxWorksheetDrawingContentType::GraphicFrame(
XlsxGraphicFrame::load(reader, e)?,
));
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"nvGrpSpPr" => {
shape.non_visual_group_shape_properties =
Some(XlsxNonVisualGroupShapeProperties::load(reader)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"pic" => {
shapes.push(XlsxWorksheetDrawingContentType::Picture(XlsxPicture::load(
reader, e,
)?));
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"sp" => {
shapes.push(XlsxWorksheetDrawingContentType::Shape(XlsxShape::load(
reader, e,
)?));
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"grpSp" => break,
Ok(Event::Eof) => bail!("unexpected end of file at `grpSp`."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
shape.drawing_contents = Some(Box::new(shapes));
return Ok(shape);
}
}