use crate::attribute::{Aggregation, Attribute};
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct InverseAttribute {
pub name: String,
pub redeclares: Option<String>,
pub entity: String,
pub for_attribute: String,
pub aggregation: Option<Aggregation>,
}
impl InverseAttribute {
#[must_use]
pub fn new(
name: impl Into<String>,
entity: impl Into<String>,
for_attribute: impl Into<String>,
) -> Self {
Self {
name: name.into(),
redeclares: None,
entity: entity.into(),
for_attribute: for_attribute.into(),
aggregation: None,
}
}
#[must_use]
pub fn redeclaring(mut self, supertype: impl Into<String>) -> Self {
self.redeclares = Some(supertype.into());
self
}
#[must_use]
pub fn with_aggregation(mut self, aggregation: Aggregation) -> Self {
self.aggregation = Some(aggregation);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct UniqueRule {
pub label: Option<String>,
pub attributes: Vec<String>,
}
impl UniqueRule {
#[must_use]
pub fn new(label: impl Into<String>, attributes: Vec<String>) -> Self {
Self {
label: Some(label.into()),
attributes,
}
}
#[must_use]
pub const fn unlabelled(attributes: Vec<String>) -> Self {
Self {
label: None,
attributes,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct WhereRule {
pub label: String,
pub expression: String,
}
impl WhereRule {
#[must_use]
pub fn new(label: impl Into<String>, expression: impl Into<String>) -> Self {
Self {
label: label.into(),
expression: expression.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct EntityDef {
pub name: String,
pub supertypes: Vec<String>,
pub abstract_: bool,
pub attributes: Vec<Attribute>,
pub derived: Vec<String>,
pub where_rules: Vec<WhereRule>,
pub inverses: Vec<InverseAttribute>,
pub unique_rules: Vec<UniqueRule>,
}
impl EntityDef {
#[must_use]
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
supertypes: Vec::new(),
abstract_: false,
attributes: Vec::new(),
derived: Vec::new(),
where_rules: Vec::new(),
inverses: Vec::new(),
unique_rules: Vec::new(),
}
}
#[must_use]
pub fn with_inverse(mut self, inverse: InverseAttribute) -> Self {
self.inverses.push(inverse);
self
}
#[must_use]
pub fn with_unique_rule(mut self, rule: UniqueRule) -> Self {
self.unique_rules.push(rule);
self
}
#[must_use]
pub fn with_supertype(mut self, supertype: impl Into<String>) -> Self {
self.supertypes.push(supertype.into());
self
}
#[must_use]
pub const fn abstract_entity(mut self) -> Self {
self.abstract_ = true;
self
}
#[must_use]
pub fn with_attribute(mut self, attribute: Attribute) -> Self {
self.attributes.push(attribute);
self
}
#[must_use]
pub fn with_derived(mut self, name: impl Into<String>) -> Self {
self.derived.push(name.into());
self
}
#[must_use]
pub fn with_where_rule(mut self, rule: WhereRule) -> Self {
self.where_rules.push(rule);
self
}
#[must_use]
pub fn supertype(&self) -> Option<&str> {
self.supertypes.first().map(String::as_str)
}
#[must_use]
pub fn is_derived(&self, name: &str) -> bool {
self.derived
.iter()
.any(|declared| declared.eq_ignore_ascii_case(name))
}
}