Skip to main content

khive_types/
entity_alias.rs

1//! Entity aliases: other exact names of an entity, each with the origin that
2//! wrote it (ADR-221 section 2).
3
4use alloc::string::String;
5
6/// Who wrote an entity alias (ADR-221 section 2).
7#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
10pub enum EntityAliasOrigin {
11    /// A caller named it on `create` or `update(add_aliases=...)`.
12    Caller,
13    /// A rename kept the entity's previous name.
14    Rename,
15    /// A merge carried the removed entity's name or aliases.
16    Merge,
17}
18
19impl EntityAliasOrigin {
20    /// The stored spelling of this origin.
21    pub fn as_str(self) -> &'static str {
22        match self {
23            Self::Caller => "caller",
24            Self::Rename => "rename",
25            Self::Merge => "merge",
26        }
27    }
28
29    /// Parse a stored origin; `None` for any other spelling.
30    pub fn parse(value: &str) -> Option<Self> {
31        match value {
32            "caller" => Some(Self::Caller),
33            "rename" => Some(Self::Rename),
34            "merge" => Some(Self::Merge),
35            _ => None,
36        }
37    }
38}
39
40/// Another exact name of an entity, with the origin that wrote it.
41#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
42#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
43#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
44pub struct EntityAlias {
45    /// The alias, exactly as stored.
46    pub alias: String,
47    /// Who wrote it.
48    pub origin: EntityAliasOrigin,
49}