use std::{io::BufRead, todo};
use displaydoc::Display;
use quick_xml::{
events::{attributes::AttrError, Event},
Reader,
};
use thiserror::Error;
pub mod exact;
#[derive(Debug, Display, Error)]
pub enum XmlError {
Reader(#[from] quick_xml::Error),
XmlAttr(#[from] AttrError),
EofWhileExpecting(&'static str),
ExpectedDecl,
}
pub type Result<T> = std::result::Result<T, XmlError>;
pub fn expect_decl<B: BufRead>(xml: &mut Reader<B>, buf: &mut Vec<u8>) -> Result<()> {
if let Event::Decl(_) = xml.read_event(buf)? {
buf.clear();
Ok(())
} else {
Err(XmlError::ExpectedDecl)
}
}
pub fn expect_named_elem<B: BufRead>(
xml: &mut Reader<B>,
buf: &mut Vec<u8>,
key: &'static str,
parent: Option<&'static str>,
) -> Result<Option<String>> {
match xml.read_event(buf)? {
Event::Start(start) => {
if start.name() == key.as_bytes() {
let mut name = String::new();
for attr in start.attributes() {
let attr = attr?;
if attr.key == b"name" {
name = xml.decode(&attr.value).into_owned();
break;
}
}
buf.clear();
Ok(Some(name))
} else {
todo!();
}
}
Event::End(e) => {
assert_eq!(e.name(), parent.unwrap().as_bytes());
buf.clear();
Ok(None)
}
Event::Eof => Err(XmlError::EofWhileExpecting(key)),
_ => panic!(),
}
}
pub fn expect_elem<B: BufRead>(
xml: &mut Reader<B>,
buf: &mut Vec<u8>,
key: &'static str,
) -> Result<()> {
if let Event::Start(start) = xml.read_event(buf)? {
if start.name() == key.as_bytes() {
buf.clear();
Ok(())
} else {
todo!();
}
} else {
todo!()
}
}
pub fn expect_end<B: BufRead>(
xml: &mut Reader<B>,
buf: &mut Vec<u8>,
key: &'static str,
) -> Result<()> {
if let Event::End(end) = xml.read_event(buf)? {
#[allow(clippy::branches_sharing_code)]
if end.name() == key.as_bytes() {
buf.clear();
Ok(())
} else {
buf.clear();
todo!()
}
} else {
todo!()
}
}