forked from lavina/lavina
130 lines
3.8 KiB
Rust
130 lines
3.8 KiB
Rust
use std::ops::Generator;
|
|
use std::pin::Pin;
|
|
|
|
use quick_xml::NsReader;
|
|
use quick_xml::events::Event;
|
|
use quick_xml::name::ResolveResult;
|
|
|
|
use anyhow::Result;
|
|
|
|
mod ignore;
|
|
pub use ignore::Ignore;
|
|
|
|
pub trait FromXml: Sized {
|
|
type P: Parser<Output = Result<Self>>;
|
|
|
|
fn parse() -> Self::P;
|
|
}
|
|
|
|
pub trait ToXml: Sized {
|
|
fn serialize(&self, events: &mut Vec<Event<'static>>);
|
|
}
|
|
|
|
pub trait FromXmlTag: FromXml {
|
|
const NAME: &'static str;
|
|
const NS: &'static str;
|
|
}
|
|
|
|
pub trait Parser: Sized {
|
|
type Output;
|
|
|
|
fn consume<'a>(
|
|
self: Self,
|
|
namespace: ResolveResult,
|
|
event: &Event<'a>,
|
|
) -> Continuation<Self, Self::Output>;
|
|
}
|
|
|
|
impl<T, Out> Parser for T
|
|
where
|
|
T: Generator<(ResolveResult<'static>, &'static Event<'static>), Yield = (), Return = Out>
|
|
+ Unpin,
|
|
{
|
|
type Output = Out;
|
|
|
|
fn consume<'a>(
|
|
mut self: Self,
|
|
namespace: ResolveResult,
|
|
event: &Event<'a>,
|
|
) -> Continuation<Self, Self::Output> {
|
|
let s = Pin::new(&mut self);
|
|
// this is a very rude workaround fixing the fact that rust generators
|
|
// 1. don't support higher-kinded lifetimes (i.e. no `impl for <'a> Generator<Event<'a>>)
|
|
// 2. don't track borrows across yield points and lack thereof
|
|
// implementors of Parser should manually check that inputs are not used across yields
|
|
match s.resume(unsafe { std::mem::transmute((namespace, event)) }) {
|
|
std::ops::GeneratorState::Yielded(()) => Continuation::Continue(self),
|
|
std::ops::GeneratorState::Complete(res) => Continuation::Final(res),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub enum Continuation<Parser, Res> {
|
|
Final(Res),
|
|
Continue(Parser),
|
|
}
|
|
|
|
pub fn parse<T: FromXml>(input: &str) -> Result<T> {
|
|
let mut reader = NsReader::from_reader(input.as_bytes());
|
|
let mut buf = vec![];
|
|
let (ns, event) = reader.read_resolved_event_into(&mut buf)?;
|
|
let mut parser: Continuation<_, std::result::Result<T, anyhow::Error>> = T::parse().consume(ns, &event);
|
|
loop {
|
|
match parser {
|
|
Continuation::Final(res) => break res,
|
|
Continuation::Continue(next) => {
|
|
let (ns, event) = reader.read_resolved_event_into(&mut buf)?;
|
|
parser = next.consume(ns, &event);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
macro_rules! fail_fast {
|
|
($errorable: expr) => {
|
|
match $errorable {
|
|
Ok(i) => i,
|
|
Err(e) => return Continuation::Final(Err(e.into())),
|
|
}
|
|
};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! delegate_parsing {
|
|
($parser: ty, $namespace: expr, $event: expr) => {{
|
|
let mut parser = <$parser as FromXml>::parse().consume($namespace, $event);
|
|
loop {
|
|
match parser {
|
|
Continuation::Final(Ok(res)) => break Ok(res.into()),
|
|
Continuation::Final(Err(err)) => break Err(err),
|
|
Continuation::Continue(p) => {
|
|
let (namespace, event) = yield;
|
|
parser = p.consume(namespace, event);
|
|
}
|
|
}
|
|
}
|
|
}};
|
|
}
|
|
|
|
#[macro_export]
|
|
macro_rules! match_parser {
|
|
($name: expr, $ns: expr, $event: expr; $subtype: ty, $fin: block) => {
|
|
if $name.0 == <$subtype as FromXmlTag>::NAME.as_bytes() && $ns == ResolveResult::Bound(Namespace(<$subtype as FromXmlTag>::NS.as_bytes())) {
|
|
delegate_parsing!($subtype, $ns, $event)
|
|
} else {
|
|
$fin
|
|
}
|
|
};
|
|
($name: expr, $ns: expr, $event: expr; $subtype: ty, $($rest: ty),+, $fin: block) => {
|
|
if $name.0 == <$subtype as FromXmlTag>::NAME.as_bytes() && $ns == ResolveResult::Bound(Namespace(<$subtype as FromXmlTag>::NS.as_bytes())) {
|
|
delegate_parsing!($subtype, $ns, $event)
|
|
} else {
|
|
match_parser!($name, $ns, $event; $($rest),*, $fin)
|
|
}
|
|
};
|
|
}
|
|
|
|
pub use delegate_parsing;
|
|
pub(crate) use fail_fast;
|
|
pub use match_parser;
|