use crate::{
schema::app::{BelongsTo, FieldId, Model, ModelId, Name, Schema},
stmt,
};
#[derive(Debug, Clone)]
pub struct Has {
pub target: ModelId,
pub expr_ty: stmt::Type,
pub cardinality: Cardinality,
pub pair_id: FieldId,
}
#[derive(Debug, Clone)]
pub enum Cardinality {
Many {
singular: Name,
},
One,
}
impl Has {
pub fn is_many(&self) -> bool {
self.cardinality.is_many()
}
pub fn is_one(&self) -> bool {
self.cardinality.is_one()
}
pub fn singular(&self) -> Option<&Name> {
self.cardinality.singular()
}
pub fn target<'a>(&self, schema: &'a Schema) -> &'a Model {
schema.model(self.target)
}
pub fn pair<'a>(&self, schema: &'a Schema) -> &'a BelongsTo {
schema.field(self.pair_id).ty.as_belongs_to_unwrap()
}
}
impl Cardinality {
pub fn is_many(&self) -> bool {
matches!(self, Cardinality::Many { .. })
}
pub fn is_one(&self) -> bool {
matches!(self, Cardinality::One)
}
pub fn singular(&self) -> Option<&Name> {
match self {
Cardinality::Many { singular } => Some(singular),
Cardinality::One => None,
}
}
}