use crate::descriptor::types::{
AuthoredTableName, CheckRef, ClaimRef, DoorRef, ExecutionSuite, MutationPointRef, NameRefusal,
Namespace, NamespacedName, PopulationRef, ProducerName, ProjectionRef, Role, Stem,
SubjectRoute, Tag,
};
impl Namespace {
pub const fn declared(text: &'static str) -> Result<Self, NameRefusal> {
if text.is_empty() {
return Err(NameRefusal::EmptyNamespace);
}
Ok(Self(text))
}
#[must_use]
pub const fn written(self) -> &'static str {
self.0
}
}
impl Stem {
pub const fn declared(text: &'static str) -> Result<Self, NameRefusal> {
if text.is_empty() {
return Err(NameRefusal::EmptyStem);
}
Ok(Self(text))
}
#[must_use]
pub const fn written(self) -> &'static str {
self.0
}
}
impl NamespacedName {
pub const fn named(namespace: &'static str, stem: &'static str) -> Result<Self, NameRefusal> {
let namespace = match Namespace::declared(namespace) {
Ok(namespace) => namespace,
Err(refusal) => return Err(refusal),
};
let stem = match Stem::declared(stem) {
Ok(stem) => stem,
Err(refusal) => return Err(refusal),
};
Ok(Self { namespace, stem })
}
#[must_use]
pub const fn namespace(self) -> Namespace {
self.namespace
}
#[must_use]
pub const fn stem(self) -> Stem {
self.stem
}
}
macro_rules! namespaced_reference {
(const $($reference:ident),+ $(,)?) => {
$(
namespaced_reference!(@implement $reference, pub const fn);
)+
};
($($reference:ident),+ $(,)?) => {
$(
namespaced_reference!(@implement $reference, pub fn);
)+
};
(@implement $reference:ident, $($signature:tt)+) => {
impl $reference {
$($signature)+ named(
namespace: &'static str,
stem: &'static str,
) -> Result<Self, $crate::descriptor::NameRefusal> {
match $crate::descriptor::NamespacedName::named(namespace, stem) {
Ok(name) => Ok(Self(name)),
Err(refusal) => Err(refusal),
}
}
#[must_use]
pub const fn over(name: $crate::descriptor::NamespacedName) -> Self {
Self(name)
}
#[must_use]
pub const fn name(self) -> $crate::descriptor::NamespacedName {
self.0
}
}
};
}
pub(crate) use namespaced_reference;
namespaced_reference!(
AuthoredTableName,
CheckRef,
ClaimRef,
DoorRef,
ExecutionSuite,
MutationPointRef,
PopulationRef,
ProducerName,
ProjectionRef,
Role,
SubjectRoute,
Tag,
);