xsd_parser/schema/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
//! The `schema` module contains the XML schema types.
pub mod xs;
mod namespace;
mod namespace_prefix;
mod occurs;
mod qname;
use std::borrow::Cow;
use std::collections::btree_map::{BTreeMap, Entry, Iter};
use self::xs::Schema;
pub use namespace::Namespace;
pub use namespace_prefix::NamespacePrefix;
pub use occurs::{MaxOccurs, MinOccurs};
pub use qname::QName;
/// Represents the XML schema information load from different sources.
///
/// This structure is usually created by the [`Parser`](crate::parser::Parser).
/// It is the used by the [`Interpreter`](crate::interpreter::Interpreter) to
/// generate the more common [`Types`](crate::types::Types) structure out of it.
#[derive(Default, Debug)]
pub struct Schemas {
schemas: SchemaFiles,
namespace_infos: NamespaceInfos,
known_prefixes: NamespacePrefixes,
known_namespaces: Namespaces,
next_schema_id: usize,
next_namespace_id: usize,
}
/// Contians the information for a specific namespace.
#[derive(Debug)]
pub struct NamespaceInfo {
/// First used /known prefix of the namespace.
pub prefix: Option<NamespacePrefix>,
/// URI of the namespace.
pub namespace: Namespace,
/// Schema files associated with this namespace.
pub schemas: Vec<SchemaId>,
}
/// Represents an unique id for a XML schema.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct SchemaId(pub usize);
/// Represents a unique id for a XML namespace.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct NamespaceId(pub usize);
/// Map of [`SchemaId`] to [`Schema`]
pub type SchemaFiles = BTreeMap<SchemaId, Schema>;
/// Map of [`NamespaceId`] to [`NamespaceInfo`]
pub type NamespaceInfos = BTreeMap<NamespaceId, NamespaceInfo>;
/// Map of [`Namespace`] to [`NamespaceId`]
pub type Namespaces = BTreeMap<Namespace, NamespaceId>;
/// Map of [`NamespacePrefix`] to [`NamespaceId`]
pub type NamespacePrefixes = BTreeMap<NamespacePrefix, NamespaceId>;
/* Schemas */
impl Schemas {
/// Add a new schema to the schemas structure.
///
/// # Errors
///
/// Will just forward the errors from [`get_or_create_namespace_info_mut`](Self::get_or_create_namespace_info_mut).
pub fn add_schema(
&mut self,
prefix: Option<NamespacePrefix>,
namespace: Namespace,
schema: Schema,
) {
let schema_id = SchemaId(self.next_schema_id);
let schema_info = self.get_or_create_namespace_info_mut(prefix, namespace);
schema_info.schemas.push(schema_id);
self.next_schema_id = self.next_schema_id.wrapping_add(1);
match self.schemas.entry(schema_id) {
Entry::Vacant(e) => e.insert(schema),
Entry::Occupied(_) => unreachable!(),
};
}
/// Get a mutable reference to a [`NamespaceInfo`] or create a new one if needed.
pub fn get_or_create_namespace_info_mut(
&mut self,
prefix: Option<NamespacePrefix>,
namespace: Namespace,
) -> &mut NamespaceInfo {
let (ns, id) = match self.known_namespaces.entry(namespace) {
Entry::Occupied(e) => {
let id = *e.get();
let ns = self.namespace_infos.get_mut(&id).unwrap();
(ns, id)
}
Entry::Vacant(e) => {
let id = NamespaceId(self.next_namespace_id);
self.next_namespace_id = self.next_namespace_id.wrapping_add(1);
let namespace = e.key().clone();
e.insert(id);
let ns = match self.namespace_infos.entry(id) {
Entry::Vacant(e) => e.insert(NamespaceInfo::new(namespace)),
Entry::Occupied(_) => unreachable!(),
};
(ns, id)
}
};
if let Some(mut prefix) = prefix {
if matches!(self.known_prefixes.get(&prefix), Some(x) if *x != id) {
let ext = format!("_{}", id.0);
let ext = ext.as_bytes();
let mut p = prefix.0.into_owned();
p.extend_from_slice(ext);
prefix = NamespacePrefix(Cow::Owned(p));
}
self.known_prefixes.insert(prefix.clone(), id);
if ns.prefix.is_none() {
ns.prefix = Some(prefix);
}
}
ns
}
/// Returns an iterator over all schemas stored in this structure.
pub fn schemas(&self) -> Iter<'_, SchemaId, Schema> {
self.schemas.iter()
}
/// Returns an iterator over all namespace information instances stored
/// in this structure.
pub fn namespaces(&self) -> Iter<'_, NamespaceId, NamespaceInfo> {
self.namespace_infos.iter()
}
/// Returns a reference to a specific schema by using the schema id,
/// or `None` if the schema is not known.
#[must_use]
pub fn get_schema(&self, id: &SchemaId) -> Option<&Schema> {
self.schemas.get(id)
}
/// Returns a reference to a specific namespace information instance by using
/// the namespace id, or `None` if the schema is not known.
#[must_use]
pub fn get_namespace_info(&self, id: &NamespaceId) -> Option<&NamespaceInfo> {
self.namespace_infos.get(id)
}
/// Returns a reference to a specific namespace information instance by using
/// the namespace URI, or `None` if the schema is not known.
#[must_use]
pub fn get_namespace_info_by_namespace(&self, ns: &Namespace) -> Option<&NamespaceInfo> {
let id = self.resolve_namespace(ns)?;
self.get_namespace_info(&id)
}
/// Try to resolve the namespace prefix to a namespace id.
///
/// Returns the namespace id of the given namespace `prefix`, or `None`.
#[must_use]
pub fn resolve_prefix(&self, prefix: &NamespacePrefix) -> Option<NamespaceId> {
Some(*self.known_prefixes.get(prefix)?)
}
/// Try to resolve the namespace to a namespace id.
///
/// Returns the namespace id of the given namespace `ns`, or `None`.
#[must_use]
pub fn resolve_namespace(&self, ns: &Namespace) -> Option<NamespaceId> {
Some(*self.known_namespaces.get(ns)?)
}
}
/* NamespaceInfo */
impl NamespaceInfo {
/// Create a new [`NamespaceInfo`] instance from the passed `namespace`.
#[must_use]
pub fn new(namespace: Namespace) -> Self {
Self {
prefix: None,
namespace,
schemas: Vec::new(),
}
}
}