use thiserror::Error;
#[derive(Error, Debug)]
pub enum DiError {
#[error("Service not registered: {type_name}")]
ServiceNotRegistered { type_name: String },
#[error("Keyed service not registered: {type_name} with key '{key}'")]
KeyedServiceNotRegistered { type_name: String, key: String },
#[error("Circular dependency detected for service: {type_name}")]
CircularDependency { type_name: String },
#[error("Scope has been disposed")]
ScopeDisposed,
#[error("Failed to create service: {type_name} - {reason}")]
ServiceCreationFailed { type_name: String, reason: String },
#[error("Service already registered: {type_name}")]
ServiceAlreadyRegistered { type_name: String },
#[error("Type casting failed for service: {type_name}")]
TypeCastingFailed { type_name: String },
#[error("Scoped service '{type_name}' cannot be resolved from root container")]
ScopedServiceInRootContainer { type_name: String },
#[error("Generic error: {message}")]
Generic { message: String },
}
impl DiError {
pub fn service_not_registered<T: 'static>() -> Self {
Self::ServiceNotRegistered {
type_name: std::any::type_name::<T>().to_string(),
}
}
pub fn keyed_service_not_registered<T: 'static>(key: &str) -> Self {
Self::KeyedServiceNotRegistered {
type_name: std::any::type_name::<T>().to_string(),
key: key.to_string(),
}
}
pub fn circular_dependency<T: 'static>() -> Self {
Self::CircularDependency {
type_name: std::any::type_name::<T>().to_string(),
}
}
pub fn service_creation_failed<T: 'static>(reason: &str) -> Self {
Self::ServiceCreationFailed {
type_name: std::any::type_name::<T>().to_string(),
reason: reason.to_string(),
}
}
pub fn service_already_registered<T: 'static>() -> Self {
Self::ServiceAlreadyRegistered {
type_name: std::any::type_name::<T>().to_string(),
}
}
pub fn type_casting_failed<T: 'static>() -> Self {
Self::TypeCastingFailed {
type_name: std::any::type_name::<T>().to_string(),
}
}
pub fn scoped_service_in_root_container<T: 'static>() -> Self {
Self::ScopedServiceInRootContainer {
type_name: std::any::type_name::<T>().to_string(),
}
}
pub fn generic(message: &str) -> Self {
Self::Generic {
message: message.to_string(),
}
}
}
pub type DiResult<T> = Result<T, DiError>;