use std::io::Read;
use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use crate::excel::XmlReader;
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxCellValue {
pub raw_value: String,
pub space: Option<String>,
}
impl XlsxCellValue {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let mut space: Option<String> = 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"space" => {
space = Some(string_value);
break;
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
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"v" => break,
Ok(Event::Eof) => bail!("unexpected end of file at `v`."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(Self {
raw_value: text,
space,
})
}
}