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::{Debug, Display, Formatter};
21
22use super::ValueType;
23
24/// Entity types represent the classification of independent objects in the data model
25/// of the business domain.
26#[derive(Clone, Debug, PartialEq, Eq)]
27pub struct EntityType {
28 pub label: String,
29}
30
31impl EntityType {
32 /// Retrieves the unique label of the `EntityType`.
33 ///
34 /// # Examples
35 ///
36 /// ```rust
37 /// entity_type.label()
38 /// ```
39 pub fn label(&self) -> &str {
40 &self.label
41 }
42}
43
44impl Display for EntityType {
45 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46 Debug::fmt(self, f)
47 }
48}
49
50/// Relation types (or subtypes of the relation root type) represent relationships between types.
51/// Relation types have roles.
52///
53/// Other types can play roles in relations if it’s mentioned in their definition.
54///
55/// A relation type must specify at least one role.
56#[derive(Clone, Debug, PartialEq, Eq)]
57pub struct RelationType {
58 pub label: String,
59}
60
61impl RelationType {
62 /// Retrieves the unique label of the `RelationType`.
63 ///
64 /// # Examples
65 ///
66 /// ```rust
67 /// relation_type.label()
68 /// ```
69 pub fn label(&self) -> &str {
70 &self.label
71 }
72}
73
74impl Display for RelationType {
75 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
76 Debug::fmt(self, f)
77 }
78}
79
80/// Attribute types represent properties that other types can own.
81///
82/// Attribute types have a value type. This value type is fixed and unique for every given instance
83/// of the attribute type.
84///
85/// Other types can own an attribute type. That means that instances of these other types can own
86/// an instance of this attribute type. This usually means that an object in our domain has
87/// a property with the matching value.
88///
89/// Multiple types can own the same attribute type, and different instances of the same type
90/// or different types can share ownership of the same attribute instance.
91#[derive(Clone, Debug, PartialEq)]
92pub struct AttributeType {
93 pub label: String,
94 pub value_type: Option<ValueType>,
95}
96
97impl AttributeType {
98 /// Retrieves the unique label of the `AttributeType`.
99 ///
100 /// # Examples
101 ///
102 /// ```rust
103 /// attribute_type.label()
104 /// ```
105 pub fn label(&self) -> &str {
106 &self.label
107 }
108
109 /// Retrieves the `ValueType` of the `AttributeType`.
110 ///
111 /// # Examples
112 ///
113 /// ```rust
114 /// attribute_type.value_type()
115 /// ```
116 pub fn value_type(&self) -> Option<&ValueType> {
117 self.value_type.as_ref()
118 }
119}
120
121impl Display for AttributeType {
122 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
123 Debug::fmt(self, f)
124 }
125}
126
127/// Roles are special internal types used by relations. We can not create an instance
128/// of a role in a database. But we can set an instance of another type (role player)
129/// to play a role in a particular instance of a relation type.
130///
131/// Roles allow a schema to enforce logical constraints on types of role players.
132#[derive(Clone, Debug, PartialEq, Eq)]
133pub struct RoleType {
134 pub label: String,
135}
136
137impl RoleType {
138 /// Retrieves the unique label of the `RoleType`.
139 ///
140 /// # Examples
141 ///
142 /// ```rust
143 /// role_type.label()
144 /// ```
145 pub fn label(&self) -> &str {
146 &self.label
147 }
148}
149
150impl Display for RoleType {
151 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
152 Debug::fmt(self, f)
153 }
154}