use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use crate::schema::xs::Use;
use crate::types::{Ident, TypeEq, Types};
use super::use_hash;
#[derive(Debug, Clone)]
pub struct AttributeInfo {
pub ident: Ident,
pub type_: Ident,
pub use_: Use,
pub default: Option<String>,
pub display_name: Option<String>,
}
#[derive(Default, Debug, Clone)]
pub struct AttributesInfo(Vec<AttributeInfo>);
impl AttributeInfo {
#[must_use]
pub fn new(ident: Ident, type_: Ident) -> Self {
Self {
ident,
type_,
use_: Use::Optional,
default: None,
display_name: None,
}
}
#[must_use]
pub fn with_use(mut self, use_: Use) -> Self {
self.use_ = use_;
self
}
}
impl TypeEq for AttributeInfo {
fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
let Self {
ident,
type_,
use_,
default,
display_name,
} = self;
ident.hash(hasher);
type_.type_hash(hasher, types);
use_hash(use_, hasher);
default.hash(hasher);
display_name.hash(hasher);
}
fn type_eq(&self, other: &Self, types: &Types) -> bool {
let Self {
ident,
type_,
use_,
default,
display_name,
} = self;
ident.eq(&other.ident)
&& type_.type_eq(&other.type_, types)
&& use_.eq(&other.use_)
&& default.eq(&other.default)
&& display_name.eq(&other.display_name)
}
}
impl Deref for AttributesInfo {
type Target = Vec<AttributeInfo>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for AttributesInfo {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl TypeEq for AttributesInfo {
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)
}
}