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
164
165
166
use TryFrom;
use Display;
use crateDomainEvent;
/// A trait that defines an `Entity`, which is any object with a unique and globally persistent identity.
///
/// The generic type `K` should match the same type as the internal globally unique id used for the entity.
/// Be careful when choosing what to return here. The result of [`id()`] will be used as the primary key
/// for the entity when communicating with a database via a repository.
///
/// # Example
/// ```rust
/// use domain_patterns::models::Entity;
///
/// struct User {
/// id: uuid::Uuid,
/// email: String,
/// password: String,
/// }
///
/// impl Entity for User {
/// fn id(&self) -> String {
/// self.id.to_string()
/// }
/// }
///
/// impl std::cmp::PartialEq for User {
/// fn eq(&self, other: &Self) -> bool {
/// &self.id() == &other.id()
/// }
/// }
/// ```
///
/// [`id()`]: ./trait.Entity.html#tymethod.id
/// Applier should be implemented by aggregate roots in systems where you want to apply messages (commands or events)
/// to mutate an aggregate.
// TODO: Improve error handling situation for ValueObjects. Maybe validate should return a list of errors rather
// than a boolean?
/// A trait that defines a `ValueObject` which is an immutable holder of value, that validates that value
/// against certain conditions before storing it.
///
/// # Example
/// ```rust
/// use std::{fmt, error};
/// use std::convert::TryFrom;
/// use regex::Regex;
/// use domain_patterns::models::ValueObject;
///
///
/// #[derive(Clone, PartialEq)]
/// struct Email {
/// value: String,
/// }
///
/// #[derive(Debug, Clone)]
/// struct EmailValidationError;
///
/// impl fmt::Display for EmailValidationError {
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// write!(f, "Email failed to validate.")
/// }
/// }
///
/// impl error::Error for EmailValidationError {
/// fn source(&self) -> Option<&(dyn error::Error + 'static)> {
/// None
/// }
/// }
///
/// impl TryFrom<String> for Email {
/// type Error = EmailValidationError;
///
/// fn try_from(value: String) -> Result<Self, Self::Error> {
/// Self::validate(&value)?;
///
/// Ok(Email {
/// value,
/// })
/// }
/// }
///
/// impl ValueObject<String> for Email {
/// type ValueError = EmailValidationError;
///
/// fn validate(value: &String) -> Result<(), EmailValidationError> {
/// let email_rx = Regex::new(
/// r"^(?i)[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$"
/// ).unwrap();
///
/// if !email_rx.is_match(value) {
/// return Err(EmailValidationError);
/// }
///
/// Ok(())
/// }
///
/// fn value(&self) -> String {
/// return self.value.clone()
/// }
/// }
///
/// impl fmt::Display for Email {
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// write!(f, "{}", self.value)
/// }
/// }
///
/// let email = Email::try_from("test_email@email.com".to_string()).unwrap();
/// ```