acton_ern/traits/
ern_component.rs

1use crate::{Account, Category, Domain, EntityRoot, Part, Parts};
2
3/// Represents a component of an Entity Resource Name (ERN).
4///
5/// This trait is used to ensure type safety and proper ordering when building ERNs.
6/// Each component in an ERN implements this trait, defining its prefix and the
7/// type of the next component that should follow it in the ERN structure.
8///
9/// The trait is primarily used by the `ErnBuilder` to enforce the correct order
10/// of components during ERN construction.
11pub trait ErnComponent {
12    /// Returns the prefix string that should appear before this component in an ERN.
13    ///
14    /// For example, the `Domain` component has the prefix "ern:" to indicate the
15    /// start of an ERN string.
16    fn prefix() -> &'static str;
17    
18    /// The type of the next component that should follow this one in the ERN structure.
19    ///
20    /// This associated type is used by the builder pattern to enforce the correct
21    /// sequence of components. For example, `Domain::NextState` is `Category`,
22    /// indicating that a `Category` should follow a `Domain` in an ERN.
23    type NextState;
24}
25
26macro_rules! impl_ern_component {
27    ($type:ty, $prefix:expr, $next:ty) => {
28        impl ErnComponent for $type {
29            fn prefix() -> &'static str {
30                $prefix
31            }
32            type NextState = $next;
33        }
34    };
35}
36impl ErnComponent for EntityRoot {
37    fn prefix() -> &'static str {
38        ""
39    }
40    type NextState = Part;
41}
42
43impl ErnComponent for Account {
44    fn prefix() -> &'static str {
45        ""
46    }
47    type NextState = EntityRoot;
48}
49
50impl_ern_component!(Domain, "ern:", Category);
51impl_ern_component!(Category, "", Account);
52impl_ern_component!(Part, "", Parts);
53
54/// Implementation for the `Parts` component of an ERN.
55///
56/// The `Parts` component represents a collection of path parts in the ERN.
57/// Its `NextState` is itself, allowing for multiple parts to be added.
58impl ErnComponent for Parts {
59    fn prefix() -> &'static str {
60        ":"
61    }
62    type NextState = Parts;
63}