aboutsummaryrefslogtreecommitdiff
path: root/examples/factory/bootstrap.rs
blob: 5543ee522d2f7346fd2c60a5afd631c66ee71b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::error::Error;

use syrette::ptr::TransientPtr;
use syrette::DIContainer;

use crate::interfaces::user::{IUser, IUserFactory};
use crate::interfaces::user_manager::IUserManager;
use crate::user::User;
use crate::user_manager::UserManager;

pub fn bootstrap() -> Result<DIContainer, Box<dyn Error>>
{
    let mut di_container = DIContainer::new();

    di_container
        .bind::<dyn IUserManager>()
        .to::<UserManager>()?;

    di_container.bind::<IUserFactory>().to_factory(&|_| {
        Box::new(move |name, date_of_birth, password| {
            let user: TransientPtr<dyn IUser> =
                TransientPtr::new(User::new(name, date_of_birth, password));

            user
        })
    })?;

    Ok(di_container)
}