reactive_graph_graph/instances/relations/
relation_instance_id.rs1use regex::Regex;
2use schemars::JsonSchema;
3use serde::Deserialize;
4use serde::Serialize;
5use std::cmp::Ordering;
6use std::fmt::Display;
7use std::fmt::Formatter;
8use std::str::FromStr;
9use std::sync::LazyLock;
10use typed_builder::TypedBuilder;
11use uuid::Uuid;
12
13use crate::INSTANCE_ID_SEPARATOR;
14use crate::InstanceId;
15use crate::NamespaceSegment;
16use crate::NamespacedType;
17use crate::NamespacedTypeGetter;
18use crate::RelationInstanceIdParseError;
19use crate::RelationInstanceTypeId;
20use crate::RelationTypeId;
21use crate::TypeDefinition;
22use crate::TypeDefinitionGetter;
23use crate::TypeIdType;
24use crate::namespace::Namespace;
25
26pub static RELATION_INSTANCE_ID_SEPARATOR: &str = "--";
28
29pub static RELATION_INSTANCE_ID_OUTBOUND_SEPARATOR: &str = "--[";
30
31pub static RELATION_INSTANCE_ID_INBOUND_SEPARATOR: &str = "]-->";
32
33pub const RELATION_INSTANCE_ID_PATTERN: &str = r"^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})--\[([a-z_]+(?:::[a-z_]+)*(?:::([A-Z][a-zA-Z0-9]*))(?:__([a-zA-Z0-9_-]+))*)]-->([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$";
35
36pub static RELATION_INSTANCE_ID_REGEX: LazyLock<Regex> =
37 LazyLock::new(|| Regex::new(RELATION_INSTANCE_ID_PATTERN).expect("Failed to construct RELATION_INSTANCE_ID_REGEX!"));
38
39#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema, TypedBuilder)]
41pub struct RelationInstanceId {
42 pub outbound_id: Uuid,
44
45 #[builder(setter(into))]
47 pub ty: RelationInstanceTypeId,
48
49 pub inbound_id: Uuid,
51}
52
53impl RelationInstanceId {
54 pub fn new<RIT: Into<RelationInstanceTypeId>>(outbound_id: Uuid, ty: RIT, inbound_id: Uuid) -> Self {
55 RelationInstanceId {
56 outbound_id,
57 ty: ty.into(),
58 inbound_id,
59 }
60 }
61
62 pub fn new_singleton<RT: Into<RelationTypeId>>(outbound_id: Uuid, ty: RT, inbound_id: Uuid) -> Self {
63 RelationInstanceId {
64 outbound_id,
65 ty: RelationInstanceTypeId::new_singleton(ty),
66 inbound_id,
67 }
68 }
69
70 pub fn new_unique_for_instance_id<RT: Into<RelationTypeId>, ID: Into<InstanceId>>(outbound_id: Uuid, ty: RT, instance_id: ID, inbound_id: Uuid) -> Self {
71 RelationInstanceId {
72 outbound_id,
73 ty: RelationInstanceTypeId::new(ty, instance_id),
74 inbound_id,
75 }
76 }
77
78 pub fn new_with_random_instance_id<RT: Into<RelationTypeId>>(outbound_id: Uuid, ty: RT, inbound_id: Uuid) -> Self {
79 RelationInstanceId {
80 outbound_id,
81 ty: RelationInstanceTypeId::new_with_random_instance_id(ty),
82 inbound_id,
83 }
84 }
85}
86
87impl NamespacedTypeGetter for RelationInstanceId {
88 fn namespaced_type(&self) -> NamespacedType {
89 self.ty.namespaced_type()
90 }
91
92 fn namespace(&self) -> Namespace {
93 self.ty.namespace()
94 }
95
96 fn path(&self) -> Namespace {
97 self.ty.path()
98 }
99
100 fn type_name(&self) -> NamespaceSegment {
103 self.ty.type_name()
104 }
105}
106
107impl TypeDefinitionGetter for RelationInstanceId {
108 fn type_definition(&self) -> TypeDefinition {
109 self.into()
110 }
111
112 fn type_id_type() -> TypeIdType {
113 TypeIdType::RelationType
114 }
115}
116
117impl PartialOrd<Self> for RelationInstanceId {
118 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
119 Some(self.cmp(other))
120 }
121}
122
123impl Ord for RelationInstanceId {
124 fn cmp(&self, other: &Self) -> Ordering {
125 match self.ty.cmp(&other.ty) {
126 Ordering::Less => Ordering::Less,
127 Ordering::Equal => match self.outbound_id.cmp(&other.outbound_id) {
128 Ordering::Less => Ordering::Less,
129 Ordering::Equal => self.inbound_id.cmp(&other.inbound_id),
130 Ordering::Greater => Ordering::Greater,
131 },
132 Ordering::Greater => Ordering::Greater,
133 }
134 }
135}
136
137impl From<&RelationInstanceId> for RelationInstanceId {
138 fn from(ty: &RelationInstanceId) -> Self {
139 ty.clone()
140 }
141}
142
143impl From<&RelationInstanceId> for TypeDefinition {
144 fn from(ty: &RelationInstanceId) -> Self {
145 ty.type_definition()
146 }
147}
148
149impl From<&RelationInstanceId> for NamespacedType {
150 fn from(ty: &RelationInstanceId) -> Self {
151 NamespacedType::from(&ty.ty)
153 }
154}
155
156impl FromStr for RelationInstanceId {
157 type Err = RelationInstanceIdParseError;
158
159 fn from_str(s: &str) -> Result<Self, Self::Err> {
160 let Some((outbound_id_part, s)) = s.split_once(RELATION_INSTANCE_ID_OUTBOUND_SEPARATOR) else {
161 return Err(RelationInstanceIdParseError::MissingOutboundId);
162 };
163 let outbound_id = Uuid::from_str(outbound_id_part).map_err(|_| RelationInstanceIdParseError::InvalidOutboundId(outbound_id_part.to_string()))?;
164 let Some((relation_instance_type_id_part, inbound_id_part)) = s.split_once(RELATION_INSTANCE_ID_INBOUND_SEPARATOR) else {
165 return Err(RelationInstanceIdParseError::MissingInboundId);
166 };
167 let inbound_id = Uuid::from_str(inbound_id_part).map_err(|_| RelationInstanceIdParseError::InvalidInboundId(inbound_id_part.to_string()))?;
168 let ty = RelationInstanceTypeId::from_str(relation_instance_type_id_part).map_err(RelationInstanceIdParseError::RelationInstanceTypeIdParseError)?;
169 Ok(RelationInstanceId::new(outbound_id, ty, inbound_id))
170 }
171}
172
173impl Display for RelationInstanceId {
174 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
175 match &self.ty.instance_id() {
176 InstanceId::Singleton => {
177 write!(f, "{}--[{}]-->{}", self.outbound_id, &self.namespace().to_string(), self.inbound_id)
178 }
179 _ => {
180 write!(
181 f,
182 "{}--[{}{INSTANCE_ID_SEPARATOR}{}]-->{}",
183 self.outbound_id,
184 &self.namespace().to_string(),
185 &self.ty.instance_id().to_string(),
186 self.inbound_id
187 )
188 }
189 }
190 }
191}
192
193#[cfg(test)]
194mod tests {
195 use super::RELATION_INSTANCE_ID_REGEX;
196 use super::RelationInstanceId;
197 use crate::InstanceId;
198 use crate::NamespaceSegment;
199 use crate::RelationInstanceTypeId;
200 use crate::RelationTypeId;
201 use std::str::FromStr;
202 use uuid::Uuid;
203
204 #[test]
205 fn relation_instance_id_regex_test() {
206 assert!(RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"));
207 assert!(
208 RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05")
209 );
210 assert!(
211 RELATION_INSTANCE_ID_REGEX
212 .is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05")
213 );
214 assert!(
215 RELATION_INSTANCE_ID_REGEX
216 .is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace::namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05")
217 );
218
219 assert!(!RELATION_INSTANCE_ID_REGEX.is_match("--[namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"));
220 assert!(!RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::Type]-->"));
221 assert!(!RELATION_INSTANCE_ID_REGEX.is_match("--[namespace::Type]-->"));
222
223 assert!(!RELATION_INSTANCE_ID_REGEX.is_match("Test--[namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"));
224 assert!(!RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::Type]-->Test"));
225 assert!(!RELATION_INSTANCE_ID_REGEX.is_match("Test--[namespace::Type]-->Test"));
226
227 assert!(
228 !RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"),
229 "NamespacedType must have a namespace"
230 );
231 assert!(
232 !RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[Namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"),
233 "namespace must be lowercase"
234 );
235 assert!(
236 !RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"),
237 "Type name must be uppercase"
238 );
239 assert!(
240 !RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"),
241 "NamespacedType must have a type name"
242 );
243 assert!(
244 !RELATION_INSTANCE_ID_REGEX
245 .is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"),
246 "NamespacedType must have a type name"
247 );
248 assert!(
249 !RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[::namespace::namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"),
250 "First segment must not be empty"
251 );
252 assert!(
253 !RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::Type::]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"),
254 "Last segment must not be empty"
255 );
256 assert!(
257 !RELATION_INSTANCE_ID_REGEX.is_match("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"),
258 "Last segment must be a type name and must not be empty"
259 );
260 }
261
262 #[test]
263 fn relation_instance_id_from_str_test() {
264 assert_eq!(
265 InstanceId::Singleton,
266 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05")
267 .unwrap()
268 .ty
269 .instance_id()
270 );
271 assert_eq!(
272 InstanceId::Singleton,
273 RelationInstanceId::from_str(
274 "8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"
275 )
276 .unwrap()
277 .ty
278 .instance_id()
279 );
280 assert_eq!(
281 InstanceId::Singleton,
282 RelationInstanceId::from_str(
283 "8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace::namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"
284 )
285 .unwrap()
286 .ty
287 .instance_id()
288 );
289
290 assert_eq!(
291 InstanceId::Id(Uuid::from_str("8fe1dd09-5878-4c6e-be33-7cfc95c60ce2").unwrap()),
292 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::Type__8fe1dd09-5878-4c6e-be33-7cfc95c60ce2]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05")
293 .unwrap()
294 .ty
295 .instance_id()
296 );
297 assert_eq!(
298 InstanceId::Id(Uuid::from_str("8fe1dd09-5878-4c6e-be33-7cfc95c60ce2").unwrap()),
299 RelationInstanceId::from_str(
300 "8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace::Type__8fe1dd09-5878-4c6e-be33-7cfc95c60ce2]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"
301 )
302 .unwrap()
303 .ty
304 .instance_id()
305 );
306 assert_eq!(
307 InstanceId::Id(Uuid::from_str("8fe1dd09-5878-4c6e-be33-7cfc95c60ce2").unwrap()),
308 RelationInstanceId::from_str(
309 "8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace::namespace::Type__8fe1dd09-5878-4c6e-be33-7cfc95c60ce2]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"
310 )
311 .unwrap()
312 .ty
313 .instance_id()
314 );
315
316 assert_eq!(
317 InstanceId::Named(NamespaceSegment::from_str("test").unwrap()),
318 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::Type__test]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05")
319 .unwrap()
320 .ty
321 .instance_id()
322 );
323 assert_eq!(
324 InstanceId::Named(NamespaceSegment::from_str("test").unwrap()),
325 RelationInstanceId::from_str(
326 "8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace::Type__test]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"
327 )
328 .unwrap()
329 .ty
330 .instance_id()
331 );
332 assert_eq!(
333 InstanceId::Named(NamespaceSegment::from_str("test").unwrap()),
334 RelationInstanceId::from_str(
335 "8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace::namespace::Type__test]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"
336 )
337 .unwrap()
338 .ty
339 .instance_id()
340 );
341
342 assert_eq!(
343 InstanceId::MultiSegmented(vec![
344 NamespaceSegment::from_str("segment_1").unwrap(),
345 NamespaceSegment::from_str("segment_2").unwrap(),
346 NamespaceSegment::from_str("segment_3").unwrap()
347 ]),
348 RelationInstanceId::from_str(
349 "8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::Type__segment_1__segment_2__segment_3]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"
350 )
351 .unwrap()
352 .ty
353 .instance_id()
354 );
355 assert_eq!(
356 InstanceId::MultiSegmented(vec![
357 NamespaceSegment::from_str("segment_1").unwrap(),
358 NamespaceSegment::from_str("segment_2").unwrap(),
359 NamespaceSegment::from_str("segment_3").unwrap()
360 ]),
361 RelationInstanceId::from_str(
362 "8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace::Type__segment_1__segment_2__segment_3]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"
363 )
364 .unwrap()
365 .ty
366 .instance_id()
367 );
368 assert_eq!(
369 InstanceId::MultiSegmented(vec![
370 NamespaceSegment::from_str("segment_1").unwrap(),
371 NamespaceSegment::from_str("segment_2").unwrap(),
372 NamespaceSegment::from_str("segment_3").unwrap()
373 ]),
374 RelationInstanceId::from_str(
375 "8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace::namespace::Type__segment_1__segment_2__segment_3]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05"
376 )
377 .unwrap()
378 .ty
379 .instance_id()
380 );
381
382 assert!(
383 RelationInstanceId::from_str("--[namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05").is_err(),
384 "Missing outbound id"
385 );
386 assert!(
387 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::Type]-->").is_err(),
388 "Missing inbound id"
389 );
390 assert!(RelationInstanceId::from_str("--[namespace::Type]-->").is_err(), "Missing outbound and inbound id");
391
392 assert!(
393 RelationInstanceId::from_str("Test--[namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05").is_err(),
394 "Outbound id must be a valid UUID"
395 );
396 assert!(
397 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::Type]-->Test").is_err(),
398 "Inbound id must be a valid UUID"
399 );
400 assert!(
401 RelationInstanceId::from_str("Test--[namespace::Type]-->Test").is_err(),
402 "Outbound id and Inbound id must be a valid UUID"
403 );
404
405 assert!(
406 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05").is_err(),
407 "NamespacedType must have a namespace"
408 );
409 assert!(
410 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[Namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05").is_err(),
411 "namespace must be lowercase"
412 );
413 assert!(
414 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05").is_err(),
415 "Type name must be uppercase"
416 );
417 assert!(
418 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05").is_err(),
419 "NamespacedType must have a type name"
420 );
421 assert!(
422 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::namespace]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05")
423 .is_err(),
424 "NamespacedType must have a type name"
425 );
426 assert!(
427 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[::namespace::namespace::Type]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05")
428 .is_err(),
429 "First segment must not be empty"
430 );
431 assert!(
432 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::namespace::Type::]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05")
433 .is_err(),
434 "Last segment must not be empty"
435 );
436 assert!(
437 RelationInstanceId::from_str("8f816c4e-b08a-472f-b37e-457a712656d5--[namespace::]-->f35395e1-d5ea-4e84-8dbd-331e1aaf3d05").is_err(),
438 "Last segment must be a type name and must not be empty"
439 );
440 }
441
442 #[test]
443 fn relation_instance_id_to_string_test() {
444 let outbound_id = Uuid::from_str("65b3aab3-82ad-4605-a9e7-227f4da73c28").unwrap();
445 let relation_ty = RelationTypeId::from_str("namespace::namespace::Type").unwrap();
446 let inbound_id = Uuid::from_str("a99b7f91-5bdb-44b9-9a87-3a6e24a54b3f").unwrap();
447
448 let relation_instance_id = RelationInstanceId::builder()
449 .outbound_id(outbound_id)
450 .ty(RelationInstanceTypeId::new(relation_ty.clone(), InstanceId::Singleton))
451 .inbound_id(inbound_id)
452 .build();
453
454 let string_representation = relation_instance_id.to_string();
455 println!("{}", string_representation);
456 assert_eq!(relation_instance_id, RelationInstanceId::from_str(&string_representation).unwrap());
457
458 let relation_instance_id = RelationInstanceId::builder()
459 .outbound_id(outbound_id)
460 .ty(RelationInstanceTypeId::new(
461 relation_ty.clone(),
462 InstanceId::Id(Uuid::from_str("f9271804-3f5d-495a-84af-a4a782aec160").unwrap()),
463 ))
464 .inbound_id(inbound_id)
465 .build();
466
467 let string_representation = relation_instance_id.to_string();
468 assert_eq!(relation_instance_id, RelationInstanceId::from_str(&string_representation).unwrap());
469
470 let relation_instance_id = RelationInstanceId::builder()
471 .outbound_id(outbound_id)
472 .ty(RelationInstanceTypeId::new(
473 relation_ty.clone(),
474 InstanceId::Named(NamespaceSegment::from_str("test").unwrap()),
475 ))
476 .inbound_id(inbound_id)
477 .build();
478
479 let string_representation = relation_instance_id.to_string();
480 assert_eq!(relation_instance_id, RelationInstanceId::from_str(&string_representation).unwrap());
481
482 let relation_instance_id = RelationInstanceId::builder()
483 .outbound_id(outbound_id)
484 .ty(RelationInstanceTypeId::new(
485 relation_ty,
486 InstanceId::MultiSegmented(vec![
487 NamespaceSegment::from_str("segment_1").unwrap(),
488 NamespaceSegment::from_str("segment_2").unwrap(),
489 NamespaceSegment::from_str("segment_3").unwrap(),
490 ]),
491 ))
492 .inbound_id(inbound_id)
493 .build();
494
495 let string_representation = relation_instance_id.to_string();
496 assert_eq!(relation_instance_id, RelationInstanceId::from_str(&string_representation).unwrap());
497 }
498}