use std::any::TypeId;
use std::borrow::Cow;
use std::fmt;
use std::hash::{Hash, Hasher};
#[derive(Clone, Copy, Debug)]
pub struct ModuleId {
type_id: TypeId,
name: &'static str,
type_name: &'static str,
}
impl ModuleId {
pub fn of<M: ?Sized + 'static>() -> Self {
let type_name = std::any::type_name::<M>();
Self {
type_id: TypeId::of::<M>(),
name: short_type_name(type_name),
type_name,
}
}
pub fn named<M: ?Sized + 'static>(name: &'static str) -> ModuleId {
Self {
type_id: TypeId::of::<M>(),
name,
type_name: std::any::type_name::<M>(),
}
}
pub fn name(&self) -> &'static str {
self.name
}
pub fn type_name(&self) -> &'static str {
self.type_name
}
}
impl PartialEq for ModuleId {
fn eq(&self, other: &Self) -> bool {
self.type_id == other.type_id && self.name == other.name
}
}
impl Eq for ModuleId {}
impl Hash for ModuleId {
fn hash<H: Hasher>(&self, state: &mut H) {
self.type_id.hash(state);
self.name.hash(state);
}
}
impl fmt::Display for ModuleId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name)
}
}
#[derive(Clone, Debug)]
pub struct ProviderKey {
type_id: TypeId,
type_name: &'static str,
name: Option<Cow<'static, str>>,
}
impl ProviderKey {
pub fn of<T: ?Sized + 'static>() -> Self {
Self {
type_id: TypeId::of::<T>(),
type_name: std::any::type_name::<T>(),
name: None,
}
}
pub fn named<T: ?Sized + 'static>(name: impl Into<Cow<'static, str>>) -> Self {
Self {
name: Some(name.into()),
..Self::of::<T>()
}
}
pub fn type_name(&self) -> &'static str {
self.type_name
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub(crate) fn type_id(&self) -> TypeId {
self.type_id
}
}
impl PartialEq for ProviderKey {
fn eq(&self, other: &Self) -> bool {
self.type_id == other.type_id && self.name == other.name
}
}
impl Eq for ProviderKey {}
impl Hash for ProviderKey {
fn hash<H: Hasher>(&self, state: &mut H) {
self.type_id.hash(state);
self.name.hash(state);
}
}
impl fmt::Display for ProviderKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.name {
Some(name) => write!(f, "{}(\"{name}\")", self.type_name),
None => f.write_str(self.type_name),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub enum Scope {
#[default]
Singleton,
Request,
Transient,
}
impl fmt::Display for Scope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Scope::Singleton => "singleton",
Scope::Request => "request",
Scope::Transient => "transient",
})
}
}
pub(crate) fn short_type_name(full: &'static str) -> &'static str {
let generics = full.find('<').unwrap_or(full.len());
match full[..generics].rfind("::") {
Some(pos) => &full[pos + 2..],
None => full,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn short_type_name_strips_paths_but_keeps_generics() {
assert_eq!(short_type_name("app::users::UsersModule"), "UsersModule");
assert_eq!(short_type_name("Plain"), "Plain");
assert_eq!(short_type_name("a::Wrap<b::C>"), "Wrap<b::C>");
}
#[test]
fn named_keys_differ_from_unnamed_and_display_their_name() {
let plain = ProviderKey::of::<u8>();
let named = ProviderKey::named::<u8>("analytics");
assert_ne!(plain, named);
assert_eq!(named, ProviderKey::named::<u8>(String::from("analytics")));
assert_eq!(named.to_string(), "u8(\"analytics\")");
assert_eq!(named.name(), Some("analytics"));
}
struct Plain;
struct Wrap<T>(T);
struct Pair<A, B>(A, B);
#[test]
fn module_ids_carry_the_full_type_path_and_compare_as_before() {
let plain = ModuleId::of::<Plain>();
assert_eq!(plain.type_name(), "navree_kernel::key::tests::Plain");
assert_eq!(plain.name(), "Plain");
assert_eq!(plain, ModuleId::of::<Plain>());
assert_eq!(
ModuleId::of::<Wrap<Plain>>().type_name(),
"navree_kernel::key::tests::Wrap<navree_kernel::key::tests::Plain>"
);
assert_eq!(
ModuleId::of::<Pair<Plain, String>>().type_name(),
"navree_kernel::key::tests::Pair<navree_kernel::key::tests::Plain, alloc::string::String>"
);
assert_eq!(
ModuleId::of::<&'static Plain>().type_name(),
"&navree_kernel::key::tests::Plain"
);
assert_eq!(
ModuleId::of::<(Plain, u8)>().type_name(),
"(navree_kernel::key::tests::Plain, u8)"
);
assert_eq!(
ModuleId::of::<[Plain; 1]>().type_name(),
"[navree_kernel::key::tests::Plain; 1]"
);
let mut ids = std::collections::HashSet::new();
ids.insert(ModuleId::of::<Plain>());
assert!(ids.contains(&ModuleId::named::<Plain>("Plain")));
}
#[test]
fn module_ids_named_differently_are_two_modules() {
assert_ne!(
ModuleId::named::<Plain>("upi"),
ModuleId::named::<Plain>("cards")
);
assert_eq!(
ModuleId::named::<Plain>("upi").type_name(),
ModuleId::of::<Plain>().type_name()
);
struct Payments(&'static str);
impl crate::Module for Payments {
fn id(&self) -> ModuleId {
ModuleId::named::<Payments>(self.0)
}
fn register(&self, _: &mut crate::ModuleBuilder) {}
}
let report = crate::Navree::app()
.module(Payments("upi"))
.module(Payments("cards"))
.validate()
.expect("two modules of one type");
let names: Vec<&str> = report.modules.iter().map(|m| m.name).collect();
assert_eq!(names, ["upi", "cards", "App"]);
}
}