acton-ern 2.0.0

A Rust library for handling Entity Resource Names (ERNs), providing type-safe, hierarchical, and k-sortable resource identifiers for distributed systems and more.
Documentation
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
use std::cmp::Ordering;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::Add;
use std::sync::Arc;

use crate::errors::ErnError;
use crate::{Account, Category, Domain, EntityRoot, ErnComponent, Part, Parts};

#[derive(Debug, PartialEq, Eq, Hash)]
struct ErnInner {
    domain: Domain,
    category: Category,
    account: Account,
    root: EntityRoot,
    parts: Parts,
}

/// Represents an Entity Resource Name (ERN), which uniquely identifies resources in distributed systems.
///
/// An ERN follows the URN format and has the structure:
/// `ern:domain:category:account:root/path/to/resource`
///
/// Each component serves a specific purpose:
/// - `domain`: Classifies the resource (e.g., internal, external, custom domains)
/// - `category`: Specifies the service or category within the system
/// - `account`: Identifies the owner or account responsible for the resource
/// - `root`: A unique identifier for the root of the resource hierarchy
/// - `parts`: Optional path-like structure showing the resource's position within the hierarchy
///
/// ERNs can be k-sortable when using `UnixTime` or `Timestamp` ID types, enabling
/// efficient ordering and range queries.
///
/// Internally wraps its fields in an `Arc`, making `clone()` a cheap atomic
/// reference-count increment instead of a deep copy.
#[derive(Debug, Clone)]
pub struct Ern {
    inner: Arc<ErnInner>,
}

impl Ern {
    /// Creates a new ERN with the specified components.
    ///
    /// # Arguments
    ///
    /// * `domain` - The domain component
    /// * `category` - The category component
    /// * `account` - The account component
    /// * `root` - The root component
    /// * `parts` - The path parts component
    ///
    /// # Example
    ///
    /// ```
    /// # use acton_ern::prelude::*;
    /// # fn example() -> Result<(), ErnError> {
    /// let ern = Ern::new(
    ///     Domain::new("my-app")?,
    ///     Category::new("users")?,
    ///     Account::new("tenant123")?,
    ///     EntityRoot::new("profile".to_string())?,
    ///     Parts::new(vec![Part::new("settings")?]),
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(
        domain: Domain,
        category: Category,
        account: Account,
        root: EntityRoot,
        parts: Parts,
    ) -> Self {
        Ern {
            inner: Arc::new(ErnInner {
                domain,
                category,
                account,
                root,
                parts,
            }),
        }
    }

    fn from_inner(inner: ErnInner) -> Self {
        Ern {
            inner: Arc::new(inner),
        }
    }

    /// Returns a reference to the domain component.
    pub fn domain(&self) -> &Domain {
        &self.inner.domain
    }

    /// Returns a reference to the category component.
    pub fn category(&self) -> &Category {
        &self.inner.category
    }

    /// Returns a reference to the account component.
    pub fn account(&self) -> &Account {
        &self.inner.account
    }

    /// Returns a reference to the root component.
    pub fn root(&self) -> &EntityRoot {
        &self.inner.root
    }

    /// Returns a reference to the parts component.
    pub fn parts(&self) -> &Parts {
        &self.inner.parts
    }

    /// Creates a new ERN with the given root and default values for other components.
    ///
    /// This is a convenient way to create an ERN when you only care about the root component.
    ///
    /// # Arguments
    ///
    /// * `root` - The string value for the root component
    ///
    /// # Returns
    ///
    /// * `Ok(Ern)` - The created ERN with default values for domain, category, account, and parts
    /// * `Err(ErnError)` - If the root value is invalid
    ///
    /// # Example
    ///
    /// ```
    /// # use acton_ern::prelude::*;
    /// # fn example() -> Result<(), ErnError> {
    /// let ern = Ern::with_root("profile")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_root(root: impl Into<String>) -> Result<Self, ErnError> {
        let root = EntityRoot::new(root.into())?;
        Ok(Ern::from_inner(ErnInner {
            root,
            domain: Domain::default(),
            category: Category::default(),
            account: Account::default(),
            parts: Parts::new(Vec::default()),
        }))
    }

    /// Creates a new ERN based on an existing ERN but with a different root.
    ///
    /// This method preserves all other components (domain, category, account, parts)
    /// but replaces the root with a new value.
    ///
    /// # Arguments
    ///
    /// * `new_root` - The string value for the new root component
    ///
    /// # Returns
    ///
    /// * `Ok(Ern)` - A new ERN with the updated root
    /// * `Err(ErnError)` - If the new root value is invalid
    ///
    /// # Example
    ///
    /// ```
    /// # use acton_ern::prelude::*;
    /// # fn example() -> Result<(), ErnError> {
    /// let ern1 = Ern::with_root("profile")?;
    /// let ern2 = ern1.with_new_root("settings")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_new_root(&self, new_root: impl Into<String>) -> Result<Self, ErnError> {
        let new_root = EntityRoot::new(new_root.into())?;
        Ok(Ern::from_inner(ErnInner {
            domain: self.inner.domain.clone(),
            category: self.inner.category.clone(),
            account: self.inner.account.clone(),
            root: new_root,
            parts: self.inner.parts.clone(),
        }))
    }

    pub fn with_domain(domain: impl Into<String>) -> Result<Self, ErnError> {
        let domain = Domain::new(domain)?;
        Ok(Ern::from_inner(ErnInner {
            domain,
            category: Category::default(),
            account: Account::default(),
            root: EntityRoot::default(),
            parts: Parts::default(),
        }))
    }

    pub fn with_category(category: impl Into<String>) -> Result<Self, ErnError> {
        let category = Category::new(category)?;
        Ok(Ern::from_inner(ErnInner {
            domain: Domain::default(),
            category,
            account: Account::default(),
            root: EntityRoot::default(),
            parts: Parts::default(),
        }))
    }

    pub fn with_account(account: impl Into<String>) -> Result<Self, ErnError> {
        let account = Account::new(account)?;
        Ok(Ern::from_inner(ErnInner {
            domain: Domain::default(),
            category: Category::default(),
            account,
            root: EntityRoot::default(),
            parts: Parts::default(),
        }))
    }

    /// Adds a new part to the ERN's path.
    ///
    /// This method creates a new ERN with the same domain, category, account, and root,
    /// but with an additional part appended to the path.
    ///
    /// # Arguments
    ///
    /// * `part` - The string value for the new part
    ///
    /// # Returns
    ///
    /// * `Ok(Ern)` - A new ERN with the added part
    /// * `Err(ErnError)` - If the part value is invalid or adding it would exceed the maximum of 10 parts
    ///
    /// # Example
    ///
    /// ```
    /// # use acton_ern::prelude::*;
    /// # fn example() -> Result<(), ErnError> {
    /// let ern1 = Ern::with_root("profile")?;
    /// let ern2 = ern1.add_part("settings")?;
    /// let ern3 = ern2.add_part("appearance")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn add_part(&self, part: impl Into<String>) -> Result<Self, ErnError> {
        let new_part = Part::new(part)?;
        let mut new_parts = self.inner.parts.clone();

        // Check if adding another part would exceed the maximum
        if new_parts.0.len() >= 10 {
            return Err(ErnError::ParseFailure(
                "Parts",
                "cannot exceed maximum of 10 parts".to_string(),
            ));
        }

        new_parts.0.push(new_part);
        Ok(Ern::from_inner(ErnInner {
            domain: self.inner.domain.clone(),
            category: self.inner.category.clone(),
            account: self.inner.account.clone(),
            root: self.inner.root.clone(),
            parts: new_parts,
        }))
    }

    pub fn with_parts(
        &self,
        parts: impl IntoIterator<Item = impl Into<String>>,
    ) -> Result<Self, ErnError> {
        let new_parts: Result<Vec<Part>, _> = parts.into_iter().map(Part::new).collect();
        Ok(Ern::from_inner(ErnInner {
            domain: self.inner.domain.clone(),
            category: self.inner.category.clone(),
            account: self.inner.account.clone(),
            root: self.inner.root.clone(),
            parts: Parts(new_parts?),
        }))
    }

    /// Checks if this ERN is a child of another ERN.
    ///
    /// An ERN is considered a child of another ERN if:
    /// 1. They have the same domain, category, account, and root
    /// 2. The child's parts start with all of the parent's parts
    /// 3. The child has more parts than the parent
    ///
    /// # Arguments
    ///
    /// * `other` - The potential parent ERN
    ///
    /// # Returns
    ///
    /// * `true` - If this ERN is a child of the other ERN
    /// * `false` - Otherwise
    ///
    /// # Example
    ///
    /// ```
    /// # use acton_ern::prelude::*;
    /// # fn example() -> Result<(), ErnError> {
    /// let parent = Ern::with_root("profile")?.add_part("settings")?;
    /// let child = parent.add_part("appearance")?;
    ///
    /// assert!(child.is_child_of(&parent));
    /// assert!(!parent.is_child_of(&child));
    /// # Ok(())
    /// # }
    /// ```
    pub fn is_child_of(&self, other: &Ern) -> bool {
        self.inner.domain == other.inner.domain
            && self.inner.category == other.inner.category
            && self.inner.account == other.inner.account
            && self.inner.root == other.inner.root
            && other.inner.parts.0.len() < self.inner.parts.0.len()
            && self.inner.parts.0.starts_with(&other.inner.parts.0)
    }

    /// Returns the parent ERN of this ERN, if it exists.
    ///
    /// The parent ERN has the same domain, category, account, and root,
    /// but with one fewer part in the path. If this ERN has no parts,
    /// it has no parent and this method returns `None`.
    ///
    /// # Returns
    ///
    /// * `Some(Ern)` - The parent ERN
    /// * `None` - If this ERN has no parts (and thus no parent)
    ///
    /// # Example
    ///
    /// ```
    /// # use acton_ern::prelude::*;
    /// # fn example() -> Result<(), ErnError> {
    /// let ern1 = Ern::with_root("profile")?;
    /// let ern2 = ern1.add_part("settings")?;
    /// let ern3 = ern2.add_part("appearance")?;
    ///
    /// assert_eq!(ern3.parent().unwrap().to_string(), ern2.to_string());
    /// assert_eq!(ern2.parent().unwrap().to_string(), ern1.to_string());
    /// assert!(ern1.parent().is_none());
    /// # Ok(())
    /// # }
    /// ```
    pub fn parent(&self) -> Option<Self> {
        if self.inner.parts.0.is_empty() {
            None
        } else {
            Some(Ern::from_inner(ErnInner {
                domain: self.inner.domain.clone(),
                category: self.inner.category.clone(),
                account: self.inner.account.clone(),
                root: self.inner.root.clone(),
                parts: Parts(self.inner.parts.0[..self.inner.parts.0.len() - 1].to_vec()),
            }))
        }
    }
}

impl PartialEq for Ern {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner) || self.inner == other.inner
    }
}

impl Eq for Ern {}

impl Hash for Ern {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.inner.hash(state);
    }
}

impl Ord for Ern {
    fn cmp(&self, other: &Self) -> Ordering {
        self.inner.root.name().cmp(other.inner.root.name())
    }
}

impl PartialOrd for Ern {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Display for Ern {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let mut display = format!(
            "{}{}:{}:{}:{}",
            Domain::prefix(),
            self.inner.domain,
            self.inner.category,
            self.inner.account,
            self.inner.root
        );
        if !self.inner.parts.0.is_empty() {
            display = format!("{}/{}", display, self.inner.parts);
        }
        write!(f, "{}", display)
    }
}

impl Add for Ern {
    type Output = Ern;

    fn add(self, rhs: Self) -> Self::Output {
        let mut new_parts = self.inner.parts.0.clone();
        new_parts.extend(rhs.inner.parts.0.iter().cloned());
        Ern::from_inner(ErnInner {
            domain: self.inner.domain.clone(),
            category: self.inner.category.clone(),
            account: self.inner.account.clone(),
            root: self.inner.root.clone(),
            parts: Parts(new_parts),
        })
    }
}

impl Default for Ern {
    /// Provides a default ERN using the default values of all its components.
    ///
    /// This is primarily used internally and for testing. For creating ERNs in
    /// application code, prefer using `ErnBuilder` or the `with_root` method.
    fn default() -> Self {
        Ern::new(
            Domain::default(),
            Category::default(),
            Account::default(),
            EntityRoot::default(),
            Parts::new(Vec::default()),
        )
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for Ern {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut state = serializer.serialize_struct("Ern", 5)?;
        state.serialize_field("domain", &self.inner.domain)?;
        state.serialize_field("category", &self.inner.category)?;
        state.serialize_field("account", &self.inner.account)?;
        state.serialize_field("root", &self.inner.root)?;
        state.serialize_field("parts", &self.inner.parts)?;
        state.end()
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Ern {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(serde::Deserialize)]
        struct ErnFields {
            domain: Domain,
            category: Category,
            account: Account,
            root: EntityRoot,
            parts: Parts,
        }
        let fields = ErnFields::deserialize(deserializer)?;
        Ok(Ern::new(
            fields.domain,
            fields.category,
            fields.account,
            fields.root,
            fields.parts,
        ))
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;
    use std::thread::sleep;
    use std::time::Duration;

    use crate::Part;

    use super::*;

    #[test]
    fn test_ern_timestamp_ordering() {
        let ern1: Ern = Ern::with_root("root_a").unwrap();
        sleep(Duration::from_millis(10));
        let ern2: Ern = Ern::with_root("root_b").unwrap();
        sleep(Duration::from_millis(10));
        let ern3: Ern = Ern::with_root("root_c").unwrap();

        // Test ascending order
        assert!(ern1 < ern2);
        assert!(ern2 < ern3);
        assert!(ern1 < ern3);

        // Test descending order
        assert!(ern3 > ern2);
        assert!(ern2 > ern1);
        assert!(ern3 > ern1);

        // Test equality
        let ern1_clone = ern1.clone();
        assert_eq!(ern1, ern1_clone);

        // Test sorting
        let mut erns = vec![ern3.clone(), ern1.clone(), ern2.clone()];
        erns.sort();
        assert_eq!(erns, vec![ern1, ern2, ern3]);
    }

    #[test]
    fn test_ern_unixtime_ordering() {
        let ern1: Ern = Ern::with_root("root_a").unwrap();
        sleep(Duration::from_millis(10));
        let ern2: Ern = Ern::with_root("root_b").unwrap();
        sleep(Duration::from_millis(10));
        let ern3: Ern = Ern::with_root("root_c").unwrap();

        // Test ascending order
        assert!(ern1 < ern2);
        assert!(ern2 < ern3);
        assert!(ern1 < ern3);

        // Test descending order
        assert!(ern3 > ern2);
        assert!(ern2 > ern1);
        assert!(ern3 > ern1);

        // Test equality
        let ern1_clone = ern1.clone();
        assert_eq!(ern1, ern1_clone);

        // Test sorting
        let mut erns = vec![ern3.clone(), ern1.clone(), ern2.clone()];
        erns.sort();
        assert_eq!(erns, vec![ern1, ern2, ern3]);
    }

    #[test]
    fn test_ern_timestamp_unixtime_consistency() {
        let ern_timestamp1: Ern = Ern::with_root("root_a").unwrap();
        let ern_unixtime1: Ern = Ern::with_root("root_a").unwrap();

        sleep(Duration::from_millis(10));

        let ern_timestamp2: Ern = Ern::with_root("root_b").unwrap();
        let ern_unixtime2: Ern = Ern::with_root("root_b").unwrap();

        // Ensure that the ordering is consistent between Timestamp and UnixTime
        assert_eq!(
            ern_timestamp1 < ern_timestamp2,
            ern_unixtime1 < ern_unixtime2
        );
    }
    #[test]
    fn test_ern_with_root() {
        let ern: Ern = Ern::with_root("custom_root").unwrap();
        assert!(ern.root().as_str().starts_with("custom_root"));
        assert_eq!(*ern.domain(), Domain::default());
        assert_eq!(*ern.category(), Category::default());
        assert_eq!(*ern.account(), Account::default());
        assert_eq!(*ern.parts(), Parts::default());
    }

    #[test]
    fn test_ern_with_new_root() {
        let original_ern: Ern = Ern::default();
        let new_ern: Ern = original_ern.with_new_root("new_root").unwrap();
        assert!(new_ern.root().as_str().starts_with("new_root"));
        assert_eq!(new_ern.domain(), original_ern.domain());
        assert_eq!(new_ern.category(), original_ern.category());
        assert_eq!(new_ern.account(), original_ern.account());
        assert_eq!(new_ern.parts(), original_ern.parts());
    }

    #[test]
    fn test_add_erns() -> anyhow::Result<()> {
        let parent_root = EntityRoot::from_str("root_a")?;
        let parent: Ern = Ern::new(
            Domain::from_str("acton-internal").unwrap(),
            Category::from_str("hr").unwrap(),
            Account::from_str("company123").unwrap(),
            parent_root.clone(),
            Parts(vec![
                Part::from_str("department_a").unwrap(),
                Part::from_str("team1").unwrap(),
            ]),
        );

        let child: Ern = Ern::new(
            Domain::from_str("acton-internal").unwrap(),
            Category::from_str("hr").unwrap(),
            Account::from_str("company123").unwrap(),
            EntityRoot::from_str("root_b").unwrap(),
            Parts(vec![Part::from_str("role_x").unwrap()]),
        );

        let combined: Ern = parent + child;

        assert_eq!(*combined.domain(), Domain::from_str("acton-internal").unwrap());
        assert_eq!(*combined.category(), Category::from_str("hr").unwrap());
        assert_eq!(*combined.account(), Account::from_str("company123").unwrap());
        assert_eq!(*combined.root(), parent_root);
        assert_eq!(
            *combined.parts(),
            Parts(vec![
                Part::from_str("department_a").unwrap(),
                Part::from_str("team1").unwrap(),
                Part::from_str("role_x").unwrap(),
            ])
        );
        Ok(())
    }

    #[test]
    fn test_add_erns_empty_child() {
        let parent: Ern = Ern::new(
            Domain::from_str("acton-internal").unwrap(),
            Category::from_str("hr").unwrap(),
            Account::from_str("company123").unwrap(),
            EntityRoot::from_str("rootp").unwrap(),
            Parts(vec![Part::from_str("department_a").unwrap()]),
        );

        let child: Ern = Ern::new(
            Domain::from_str("acton-internal").unwrap(),
            Category::from_str("hr").unwrap(),
            Account::from_str("company123").unwrap(),
            EntityRoot::from_str("rootc").unwrap(),
            Parts(vec![]),
        );

        let combined = parent + child;

        assert_eq!(
            *combined.parts(),
            Parts(vec![Part::from_str("department_a").unwrap()])
        );
    }

    #[test]
    fn test_add_erns_empty_parent() {
        let parent: Ern = Ern::new(
            Domain::from_str("acton-internal").unwrap(),
            Category::from_str("hr").unwrap(),
            Account::from_str("company123").unwrap(),
            EntityRoot::from_str("rootp").unwrap(),
            Parts(vec![]),
        );
        let child: Ern = Ern::new(
            Domain::from_str("acton-internal").unwrap(),
            Category::from_str("hr").unwrap(),
            Account::from_str("company123").unwrap(),
            EntityRoot::from_str("rootc").unwrap(),
            Parts(vec![Part::from_str("role_x").unwrap()]),
        );
        let combined = parent + child;
        assert_eq!(
            *combined.parts(),
            Parts(vec![Part::from_str("role_x").unwrap()])
        );
    }

    #[test]
    fn test_add_erns_display() {
        let parent: Ern = Ern::new(
            Domain::from_str("acton-internal").unwrap(),
            Category::from_str("hr").unwrap(),
            Account::from_str("company123").unwrap(),
            EntityRoot::from_str("rootp").unwrap(),
            Parts(vec![Part::from_str("department_a").unwrap()]),
        );

        let child: Ern = Ern::new(
            Domain::from_str("acton-internal").unwrap(),
            Category::from_str("hr").unwrap(),
            Account::from_str("company123").unwrap(),
            EntityRoot::from_str("rootc").unwrap(),
            Parts(vec![Part::from_str("team1").unwrap()]),
        );

        let combined = parent + child;

        assert!(
            combined
                .to_string()
                .starts_with("ern:acton-internal:hr:company123:rootp")
        );
    }
    #[test]
    fn test_ern_custom() -> anyhow::Result<()> {
        let ern: Ern = Ern::new(
            Domain::new("custom")?,
            Category::new("service")?,
            Account::new("account123")?,
            EntityRoot::new("root".to_string())?,
            Parts::new(vec![Part::new("resource")?]),
        );
        assert!(
            ern.to_string()
                .starts_with("ern:custom:service:account123:root")
        );
        Ok(())
    }

    #[test]
    fn test_ern_append_invalid_part() -> anyhow::Result<()> {
        let invalid_part = Part::new(":invalid");

        assert!(invalid_part.is_err());
        Ok(())
    }
}