use std::io::Read;
use anyhow::bail;
use quick_xml::events::Event;
use crate::{
excel::XmlReader,
raw::drawing::text::{text_field::XlsxTextField, text_run::XlsxTextRun},
};
use super::{
super::default_text_run_properties::{
load_end_paragraph_run_properties, XlsxEndParagraphRunProperties,
},
line_break::XlsxTextLineBreak,
paragraph_properties::{load_text_paragraph_properties, XlsxTextParagraphProperties},
};
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxTextParagraphs {
pub end_paragraph_run_properties: Option<XlsxEndParagraphRunProperties>,
pub paragraph_properties: Option<Box<XlsxTextParagraphProperties>>,
pub runs: Option<Vec<XlsxRunType>>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum XlsxRunType {
Text(XlsxTextRun),
LineBreak(XlsxTextLineBreak),
TextField(XlsxTextField),
}
impl XlsxTextParagraphs {
pub(crate) fn load(reader: &mut XmlReader<impl Read>) -> anyhow::Result<Self> {
let mut field = Self {
paragraph_properties: None,
end_paragraph_run_properties: None,
runs: None,
};
let mut runs: Vec<XlsxRunType> = 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"br" => {
let br = XlsxTextLineBreak::load(reader)?;
runs.push(XlsxRunType::LineBreak(br));
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"endParaRPr" => {
field.end_paragraph_run_properties =
Some(load_end_paragraph_run_properties(reader, e)?);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"fld" => {
runs.push(XlsxRunType::TextField(XlsxTextField::load(reader, e)?));
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"pPr" => {
field.paragraph_properties =
Some(Box::new(load_text_paragraph_properties(reader, e)?));
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"r" => {
runs.push(XlsxRunType::Text(XlsxTextRun::load(reader)?));
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"p" => break,
Ok(Event::Eof) => bail!("unexpected end of file at `p`."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
field.runs = Some(runs);
return Ok(field);
}
}