acton_ern/traits/
ern_component.rs

1use crate::{Account, Category, Domain, EntityRoot, Part, Parts};
2
3/// Represents a component of a ERN (Entity Resource Name) (Acton Resource Name) that ensures type safety and ordering.
4pub trait ErnComponent {
5    /// Returns the prefix string that should appear before this component in a ERN (Entity Resource Name).
6    fn prefix() -> &'static str;
7    /// The type of the next ERN (Entity Resource Name) component in the sequence.
8    type NextState;
9}
10
11macro_rules! impl_ern_component {
12    ($type:ty, $prefix:expr, $next:ty) => {
13        impl ErnComponent for $type {
14            fn prefix() -> &'static str {
15                $prefix
16            }
17            type NextState = $next;
18        }
19    };
20}
21impl ErnComponent for EntityRoot {
22    fn prefix() -> &'static str {
23        ""
24    }
25    type NextState = Part;
26}
27
28impl ErnComponent for Account {
29    fn prefix() -> &'static str {
30        ""
31    }
32    type NextState = EntityRoot;
33}
34
35impl_ern_component!(Domain, "ern:", Category);
36impl_ern_component!(Category, "", Account);
37impl_ern_component!(Part, "", Parts);
38
39/// Implementation for the `Parts` component of a ERN (Entity Resource Name).
40impl ErnComponent for Parts {
41    fn prefix() -> &'static str {
42        ":"
43    }
44    type NextState = Parts;
45}