use std::hash::{Hash, Hasher};
use crate::schema::{MaxOccurs, MinOccurs};
use crate::types::{Ident, TypeEq, Types};
#[derive(Debug, Clone)]
pub struct ReferenceInfo {
pub type_: Ident,
pub min_occurs: MinOccurs,
pub max_occurs: MaxOccurs,
}
impl ReferenceInfo {
#[must_use]
pub fn new<T>(type_: T) -> Self
where
T: Into<Ident>,
{
Self {
type_: type_.into(),
min_occurs: 1,
max_occurs: MaxOccurs::Bounded(1),
}
}
#[must_use]
pub fn is_single(&self) -> bool {
self.min_occurs == 1 && self.max_occurs == MaxOccurs::Bounded(1)
}
#[must_use]
pub fn min_occurs(mut self, min: MinOccurs) -> Self {
self.min_occurs = min;
self
}
#[must_use]
pub fn max_occurs(mut self, max: MaxOccurs) -> Self {
self.max_occurs = max;
self
}
}
impl TypeEq for ReferenceInfo {
fn type_hash<H: Hasher>(&self, hasher: &mut H, types: &Types) {
let Self {
type_,
min_occurs,
max_occurs,
} = self;
type_.type_hash(hasher, types);
min_occurs.hash(hasher);
max_occurs.hash(hasher);
}
fn type_eq(&self, other: &Self, types: &Types) -> bool {
let Self {
type_,
min_occurs,
max_occurs,
} = self;
type_.type_eq(&other.type_, types)
&& min_occurs.eq(&other.min_occurs)
&& max_occurs.eq(&other.max_occurs)
}
}