use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use crate::types::{Ident, TypeEq, Types};
use super::Base;
#[derive(Default, Debug, Clone)]
pub struct UnionInfo {
pub base: Base,
pub types: UnionTypesInfo,
}
#[derive(Debug, Clone)]
pub struct UnionTypeInfo {
pub type_: Ident,
pub display_name: Option<String>,
}
#[derive(Default, Debug, Clone)]
pub struct UnionTypesInfo(pub Vec<UnionTypeInfo>);
impl TypeEq for UnionInfo {
fn type_hash<H: Hasher>(&self, hasher: &mut H, ctx: &Types) {
let Self { base, types } = self;
base.type_hash(hasher, ctx);
types.type_hash(hasher, ctx);
}
fn type_eq(&self, other: &Self, ctx: &Types) -> bool {
let Self { base, types } = self;
base.type_eq(&other.base, ctx) && types.type_eq(&other.types, ctx)
}
}
impl UnionTypeInfo {
#[must_use]
pub fn new(type_: Ident) -> Self {
Self {
type_,
display_name: None,
}
}
}
impl TypeEq for UnionTypeInfo {
fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
let Self {
type_,
display_name,
} = self;
type_.type_hash(hasher, types);
display_name.hash(hasher);
}
fn type_eq(&self, other: &Self, types: &Types) -> bool {
let Self {
type_,
display_name,
} = self;
type_.type_eq(&other.type_, types) && display_name.eq(&other.display_name)
}
}
impl Deref for UnionTypesInfo {
type Target = Vec<UnionTypeInfo>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for UnionTypesInfo {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl TypeEq for UnionTypesInfo {
fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
TypeEq::type_hash_slice(&self.0, hasher, types);
}
fn type_eq(&self, other: &Self, types: &Types) -> bool {
TypeEq::type_eq_iter(self.0.iter(), other.0.iter(), types)
}
}