typedb_driver/concept/type_.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20use std::fmt;
21
22use super::ValueType;
23
24/// Annotations are used to specify extra schema constraints.
25#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
26pub enum Annotation {
27 Key,
28 Unique,
29}
30
31#[derive(Clone, Debug, PartialEq)]
32pub enum ThingType {
33 RootThingType(RootThingType),
34 EntityType(EntityType),
35 RelationType(RelationType),
36 AttributeType(AttributeType),
37}
38
39impl ThingType {
40 /// Retrieves the unique label of the `ThingType`.
41 ///
42 /// # Examples
43 ///
44 /// ```rust
45 /// thing_type.label()
46 /// ```
47 pub fn label(&self) -> &str {
48 match self {
49 Self::RootThingType(_) => RootThingType::LABEL,
50 Self::EntityType(entity_type) => &entity_type.label,
51 Self::RelationType(relation_type) => &relation_type.label,
52 Self::AttributeType(attribute_type) => &attribute_type.label,
53 }
54 }
55}
56
57#[derive(Clone, Debug, PartialEq, Eq)]
58pub struct RootThingType;
59
60impl RootThingType {
61 pub(crate) const LABEL: &'static str = "thing";
62}
63
64/// Entity types represent the classification of independent objects in the data model
65/// of the business domain.
66#[derive(Clone, Debug, PartialEq, Eq)]
67pub struct EntityType {
68 pub label: String,
69 pub is_root: bool,
70 pub is_abstract: bool,
71}
72
73impl EntityType {
74 pub(crate) const ROOT_LABEL: &'static str = "entity";
75
76 /// Returns the root `EntityType`
77 pub fn root() -> Self {
78 Self { label: Self::ROOT_LABEL.to_owned(), is_root: true, is_abstract: true }
79 }
80}
81
82/// Relation types (or subtypes of the relation root type) represent relationships between types.
83/// Relation types have roles.
84///
85/// Other types can play roles in relations if it’s mentioned in their definition.
86///
87/// A relation type must specify at least one role.
88#[derive(Clone, Debug, PartialEq, Eq)]
89pub struct RelationType {
90 pub label: String,
91 pub is_root: bool,
92 pub is_abstract: bool,
93}
94
95impl RelationType {
96 pub(crate) const ROOT_LABEL: &'static str = "relation";
97
98 /// Returns the root `RelationType`
99 pub fn root() -> Self {
100 Self { label: Self::ROOT_LABEL.to_owned(), is_root: true, is_abstract: true }
101 }
102}
103
104/// Attribute types represent properties that other types can own.
105///
106/// Attribute types have a value type. This value type is fixed and unique for every given instance
107/// of the attribute type.
108///
109/// Other types can own an attribute type. That means that instances of these other types can own
110/// an instance of this attribute type. This usually means that an object in our domain has
111/// a property with the matching value.
112///
113/// Multiple types can own the same attribute type, and different instances of the same type
114/// or different types can share ownership of the same attribute instance.
115#[derive(Clone, Debug, PartialEq)]
116pub struct AttributeType {
117 pub label: String,
118 pub is_root: bool,
119 pub is_abstract: bool,
120 pub value_type: ValueType,
121}
122
123impl AttributeType {
124 pub(crate) const ROOT_LABEL: &'static str = "attribute";
125
126 /// Returns the root `AttributeType`
127 pub fn root() -> Self {
128 Self { label: Self::ROOT_LABEL.to_owned(), is_root: true, is_abstract: true, value_type: ValueType::Object }
129 }
130}
131
132/// Roles are special internal types used by relations. We can not create an instance
133/// of a role in a database. But we can set an instance of another type (role player)
134/// to play a role in a particular instance of a relation type.
135///
136/// Roles allow a schema to enforce logical constraints on types of role players.
137#[derive(Clone, Debug, PartialEq, Eq)]
138pub struct RoleType {
139 pub label: ScopedLabel,
140 pub is_root: bool,
141 pub is_abstract: bool,
142}
143
144#[derive(Clone, Debug, PartialEq, Eq)]
145pub struct ScopedLabel {
146 pub scope: String,
147 pub name: String,
148}
149
150impl fmt::Display for ScopedLabel {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 write!(f, "{}:{}", self.scope, self.name)
153 }
154}