use anyhow::bail;
use quick_xml::events::{BytesStart, Event};
use std::io::Read;
use crate::{excel::XmlReader, helper::string_to_bool};
use super::color::XlsxColor;
pub type XlsxBorders = Vec<XlsxBorder>;
pub(crate) fn load_borders(reader: &mut XmlReader<impl Read>) -> anyhow::Result<XlsxBorders> {
let mut buf: Vec<u8> = Vec::new();
let mut borders: Vec<XlsxBorder> = vec![];
loop {
buf.clear();
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"border" => {
let border = XlsxBorder::load(reader, e)?;
borders.push(border);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"borders" => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(borders)
}
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxBorder {
pub diagonal_down: Option<bool>,
pub diagonal_up: Option<bool>,
pub outline: Option<bool>,
pub left: Option<XlsxLeftBorder>,
pub right: Option<XlsxRightBorder>,
pub top: Option<XlsxTopBorder>,
pub bottom: Option<XlsxBottomBorder>,
pub diagonal: Option<XlsxDiagonalBorder>,
}
impl XlsxBorder {
pub(crate) fn load(reader: &mut XmlReader<impl Read>, e: &BytesStart) -> anyhow::Result<Self> {
let attributes = e.attributes();
let mut border = Self {
diagonal_down: None,
diagonal_up: None,
outline: None,
left: None,
right: None,
top: None,
bottom: None,
diagonal: 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"diagonalDown" => {
border.diagonal_down = string_to_bool(&string_value);
}
b"diagonalUp" => {
border.diagonal_down = string_to_bool(&string_value);
}
b"outline" => {
border.diagonal_down = string_to_bool(&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"left" => {
let border_style = XlsxBorderStyle::load(reader, e, b"left")?;
border.left = Some(border_style);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"start" => {
let border_style = XlsxBorderStyle::load(reader, e, b"start")?;
border.left = Some(border_style);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"right" => {
let border_style = XlsxBorderStyle::load(reader, e, b"right")?;
border.right = Some(border_style);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"end" => {
let border_style = XlsxBorderStyle::load(reader, e, b"end")?;
border.right = Some(border_style);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"top" => {
let border_style = XlsxBorderStyle::load(reader, e, b"top")?;
border.top = Some(border_style);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"bottom" => {
let border_style = XlsxBorderStyle::load(reader, e, b"bottom")?;
border.bottom = Some(border_style);
}
Ok(Event::Start(ref e)) if e.local_name().as_ref() == b"diagonal" => {
let border_style = XlsxBorderStyle::load(reader, e, b"diagonal")?;
border.diagonal = Some(border_style);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == b"border" => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(border)
}
}
pub type XlsxRightBorder = XlsxBorderStyle;
pub type XlsxLeftBorder = XlsxBorderStyle;
pub type XlsxTopBorder = XlsxBorderStyle;
pub type XlsxBottomBorder = XlsxBorderStyle;
pub type XlsxDiagonalBorder = XlsxBorderStyle;
pub type XlsxEndBorder = XlsxBorderStyle;
pub type XlsxStartBorder = XlsxBorderStyle;
pub type XlsxHorizontalBorder = XlsxBorderStyle;
pub type XlsxVerticalBorder = XlsxBorderStyle;
#[derive(Debug, Clone, PartialEq)]
pub struct XlsxBorderStyle {
pub style: Option<String>,
pub color: Option<XlsxColor>,
}
impl XlsxBorderStyle {
pub fn load(
reader: &mut XmlReader<impl Read>,
e: &BytesStart,
tag: &[u8],
) -> anyhow::Result<Self> {
let attributes = e.attributes();
let mut border_style = Self {
color: None,
style: 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"style" => {
border_style.style = Some(string_value);
break;
}
_ => {}
}
}
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"color" => {
let color = XlsxColor::load(e)?;
border_style.color = Some(color);
}
Ok(Event::End(ref e)) if e.local_name().as_ref() == tag => break,
Ok(Event::Eof) => bail!("unexpected end of file."),
Err(e) => bail!(e.to_string()),
_ => (),
}
}
Ok(border_style)
}
}