use crate::{ast::*, parser::*};
#[derive(Debug, Clone, PartialEq)]
pub struct Entity {
pub name: String,
pub attributes: Vec<EntityAttribute>,
pub constraint: Option<Constraint>,
pub subtype_of: Option<SubTypeDecl>,
pub derive_clause: Option<DeriveClause>,
pub inverse_clause: Option<InverseClause>,
pub unique_clause: Option<UniqueClause>,
pub where_clause: Option<WhereClause>,
}
crate::derive_ast_component!(Entity, entity_decl);
impl Entity {
pub fn has_supertype_decl(&self) -> bool {
if let Some(c) = &self.constraint {
match c {
Constraint::AbstractSuperType(..) | Constraint::SuperTypeRule(..) => true,
Constraint::AbstractEntity => false,
}
} else {
false
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct EntityBody {
pub attributes: Vec<EntityAttribute>,
pub derive_clause: Option<DeriveClause>,
pub inverse_clause: Option<InverseClause>,
pub unique_clause: Option<UniqueClause>,
pub where_clause: Option<WhereClause>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AttributeDecl {
Reference(String),
Qualified {
group: String,
attribute: String,
rename: Option<String>,
},
}
impl<'a> PartialEq<&'a str> for AttributeDecl {
fn eq(&self, other: &&'a str) -> bool {
match self {
AttributeDecl::Reference(name) => other.eq(name),
_ => false,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct EntityAttribute {
pub name: AttributeDecl,
pub ty: Type,
pub optional: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DeriveClause {
pub attributes: Vec<DerivedAttribute>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DerivedAttribute {
pub attr: AttributeDecl,
pub ty: Type,
pub expr: Expression,
}
#[derive(Debug, Clone, PartialEq)]
pub struct InverseClause {
pub attributes: Vec<InverseAttribute>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct InverseAttribute {
pub name: AttributeDecl,
pub dest: String,
pub dest_aggregation: AggregationOption,
pub attribute: String,
pub attribute_prefix: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum AggregationOption {
Set { bound: Option<Bound> },
Bag { bound: Option<Bound> },
None,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Constraint {
AbstractEntity,
AbstractSuperType(Option<SuperTypeExpression>),
SuperTypeRule(SuperTypeExpression),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubTypeDecl {
pub entity_references: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SuperTypeExpression {
Reference(String),
AndOr { factors: Vec<SuperTypeExpression> },
And { terms: Vec<SuperTypeExpression> },
OneOf { exprs: Vec<SuperTypeExpression> },
}
#[derive(Debug, Clone, PartialEq)]
pub struct SubTypeConstraint {
pub name: String,
pub entity: String,
pub is_abstract: bool,
pub total_over: Option<Vec<String>>,
pub expr: Option<SuperTypeExpression>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UniqueClause {
pub rules: Vec<UniqueRule>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct UniqueRule {
pub name: Option<String>,
pub attributes: Vec<AttributeDecl>,
}