#configuration #process #dependency-injection

instancebuilder

Convenient way of managing dependency injection

2 unstable releases

0.2.0 Jul 30, 2024
0.1.1 Mar 27, 2024

#1128 in Rust patterns

Download history 1/week @ 2024-08-28 12/week @ 2024-09-04 16/week @ 2024-09-11 8/week @ 2024-09-18 4/week @ 2024-09-25 2/week @ 2024-10-02 5/week @ 2024-10-09 44/week @ 2024-10-16 38/week @ 2024-10-23 17/week @ 2024-10-30 68/week @ 2024-11-06

86 downloads per month

MIT license

9KB
88 lines

instancebuilder

Handy solution for dependency injection in Rust.

Installation

cargo add instancebuilder

Examples

Simple

use ::std::convert::Infallible;
use ::instancebuilder::{Error, InstanceBuilder, FromInstanceBuilder};

// Example set of data of any type. Must implement Send + Sync to be threadsafe.
struct TestConfig {
    key: String,
}

// Your implementation that needs to get build.
struct TestImplementation {
    message: String,
}

impl FromInstanceBuilder for TestImplementation {
    fn try_from_builder(builder: &InstanceBuilder) -> Result<Self, Error> {
        // Put here the code necessary to build the instance
        let config: &TestConfig = builder.data()?;

        Ok(Self {
            message: config.key.clone(),
        })
    }
}

fn main() {
    // Test object to inject. This can be a database pool or a shared instance of something
    let config = TestConfig {
        key: String::from("help me!"),
    };

    let mut  builder = InstanceBuilder::new();
    // Add dependency object to the builder
    builder.insert(config);

    // Build instance of the implementation
    // dependencies are injected by the `FromInstanceBuilder` trait implementation
    let instance = builder.build::<TestImplementation>().unwrap();
}

Nested dependencies

use ::std::convert::Infallible;
use ::instancebuilder::{Error, InstanceBuilder, FromInstanceBuilder};

// Example set of data of any type. Must implement Send + Sync to be threadsafe.
struct TestConfig {
    key: String,
}

// Nested dependent struct
struct InnerTestImplementation {
    message: String,
}

impl FromInstanceBuilder for InnerTestImplementation {
    fn try_from_builder(builder: &InstanceBuilder) -> Result<Self, Error> {
        // Put here the code necessary to build the instance
        // the builder instance contains the initialized data
        let config: &TestConfig = builder.data()?;
        
        Ok(Self {
            message: config.key.clone(),
        })
    }
}

// Outer struct that depends on the nested one
struct OuterTestImplementation {
    inner: InnerTestImplementation,
}

impl FromInstanceBuilder for OuterTestImplementation {
    fn try_from_builder(builder: &InstanceBuilder) -> Result<Self, Error> {
        // Put here the code necessary to build the instance
        Ok(Self {
            // Builds dependency `InnerTestImplementation`
            inner: builder.build()?,
        })
    }
}

fn main() {
    // Test object to inject. This can be a database pool or a shared instance of something
    let config = TestConfig {
        key: String::from("help me!"),
    };

    let mut  builder = InstanceBuilder::new();
    // Add dependency object to the builder
    builder.insert(config);

    // Build instance of the implementation
    // dependencies are injected by the `FromInstanceBuilder` trait implementation
    let instance = builder.build::<OuterTestImplementation>().unwrap();
}

No runtime deps