use std::io::Read;
use anyhow::bail;
use quick_xml::events::Event;
use super::paragraph::{text_list_style::XlsxTextListStyle, text_paragraphs::XlsxTextParagraphs};
use crate::{excel::XmlReader, raw::drawing::text::body_properties::XlsxBodyProperties};
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxShapeTextBody {
pub body_properties: Option<XlsxBodyProperties>,
pub text_list_style: Option<Box<XlsxTextListStyle>>,
pub text_paragraph: Option<Vec<XlsxTextParagraphs>>,
}
impl XlsxShapeTextBody {
pub(crate) fn load(reader: &mut XmlReader<impl Read>) -> anyhow::Result<Self> {
let mut text_body = Self {
body_properties: None,
text_list_style: None,
text_paragraph: None,
};
let mut paragraphs: Vec<XlsxTextParagraphs> = 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"bodyPr" => {
text_body.body_properties = Some(XlsxBodyProperties::load(reader, e)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"lstStyle" => {
text_body.text_list_style = Some(Box::new(XlsxTextListStyle::load(reader)?));
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"p" => {
paragraphs.push(XlsxTextParagraphs::load(reader)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"txBody" => break,
Ok(Event::Eof) => bail!("unexpected end of file at `txBody`."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
text_body.text_paragraph = Some(paragraphs);
return Ok(text_body);
}
}