yadir

Crates.ioyadir
lib.rsyadir
version0.3.2
sourcesrc
created_at2024-07-15 17:12:08.636184
updated_at2024-07-21 21:13:04.670638
descriptionYet Another Dependency Injection Registry for Rust
homepagehttps://github.com/WarriorsSami/yadir
repositoryhttps://github.com/WarriorsSami/yadir
max_upload_size
id1304053
size28,590
Bărbuț-Dică Sami (WarriorsSami)

documentation

README

Yadir

GitHub Actions Workflow Status Crates.io Version

Yadir is yet another dependency injection registry for Rust.


About

Yadir's API is heavily inspired by the Microsoft.Extensions.DependencyInjection library for .NET. It provides a simple and easy way to register and resolve dependencies in your Rust application at runtime.

Its initial implementation is based on the Registry design pattern, formulated by Will Crichton in his book about Type-Driven API Design in Rust.

Usage

Add Yadir to your Cargo.toml file:

[dependencies]
yadir = { version = "0.3.0", features = ["derive"] }

Create a new registry and register your dependencies, after implementing the DIBuilder trait for each one of them:

use yadir::core::contracts::DIBuilder;
use yadir::core::primitives::{DIManager, DIObj};
use yadir::{deps, let_deps};
use async_trait::async_trait;
use dyn_clone::{clone_trait_object, DynClone};
use yadir_derive::DIBuilder;

clone_trait_object!(Printer);
clone_trait_object!(Writer);

trait Printer: Sync + Send + DynClone {
    fn print(&self) -> String;
}

trait Writer: Sync + Send + DynClone {
    fn write(&self) -> String;
}

#[derive(Clone, DIBuilder)]
#[build_as(Box<dyn Printer>)]
struct Bar;

impl Printer for Bar {
    fn print(&self) -> String {
        "bar".to_string()
    }
}

#[derive(Clone, DIBuilder)]
#[build_as(Box<dyn Writer>)]
struct Baz;

impl Writer for Baz {
    fn write(&self) -> String {
        "baz".to_string()
    }
}

#[derive(Clone, DIBuilder)]
#[build_method("new")]
struct Foo {
    #[deps]
    printer: Box<dyn Printer>,
    #[deps]
    writer: Box<dyn Writer>,
}

impl Foo {
    fn new(printer: Box<dyn Printer>, writer: Box<dyn Writer>) -> Self {
        Self { printer, writer }
    }

    fn print(&self) -> String {
        format!("foo {} {}", self.printer.print(), self.writer.write())
    }
}

#[tokio::main]
async fn main() {
    let mut manager = DIManager::default();
    manager.build::<Bar>().await;
    manager.build::<Baz>().await;
    
    let foo = manager.build::<Foo>().await.unwrap().extract();
    
    assert_eq!(foo.print(), "foo bar baz");
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Commit count: 0

cargo fmt