use std::collections::HashMap;
use quick_xml::escape::resolve_predefined_entity;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Encoding {
#[default]
Utf8,
}
#[derive(Debug, Clone, Default)]
pub struct NamespaceContext {
decls: HashMap<Box<str>, Box<str>>,
}
impl NamespaceContext {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, prefix: impl Into<Box<str>>, uri: impl Into<Box<str>>) {
self.decls.insert(prefix.into(), uri.into());
}
pub fn resolve(&self, prefix: &str) -> Option<&str> {
self.decls.get(prefix).map(|s| &**s)
}
}
#[derive(Debug, Clone)]
pub struct Prelude {
pub encoding: Encoding,
pub root_name: Box<str>,
pub namespaces: NamespaceContext,
pub entities: HashMap<Box<str>, Box<str>>,
}
impl Prelude {
pub fn resolve_entity(&self, name: &str) -> Option<&str> {
lookup_entity(&self.entities, name)
}
}
pub(crate) fn lookup_entity<'a>(
entities: &'a HashMap<Box<str>, Box<str>>,
name: &str,
) -> Option<&'a str> {
if let Some(predefined) = resolve_predefined_entity(name) {
return Some(predefined);
}
entities.get(name).map(|s| &**s)
}