pub mod custom;
pub mod ident;
pub mod info;
pub mod name;
pub mod type_;
mod helper;
use std::collections::BTreeMap;
use std::collections::HashSet;
use std::ops::Deref;
use std::ops::DerefMut;
pub use self::custom::CustomType;
pub use self::helper::{VecHelper, WithIdent};
pub use self::ident::{Ident, IdentType};
pub use self::info::{
AbstractInfo, AnyAttributeInfo, AnyInfo, AttributeInfo, AttributesInfo, Base, ComplexInfo,
ElementInfo, ElementMode, ElementsInfo, EnumerationInfo, GroupInfo, ReferenceInfo, UnionInfo,
UnionTypeInfo, UnionTypesInfo, VariantInfo,
};
pub use self::name::Name;
pub use self::type_::{BuildInInfo, Type, TypeEq};
use crate::schema::{Namespace, NamespaceId};
#[derive(Default, Debug)]
pub struct Types {
pub types: BTreeMap<Ident, Type>,
pub modules: BTreeMap<NamespaceId, Module>,
next_name_id: usize,
}
#[derive(Debug)]
pub struct Module {
pub name: Name,
pub namespace: Namespace,
}
impl Types {
pub fn make_unnamed(&mut self) -> Name {
self.next_name_id = self.next_name_id.wrapping_add(1);
Name::Unnamed {
id: self.next_name_id,
ext: None,
}
}
#[must_use]
pub fn get_resolved(&self, ident: &Ident) -> Option<&Type> {
let mut visit = HashSet::new();
get_resolved(self, &mut visit, ident)
}
}
impl Deref for Types {
type Target = BTreeMap<Ident, Type>;
fn deref(&self) -> &Self::Target {
&self.types
}
}
impl DerefMut for Types {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.types
}
}
fn get_resolved<'a>(
types: &'a Types,
visited: &mut HashSet<Ident>,
ident: &Ident,
) -> Option<&'a Type> {
if !visited.insert(ident.clone()) {
return None;
}
let ty = types.get(ident)?;
match ty {
Type::Reference(x) if x.is_single() => match get_resolved(types, visited, &x.type_) {
None => Some(ty),
Some(ty) => Some(ty),
},
_ => Some(ty),
}
}