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
/*
 * Copyright (C) 2022 Vaticle
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

use std::fmt;

use super::ValueType;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Annotation {
    Key,
    Unique,
}

#[derive(Clone, Debug, PartialEq)]
pub enum ThingType {
    RootThingType(RootThingType),
    EntityType(EntityType),
    RelationType(RelationType),
    AttributeType(AttributeType),
}

impl ThingType {
    /// Retrieves the unique label of the `ThingType`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// thing_type.label()
    /// ```
    pub fn label(&self) -> &str {
        match self {
            Self::RootThingType(_) => RootThingType::LABEL,
            Self::EntityType(entity_type) => &entity_type.label,
            Self::RelationType(relation_type) => &relation_type.label,
            Self::AttributeType(attribute_type) => &attribute_type.label,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RootThingType;

impl RootThingType {
    pub(crate) const LABEL: &'static str = "thing";
}

/// Entity types represent the classification of independent objects in the data model
/// of the business domain.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EntityType {
    pub label: String,
    pub is_root: bool,
    pub is_abstract: bool,
}

impl EntityType {
    pub(crate) const ROOT_LABEL: &'static str = "entity";

    /// Returns the root `EntityType`
    pub fn root() -> Self {
        Self { label: Self::ROOT_LABEL.to_owned(), is_root: true, is_abstract: true }
    }
}

/// Relation types (or subtypes of the relation root type) represent relationships between types.
/// Relation types have roles.
///
/// Other types can play roles in relations if it’s mentioned in their definition.
///
/// A relation type must specify at least one role.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RelationType {
    pub label: String,
    pub is_root: bool,
    pub is_abstract: bool,
}

impl RelationType {
    pub(crate) const ROOT_LABEL: &'static str = "relation";

    /// Returns the root `RelationType`
    pub fn root() -> Self {
        Self { label: Self::ROOT_LABEL.to_owned(), is_root: true, is_abstract: true }
    }
}

/// Attribute types represent properties that other types can own.
///
/// Attribute types have a value type. This value type is fixed and unique for every given instance
/// of the attribute type.
///
/// Other types can own an attribute type. That means that instances of these other types can own
/// an instance of this attribute type. This usually means that an object in our domain has
/// a property with the matching value.
///
/// Multiple types can own the same attribute type, and different instances of the same type
/// or different types can share ownership of the same attribute instance.
#[derive(Clone, Debug, PartialEq)]
pub struct AttributeType {
    pub label: String,
    pub is_root: bool,
    pub is_abstract: bool,
    pub value_type: ValueType,
}

impl AttributeType {
    pub(crate) const ROOT_LABEL: &'static str = "attribute";

    /// Returns the root `AttributeType`
    pub fn root() -> Self {
        Self { label: Self::ROOT_LABEL.to_owned(), is_root: true, is_abstract: true, value_type: ValueType::Object }
    }
}

/// Roles are special internal types used by relations. We can not create an instance
/// of a role in a database. But we can set an instance of another type (role player)
/// to play a role in a particular instance of a relation type.
///
/// Roles allow a schema to enforce logical constraints on types of role players.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RoleType {
    pub label: ScopedLabel,
    pub is_root: bool,
    pub is_abstract: bool,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScopedLabel {
    pub scope: String,
    pub name: String,
}

impl fmt::Display for ScopedLabel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.scope, self.name)
    }
}