use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use std::io::Read;
use crate::excel::XmlReader;
use crate::helper::string_to_bool;
use super::hyperlink_sound::XlsxHyperlinkSound;
pub type XlsxHyperlinkOnMouseOver = XlsxHyperlinkOnEvent;
pub type XlsxHyperlinkOnClick = XlsxHyperlinkOnEvent;
pub type XlsxHyperlinkOnHover = XlsxHyperlinkOnEvent;
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxHyperlinkOnEvent {
pub sound: Option<XlsxHyperlinkSound>,
pub action: Option<String>,
pub end_sound: Option<bool>,
pub highlight_click: Option<bool>,
pub history: Option<bool>,
pub id: Option<String>,
pub invalid_url: Option<String>,
pub target_frame: Option<String>,
pub tooltip: Option<String>,
}
impl XlsxHyperlinkOnEvent {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let mut onclick: Self = Self {
sound: None,
action: None,
end_sound: None,
highlight_click: None,
history: None,
id: None,
invalid_url: None,
target_frame: None,
tooltip: 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"action" => {
onclick.action = Some(string_value);
}
b"endSnd" => {
onclick.end_sound = string_to_bool(&string_value);
}
b"highlightClick" => {
onclick.highlight_click = string_to_bool(&string_value);
}
b"history" => {
onclick.history = string_to_bool(&string_value);
}
b"id" => {
onclick.id = Some(string_value);
}
b"invalidUrl" => {
onclick.invalid_url = Some(string_value);
}
b"tgtFrame" => {
onclick.target_frame = Some(string_value);
}
b"tooltip" => {
onclick.tooltip = Some(string_value);
}
_ => {}
}
}
Err(error) => {
bail!(error.to_string())
}
}
}
let mut buf = 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"snd" => {
onclick.sound = Some(XlsxHyperlinkSound::load(e)?);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"hlinkClick" => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(onclick)
}
}