use anyhow::bail;
use std::io::Read;
use quick_xml::events::{BytesStart, Event};
use crate::{excel::XmlReader, helper::string_to_bool};
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxTotalsRowFormula {
pub formula: String,
pub array: Option<bool>,
}
impl XlsxTotalsRowFormula {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let mut text = String::new();
let mut buf: Vec<u8> = Vec::new();
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Text(t)) => text.push_str(&t.unescape()?),
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"totalsRowFormula" => break,
Ok(Event::Eof) => bail!("unexpected end of file at `totalsRowFormula`."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
let attributes = e.attributes();
let mut formula = Self {
formula: text,
array: None,
};
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"array" => {
formula.array = string_to_bool(&string_value);
break;
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
Ok(formula)
}
}