1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// SPDX-FileCopyrightText: 2021 Lutris Engineering, Inc
// SPDX-License-Identifier: BlueOak-1.0.0 OR BSD-2-Clause-Patent
// SPDX-FileContributor: Piper McCorkle <piper@lutris.engineering>
use crate::{builtin_idents, Transactable, Transaction, Value, ID};
/// The type of an attribute's values
#[derive(Clone, Copy)]
pub enum AttributeType {
/// A [Value::String](crate::Value::String)
String,
/// A [Value::Integer](crate::Value::Integer)
Integer,
/// A [Value::Decimal](crate::Value::Decimal)
Decimal,
/// A [Value::ID](crate::Value::ID)
ID,
/// A [Value::ID](crate::Value::ID) referring to an entity
Ref,
/// A [Value::Boolean](crate::Value::Boolean)
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,
}
}
}
/// An imperative way to generate an attribute's schema
#[derive(Clone)]
pub struct AttributeSchema {
/// The attribute's ID
pub id: ID,
/// The attribute's unique identifier
pub ident: Option<String>,
/// Whether this attribute can store multiple values for an entity
pub many: bool,
/// What type values should be in this attribute
pub value_type: Option<AttributeType>,
/// A docstring for this attribute
pub doc: Option<String>,
/// Whether there can only be one entity of each value for this
/// attribute
pub unique: bool,
/// Whether this attribute refers to a component
pub component: bool,
}
impl AttributeSchema {
/// Start generating an attribute's schema
pub fn new() -> Self {
Self {
id: ID::new(),
ident: None,
many: false,
value_type: None,
doc: None,
unique: false,
component: false,
}
}
/// Set a specific ID for an attribute
pub const fn set_id(mut self, id: ID) -> Self {
self.id = id;
self
}
/// Set the attribute's ident
#[allow(clippy::missing_const_for_fn)]
pub fn ident(mut self, ident: String) -> Self {
self.ident = Some(ident);
self
}
/// Set the attribute's cardinality to many
pub const fn many(mut self) -> Self {
self.many = true;
self
}
/// Set the attribute's value type
pub const fn value_type(mut self, t: AttributeType) -> Self {
self.value_type = Some(t);
self
}
/// Set the attribute's docstring
#[allow(clippy::missing_const_for_fn)]
pub fn doc(mut self, doc: String) -> Self {
self.doc = Some(doc);
self
}
/// Set the attribute as unique
pub const fn unique(mut self) -> Self {
self.unique = true;
self
}
/// Set the attribute as being a component reference
pub const fn component(mut self) -> Self {
self.value_type = Some(AttributeType::Ref);
self.component = true;
self
}
}
impl Default for AttributeSchema {
/// ```
/// datom::AttributeSchema::default();
/// ```
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
}
}