use std::collections::HashMap;
use crate::automaton::Automaton;
use crate::datatypes::Builtin;
pub(crate) const XS: &str = "http://www.w3.org/2001/XMLSchema";
pub(crate) const XSI: &str = "http://www.w3.org/2001/XMLSchema-instance";
pub(crate) type TypeId = usize;
pub(crate) type ElementId = usize;
pub(crate) const ANY_TYPE: TypeId = 0;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) struct QName {
pub(crate) namespace: Option<String>,
pub(crate) local: String,
}
impl QName {
pub(crate) fn new(namespace: Option<&str>, local: &str) -> Self {
Self {
namespace: namespace.map(str::to_owned),
local: local.to_owned(),
}
}
pub(crate) fn matches(&self, namespace: Option<&str>, local: &str) -> bool {
self.local == local && self.namespace.as_deref() == namespace
}
}
pub(crate) fn namespace_name(namespace: Option<&str>) -> Option<&str> {
namespace.filter(|ns| !ns.is_empty())
}
pub(crate) fn clark(namespace: Option<&str>, local: &str) -> String {
match namespace {
Some(ns) => format!("{{{ns}}}{local}"),
None => local.to_owned(),
}
}
impl std::fmt::Display for QName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&clark(self.namespace.as_deref(), &self.local))
}
}
#[derive(Debug, Clone)]
pub(crate) struct ElementDecl {
pub(crate) name: QName,
pub(crate) type_id: TypeId,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ProcessContents {
Strict,
Lax,
Skip,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum NamespaceConstraint {
Any,
Other(Option<String>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Wildcard {
pub(crate) namespaces: NamespaceConstraint,
pub(crate) process: ProcessContents,
}
impl Wildcard {
pub(crate) fn allows(&self, namespace: Option<&str>) -> bool {
match &self.namespaces {
NamespaceConstraint::Any => true,
NamespaceConstraint::Other(target) => {
namespace.is_some() && namespace != target.as_deref()
}
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct AttributeUse {
pub(crate) name: QName,
pub(crate) type_id: TypeId,
pub(crate) required: bool,
}
#[derive(Debug, Clone)]
pub(crate) enum Content {
Empty,
Simple(TypeId),
ElementOnly(Automaton),
Mixed(Automaton),
}
#[derive(Debug, Clone)]
pub(crate) struct ComplexType {
pub(crate) name: Option<QName>,
pub(crate) base: TypeId,
pub(crate) is_abstract: bool,
pub(crate) content: Content,
pub(crate) attributes: Vec<AttributeUse>,
pub(crate) attribute_wildcard: Option<Wildcard>,
}
#[derive(Debug, Clone)]
pub(crate) struct SimpleType {
pub(crate) name: Option<QName>,
pub(crate) base: TypeId,
pub(crate) builtin: Builtin,
pub(crate) enumeration: Option<Vec<String>>,
}
#[derive(Debug, Clone)]
pub(crate) enum TypeDef {
Simple(SimpleType),
Complex(ComplexType),
}
impl TypeDef {
pub(crate) fn name(&self) -> Option<&QName> {
match self {
Self::Simple(s) => s.name.as_ref(),
Self::Complex(c) => c.name.as_ref(),
}
}
pub(crate) fn base(&self) -> TypeId {
match self {
Self::Simple(s) => s.base,
Self::Complex(c) => c.base,
}
}
}
#[derive(Debug, Clone)]
pub struct Schema {
pub(crate) types: Vec<TypeDef>,
pub(crate) elements: Vec<ElementDecl>,
pub(crate) global_elements: HashMap<Option<String>, HashMap<String, ElementId>>,
pub(crate) global_types: HashMap<Option<String>, HashMap<String, TypeId>>,
pub(crate) documents: Vec<String>,
pub(crate) omitted_imports: Vec<OmittedImport>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OmittedImport {
pub file: String,
pub line: u32,
pub namespace: String,
pub location: String,
}
impl Schema {
pub(crate) fn global_element(&self, namespace: Option<&str>, local: &str) -> Option<ElementId> {
let by_name = self.global_elements.get(&namespace.map(str::to_owned))?;
by_name.get(local).copied()
}
pub(crate) fn global_type(&self, namespace: Option<&str>, local: &str) -> Option<TypeId> {
let by_name = self.global_types.get(&namespace.map(str::to_owned))?;
by_name.get(local).copied()
}
pub(crate) fn is_derived_from(&self, derived: TypeId, base: TypeId) -> bool {
let mut current = derived;
loop {
if current == base {
return true;
}
if current == ANY_TYPE {
return false;
}
current = self.types[current].base();
}
}
#[must_use]
pub fn documents(&self) -> &[String] {
&self.documents
}
#[must_use]
pub fn omitted_imports(&self) -> &[OmittedImport] {
&self.omitted_imports
}
#[must_use]
pub fn global_element_count(&self) -> usize {
self.global_elements.values().map(HashMap::len).sum()
}
#[must_use]
pub fn type_count(&self) -> usize {
self.types.len()
}
}