use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use xsd_parser_types::xml::NamespacesShared;
use crate::models::{schema::xs::Use, EnumerationIdent, TypeIdent};
use super::{Base, Constrains, MetaTypes, TypeEq};
#[derive(Default, Debug, Clone)]
pub struct EnumerationMeta {
pub base: Base,
pub variants: EnumerationMetaVariants,
pub constrains: Constrains,
}
#[derive(Debug, Clone)]
pub struct EnumerationMetaVariant {
pub ident: EnumerationIdent,
pub use_: Use,
pub type_: Option<TypeIdent>,
pub display_name: Option<String>,
pub documentation: Vec<String>,
pub namespaces: Option<NamespacesShared<'static>>,
}
#[derive(Default, Debug, Clone)]
pub struct EnumerationMetaVariants(pub Vec<EnumerationMetaVariant>);
impl TypeEq for EnumerationMeta {
fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
let Self {
base,
variants,
constrains,
} = self;
base.type_hash(hasher, types);
variants.type_hash(hasher, types);
constrains.hash(hasher);
}
fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
let Self {
base,
variants,
constrains,
} = self;
base.type_eq(&other.base, types)
&& variants.type_eq(&other.variants, types)
&& constrains.eq(&other.constrains)
}
}
impl EnumerationMetaVariant {
#[must_use]
pub fn new(ident: EnumerationIdent) -> Self {
Self {
ident,
use_: Use::Optional,
type_: None,
display_name: None,
documentation: Vec::new(),
namespaces: None,
}
}
#[must_use]
pub fn with_type(mut self, type_: Option<TypeIdent>) -> Self {
self.type_ = type_;
self
}
}
impl TypeEq for EnumerationMetaVariant {
fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
let Self {
ident,
use_,
type_,
display_name,
documentation,
namespaces,
} = self;
ident.hash(hasher);
use_.hash(hasher);
type_.type_hash(hasher, types);
display_name.hash(hasher);
documentation.hash(hasher);
namespaces.hash(hasher);
}
fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
let Self {
ident,
use_,
type_,
display_name,
documentation,
namespaces,
} = self;
ident.eq(&other.ident)
&& use_.eq(&other.use_)
&& type_.type_eq(&other.type_, types)
&& display_name.eq(&other.display_name)
&& documentation.eq(&other.documentation)
&& namespaces.eq(&other.namespaces)
}
}
impl Deref for EnumerationMetaVariants {
type Target = Vec<EnumerationMetaVariant>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for EnumerationMetaVariants {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl TypeEq for EnumerationMetaVariants {
fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &MetaTypes) {
TypeEq::type_hash_slice(&self.0, hasher, types);
}
fn type_eq(&self, other: &Self, types: &MetaTypes) -> bool {
TypeEq::type_eq_iter(self.0.iter(), other.0.iter(), types)
}
}