use std::any::TypeId;
use std::borrow::Cow;
use std::fmt;
use std::hash::{Hash, Hasher};
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct ModuleId {
type_id: TypeId,
name: &'static str,
}
impl ModuleId {
pub fn of<M: ?Sized + 'static>() -> Self {
Self {
type_id: TypeId::of::<M>(),
name: short_type_name(std::any::type_name::<M>()),
}
}
pub(crate) fn with_name<M: ?Sized + 'static>(name: &'static str) -> Self {
Self {
type_id: TypeId::of::<M>(),
name,
}
}
pub fn name(&self) -> &'static str {
self.name
}
}
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"));
}
}