use crate::{builtin_idents, Transactable, Transaction, Value, ID};
#[derive(Clone, Copy)]
pub enum AttributeType {
String,
Integer,
Decimal,
ID,
Ref,
Boolean,
}
impl From<AttributeType> for ID {
fn from(t: AttributeType) -> Self {
use builtin_idents::*;
match t {
AttributeType::String => TYPE_STRING,
AttributeType::Integer => TYPE_INTEGER,
AttributeType::Decimal => TYPE_DECIMAL,
AttributeType::ID => TYPE_ID,
AttributeType::Ref => TYPE_REF,
AttributeType::Boolean => TYPE_BOOLEAN,
}
}
}
#[derive(Clone)]
pub struct AttributeSchema {
pub id: ID,
pub ident: Option<String>,
pub many: bool,
pub value_type: Option<AttributeType>,
pub doc: Option<String>,
pub unique: bool,
pub component: bool,
}
impl AttributeSchema {
pub fn new() -> Self {
Self {
id: ID::new(),
ident: None,
many: false,
value_type: None,
doc: None,
unique: false,
component: false,
}
}
pub const fn set_id(mut self, id: ID) -> Self {
self.id = id;
self
}
#[allow(clippy::missing_const_for_fn)]
pub fn ident(mut self, ident: String) -> Self {
self.ident = Some(ident);
self
}
pub const fn many(mut self) -> Self {
self.many = true;
self
}
pub const fn value_type(mut self, t: AttributeType) -> Self {
self.value_type = Some(t);
self
}
#[allow(clippy::missing_const_for_fn)]
pub fn doc(mut self, doc: String) -> Self {
self.doc = Some(doc);
self
}
pub const fn unique(mut self) -> Self {
self.unique = true;
self
}
pub const fn component(mut self) -> Self {
self.value_type = Some(AttributeType::Ref);
self.component = true;
self
}
}
impl Default for AttributeSchema {
fn default() -> Self {
Self::new()
}
}
impl Transactable for AttributeSchema {
fn tx(&self) -> Transaction {
let mut tx = Transaction::new();
tx.add(self.id.into(), builtin_idents::ID.into(), self.id.into());
if let Some(ident) = self.ident.clone() {
tx.add(self.id.into(), builtin_idents::IDENT.into(), ident.into());
}
if self.many {
tx.add(
self.id.into(),
builtin_idents::CARDINALITY.into(),
builtin_idents::CARDINALITY_MANY.into(),
)
}
if let Some(t) = self.value_type {
tx.add(
self.id.into(),
builtin_idents::VALUE_TYPE.into(),
Value::ID(t.into()),
);
}
if let Some(doc) = self.doc.clone() {
tx.add(self.id.into(), builtin_idents::DOC.into(), doc.into());
}
if self.unique {
tx.add(self.id.into(), builtin_idents::UNIQUE.into(), true.into());
}
if self.component {
tx.add(
self.id.into(),
builtin_idents::IS_COMPONENT.into(),
true.into(),
);
}
tx
}
}