use quick_xml::events::Event;
use quick_xml::Reader;
use super::HwpxError;
pub trait XmlNameBytes {
fn xml_name_bytes(&self) -> &[u8];
}
impl XmlNameBytes for str {
fn xml_name_bytes(&self) -> &[u8] {
self.as_bytes()
}
}
impl XmlNameBytes for [u8] {
fn xml_name_bytes(&self) -> &[u8] {
self
}
}
impl<const N: usize> XmlNameBytes for [u8; N] {
fn xml_name_bytes(&self) -> &[u8] {
self
}
}
pub fn local_name<T: XmlNameBytes + ?Sized>(name: &T) -> &[u8] {
let name = name.xml_name_bytes();
if let Some(pos) = name.iter().position(|&b| b == b':') {
&name[pos + 1..]
} else {
name
}
}
pub fn attr_str(attr: &quick_xml::events::attributes::Attribute) -> String {
let raw = attr.value.as_ref();
match quick_xml::escape::unescape(raw) {
Ok(value) => value.into_owned(),
Err(_) => raw.to_owned(),
}
}
pub fn attr_eq(attr: &quick_xml::events::attributes::Attribute, val: &str) -> bool {
attr.value.as_ref() == val
}
pub fn parse_u8(attr: &quick_xml::events::attributes::Attribute) -> u8 {
attr_str(attr).parse().unwrap_or(0)
}
pub fn parse_i8(attr: &quick_xml::events::attributes::Attribute) -> i8 {
attr_str(attr).parse().unwrap_or(0)
}
pub fn parse_u16(attr: &quick_xml::events::attributes::Attribute) -> u16 {
attr_str(attr).parse().unwrap_or(0)
}
pub fn parse_i16(attr: &quick_xml::events::attributes::Attribute) -> i16 {
attr_str(attr).parse().unwrap_or(0)
}
pub fn parse_u32(attr: &quick_xml::events::attributes::Attribute) -> u32 {
attr_str(attr).parse().unwrap_or(0)
}
pub fn parse_i32(attr: &quick_xml::events::attributes::Attribute) -> i32 {
attr_str(attr).parse().unwrap_or(0)
}
pub fn parse_i32_wrapping(attr: &quick_xml::events::attributes::Attribute) -> i32 {
let s = attr_str(attr);
if let Ok(v) = s.parse::<i32>() {
return v;
}
if let Ok(v) = s.parse::<u32>() {
return v as i32;
}
0
}
pub fn parse_color(attr: &quick_xml::events::attributes::Attribute) -> u32 {
let s = attr_str(attr);
parse_color_str(&s)
}
pub fn parse_color_str(s: &str) -> u32 {
if s == "none" || s.is_empty() {
return 0xFFFFFFFF; }
let hex = s.trim_start_matches('#');
if hex.len() == 6 {
if let Ok(v) = u32::from_str_radix(hex, 16) {
let r = (v >> 16) & 0xFF;
let g = (v >> 8) & 0xFF;
let b = v & 0xFF;
return b << 16 | g << 8 | r;
}
} else if hex.len() == 8 {
if let Ok(v) = u32::from_str_radix(hex, 16) {
let a = (v >> 24) & 0xFF;
let r = (v >> 16) & 0xFF;
let g = (v >> 8) & 0xFF;
let b = v & 0xFF;
return a << 24 | b << 16 | g << 8 | r;
}
}
0x00000000 }
pub fn parse_bool(attr: &quick_xml::events::attributes::Attribute) -> bool {
let s = attr_str(attr);
s == "true" || s == "1"
}
pub fn parse_hatch_style(value: &str) -> Option<i32> {
match value {
"HORIZONTAL" => Some(1),
"VERTICAL" => Some(2),
"BACK_SLASH" => Some(3),
"SLASH" => Some(4),
"CROSS" => Some(5),
"CROSS_DIAGONAL" => Some(6),
_ => None,
}
}
pub fn parse_gradient_type(value: &str) -> i16 {
match value {
"LINEAR" => 1,
"RADIAL" => 2,
"CONICAL" => 3,
"SQUARE" => 4,
_ => value.parse().unwrap_or(0),
}
}
pub fn skip_element(reader: &mut Reader<&[u8]>, _end_tag: &[u8]) -> Result<(), HwpxError> {
let mut buf = Vec::new();
let mut depth = 1u32;
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(_)) => {
depth += 1;
}
Ok(Event::End(_)) => {
depth -= 1;
if depth == 0 {
break;
}
}
Ok(Event::Eof) => break,
Err(e) => return Err(HwpxError::XmlError(format!("skip: {}", e))),
_ => {}
}
buf.clear();
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_local_name() {
assert_eq!(local_name(b"hp:p"), b"p");
assert_eq!(local_name(b"tbl"), b"tbl");
assert_eq!(local_name(b"hh:charPr"), b"charPr");
}
#[test]
fn test_parse_color_str() {
assert_eq!(parse_color_str("#FF0000"), 0x000000FF); assert_eq!(parse_color_str("#00FF00"), 0x0000FF00); assert_eq!(parse_color_str("#0000FF"), 0x00FF0000); assert_eq!(parse_color_str("#000000"), 0x00000000); assert_eq!(parse_color_str("none"), 0xFFFFFFFF); }
#[test]
fn test_parse_color_str_with_alpha() {
assert_eq!(parse_color_str("#80FF0000"), 0x800000FF);
assert_eq!(parse_color_str("#FF000000"), 0xFF000000); assert_eq!(parse_color_str("#00FF0000"), 0x000000FF); }
#[test]
fn test_parse_hatch_style() {
assert_eq!(parse_hatch_style("HORIZONTAL"), Some(1));
assert_eq!(parse_hatch_style("VERTICAL"), Some(2));
assert_eq!(parse_hatch_style("BACK_SLASH"), Some(3));
assert_eq!(parse_hatch_style("SLASH"), Some(4));
assert_eq!(parse_hatch_style("CROSS"), Some(5));
assert_eq!(parse_hatch_style("CROSS_DIAGONAL"), Some(6));
assert_eq!(parse_hatch_style(""), None);
}
}