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