Je-DI, compile time Hierarchical dependency injection framework
Basic usage
struct DBConnection(String);
impl DBConnection {
fn new(str: &str) -> std::io::Result<Self> { Ok(Self(str.to_string())) }
}
#[derive(Clone)]
struct Client;
impl Client {
pub fn new() -> Self { Self }
pub async fn call_service(&self, url: &str) -> std::io::Result<u64> {
Ok(1)
}
}
pub struct World {
pub service_url: &'static str,
pub http_client: Client,
pub db_connection: DBConnection
}
pub struct ServiceClient {
url: &'static str,
client: Client
}
use je_di::FromAsyncWorld;
use je_di::async_trait;
#[async_trait]
impl FromAsyncWorld for ServiceClient {
type World<'a> = World;
type Error = std::io::Error;
async fn from_world<'a>(world: &'a Self::World<'a>) -> Result<Self, Self::Error> {
Ok(Self { url: world.service_url, client: world.http_client.clone() })
}
}
pub struct MeId(u64);
#[async_trait]
impl FromAsyncDependency for MeId {
type World<'a> = World;
type Error = std::io::Error;
type Dependency = ServiceClient;
async fn from_dependency(world: &Self::World<'_>, dependency: &Self::Dependency) -> Result<Self, Self::Error> {
let this = dependency.call_service(&dependency.url).await?;
Self(this)
}
}