use std::str::FromStr;
use crate::errors::ErnError;
use crate::{Account, Category, Domain, EntityRoot, Part, Parts};
pub trait ErnComponent {
fn prefix() -> &'static str;
type NextState;
fn build_root(_value: &str) -> Option<Result<EntityRoot, ErnError>> {
None
}
}
macro_rules! impl_ern_component {
($type:ty, $prefix:expr, $next:ty) => {
impl ErnComponent for $type {
fn prefix() -> &'static str {
$prefix
}
type NextState = $next;
}
};
}
impl ErnComponent for EntityRoot {
fn prefix() -> &'static str {
""
}
type NextState = Part;
fn build_root(value: &str) -> Option<Result<EntityRoot, ErnError>> {
Some(EntityRoot::from_str(value))
}
}
impl ErnComponent for Account {
fn prefix() -> &'static str {
""
}
type NextState = EntityRoot;
}
impl_ern_component!(Domain, "ern:", Category);
impl_ern_component!(Category, "", Account);
impl_ern_component!(Part, "", Parts);
impl ErnComponent for Parts {
fn prefix() -> &'static str {
":"
}
type NextState = Parts;
}