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
/*
    Appellation: appellation <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::prelude::{Classifier, Identifier};

/// An appellation is defined to be a name or title given to a person or thing; often being used in the context of wine.
/// Here, the `Appellation` is a unique object identifier that is composed of three independent parts:
///     - Classifier
///     - Identifier
///     - Name
///
/// While the name is typically referenced by external users, the classifier and id are used to navigate through computational space.
pub trait Appellation {
    type Class: Classifier;
    type Id: Identifier;

    fn class(&self) -> &Self::Class;

    fn id(&self) -> &Self::Id;

    fn name(&self) -> String;

    fn slug(&self) -> String {
        self.name().to_lowercase().replace(" ", "-")
    }

    fn type_id(&self) -> core::any::TypeId
    where
        Self: Sized + 'static,
    {
        core::any::TypeId::of::<Self>()
    }
}

pub trait FromAppellation<Cls, Id>
where
    Cls: Classifier,
    Id: Identifier,
{
    fn from_appellation(appellation: impl Appellation<Class = Cls, Id = Id>) -> Self;
}

pub trait TryFromAppellation<Cls, Id>
where
    Cls: Classifier,
    Id: Identifier,
    Self: Sized,
{
    type Error;
    fn try_from_appellation(
        appellation: impl Appellation<Class = Cls, Id = Id>,
    ) -> Result<Self, Self::Error>;
}

pub trait IntoAppellation<Cls, Id>
where
    Cls: Classifier,
    Id: Identifier,
{
    fn into_appellation(self) -> dyn Appellation<Class = Cls, Id = Id>;
}

impl<Cls, Id, T> Appellation for (Cls, Id, T)
where
    Cls: Classifier,
    Id: Identifier,
    T: ToString,
{
    type Class = Cls;
    type Id = Id;

    fn class(&self) -> &Cls {
        &self.0
    }

    fn id(&self) -> &Id {
        &self.1
    }

    fn name(&self) -> String {
        self.2.to_string()
    }
}