elif_core/foundation/
traits.rs1use std::any::TypeId;
2use std::fmt;
3
4pub trait FrameworkComponent: Send + Sync + 'static {
6 fn type_name(&self) -> &'static str {
8 std::any::type_name::<Self>()
9 }
10
11 fn type_id(&self) -> TypeId {
13 TypeId::of::<Self>()
14 }
15}
16
17#[allow(async_fn_in_trait)]
19pub trait Initializable {
20 type Config;
21 type Error: std::error::Error + Send + Sync + 'static;
22
23 async fn initialize(&mut self, config: Self::Config) -> Result<(), Self::Error>;
25
26 fn is_initialized(&self) -> bool;
28}
29
30#[allow(async_fn_in_trait)]
32pub trait Finalizable {
33 type Error: std::error::Error + Send + Sync + 'static;
34
35 async fn finalize(&mut self) -> Result<(), Self::Error>;
37}
38
39pub trait Validatable {
41 type Error: std::error::Error + Send + Sync + 'static;
42
43 fn validate(&self) -> Result<(), Self::Error>;
45}
46
47pub trait CloneableComponent: FrameworkComponent + Clone {}
49
50impl<T> CloneableComponent for T where T: FrameworkComponent + Clone {}
51
52pub trait Service: FrameworkComponent {
54 fn service_id(&self) -> String {
56 self.type_name().to_string()
57 }
58}
59
60#[allow(async_fn_in_trait)]
62pub trait ServiceFactory: Send + Sync + 'static {
63 type Service: Service;
64 type Config;
65 type Error: std::error::Error + Send + Sync + 'static;
66
67 async fn create_service(&self, config: Self::Config) -> Result<Self::Service, Self::Error>;
69}
70
71pub trait Singleton: Service {}
73
74pub trait Transient: Service {}
76
77impl fmt::Debug for dyn FrameworkComponent {
78 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79 f.debug_struct("FrameworkComponent")
80 .field("type_name", &self.type_name())
81 .field("type_id", &self.type_id())
82 .finish()
83 }
84}
85
86impl fmt::Debug for dyn Service {
87 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88 f.debug_struct("Service")
89 .field("service_id", &self.service_id())
90 .field("type_name", &self.type_name())
91 .finish()
92 }
93}