use std::any::TypeId;
use std::collections::HashSet;
use std::fmt;
use std::hash::{Hash, Hasher};
use crate::type_name::TypeName;
#[derive(Clone, Copy)]
pub struct Key {
ty: TypeName,
qualifier: TypeName,
}
impl Key {
pub fn of<T: ?Sized + 'static, Q: 'static>() -> Key {
Key { ty: TypeName::of::<T>(), qualifier: TypeName::of::<Q>() }
}
pub fn type_name(&self) -> TypeName {
self.ty
}
pub(crate) fn requalified<Q: 'static>(self) -> Key {
Key { qualifier: TypeName::of::<Q>(), ..self }
}
pub(crate) fn with_qualifier(self, qualifier: TypeId, q_name: &'static str) -> Key {
Key { qualifier: TypeName::from_parts(qualifier, q_name), ..self }
}
pub(crate) fn from_parts(
ty: TypeId,
ty_name: &'static str,
qualifier: TypeId,
q_name: &'static str,
) -> Key {
Key { ty: TypeName::from_parts(ty, ty_name), qualifier: TypeName::from_parts(qualifier, q_name) }
}
pub(crate) fn type_id(&self) -> TypeId {
self.ty.id()
}
pub(crate) fn qualifier_id(&self) -> TypeId {
self.qualifier.id()
}
pub(crate) fn qualifier(&self) -> Option<TypeName> {
(!self.is_unqualified()).then_some(self.qualifier)
}
pub(crate) fn qualifier_name(&self) -> Option<&'static str> {
self.qualifier().map(|q| q.full())
}
pub(crate) fn is_unqualified(&self) -> bool {
self.qualifier.id() == TypeId::of::<()>()
}
pub(crate) fn type_names(self) -> impl Iterator<Item = TypeName> {
std::iter::once(self.ty).chain(self.qualifier())
}
pub(crate) fn name(&self, kind: BindingKind) -> KeyName {
KeyName { key: *self, kind }
}
fn text(&self, full: bool) -> String {
self.text_with(|name| if full { format!("{name:#}") } else { name.to_string() })
}
pub(crate) fn text_in(&self, full: &HashSet<TypeName>) -> String {
self.text_with(|name| name.written(full))
}
fn text_with(&self, name: impl Fn(TypeName) -> String) -> String {
let mut text = role_spelling(&name(self.ty));
if let Some(q) = self.qualifier() {
text.push_str(" @ ");
text.push_str(&name(q));
}
text
}
}
impl PartialEq for Key {
fn eq(&self, other: &Self) -> bool {
self.ty == other.ty && self.qualifier == other.qualifier
}
}
impl Eq for Key {}
impl Hash for Key {
fn hash<H: Hasher>(&self, state: &mut H) {
self.ty.hash(state);
self.qualifier.hash(state);
}
}
impl fmt::Display for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(&self.text(f.alternate()))
}
}
impl fmt::Debug for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum BindingKind {
Single,
Collection,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyName {
key: Key,
kind: BindingKind,
}
impl KeyName {
pub fn key(&self) -> Key {
self.key
}
pub fn kind(&self) -> BindingKind {
self.kind
}
}
impl KeyName {
pub(crate) fn text_in(&self, full: &HashSet<TypeName>) -> String {
self.with_kind(self.key.text_in(full))
}
fn with_kind(&self, mut text: String) -> String {
if self.kind == BindingKind::Collection {
text.push_str(" (collection)");
}
text
}
}
impl fmt::Display for KeyName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(&self.with_kind(self.key.text(f.alternate())))
}
}
impl fmt::Debug for KeyName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
pub(crate) fn role_spelling(text: &str) -> String {
let Some(rest) = text.strip_prefix("dyn ") else { return text.to_owned() };
let (path, tail) = rest.split_at(rest.find('<').unwrap_or(rest.len()));
let (prefix, last) = match path.rfind("::") {
Some(end) => path.split_at(end + 2),
None => ("", path),
};
let alias = match last {
"ErasedGuard" => "AnyGuard",
"ErasedInterceptor" => "AnyInterceptor",
"ErasedErrorHandler" => "AnyErrorHandler",
_ => return text.to_owned(),
};
format!("{prefix}{alias}{tail}")
}