use std::any::TypeId;
use std::fmt;
pub trait FrameworkComponent: Send + Sync + 'static {
fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
fn type_id(&self) -> TypeId {
TypeId::of::<Self>()
}
}
#[allow(async_fn_in_trait)]
pub trait Initializable {
type Config;
type Error: std::error::Error + Send + Sync + 'static;
async fn initialize(&mut self, config: Self::Config) -> Result<(), Self::Error>;
fn is_initialized(&self) -> bool;
}
#[allow(async_fn_in_trait)]
pub trait Finalizable {
type Error: std::error::Error + Send + Sync + 'static;
async fn finalize(&mut self) -> Result<(), Self::Error>;
}
pub trait Validatable {
type Error: std::error::Error + Send + Sync + 'static;
fn validate(&self) -> Result<(), Self::Error>;
}
pub trait CloneableComponent: FrameworkComponent + Clone {}
impl<T> CloneableComponent for T where T: FrameworkComponent + Clone {}
pub trait Service: FrameworkComponent {
fn service_id(&self) -> String {
self.type_name().to_string()
}
}
#[allow(async_fn_in_trait)]
pub trait ServiceFactory: Send + Sync + 'static {
type Service: Service;
type Config;
type Error: std::error::Error + Send + Sync + 'static;
async fn create_service(&self, config: Self::Config) -> Result<Self::Service, Self::Error>;
}
pub trait Singleton: Service {}
pub trait Transient: Service {}
impl fmt::Debug for dyn FrameworkComponent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FrameworkComponent")
.field("type_name", &self.type_name())
.field("type_id", &self.type_id())
.finish()
}
}
impl fmt::Debug for dyn Service {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Service")
.field("service_id", &self.service_id())
.field("type_name", &self.type_name())
.finish()
}
}