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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::any::Any;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::{any::TypeId, collections::HashMap};

pub struct DIContext {
    pub containers: HashMap<TypeId, Box<dyn Any>>,
    wait_list: Vec<Box<dyn IProvider + 'static>>,
}

unsafe impl Send for DIContext {}
unsafe impl Sync for DIContext {}
impl UnwindSafe for DIContext {}
impl RefUnwindSafe for DIContext {}

impl std::fmt::Debug for DIContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DIContext")
            .field("containers", &self.containers)
            .field("wait_list.len", &self.wait_list.len())
            .finish()
    }
}

impl DIContext {
    pub fn new() -> Self {
        DIContext {
            containers: HashMap::new(),
            wait_list: vec![],
        }
    }

    pub fn register(&mut self, value: Box<dyn Any>) {
        let type_id = (&*value).type_id();
        if self.containers.contains_key(&type_id) {
            return;
        }

        self.containers.insert(type_id, value);
    }

    pub fn register_lazy<T: 'static>(&mut self, injectable: Box<dyn IProvider>) {
        self.wait_list.push(injectable);
    }

    pub fn get<T: 'static>(&self) -> Option<&T> {
        match self.containers.get(&TypeId::of::<T>()) {
            Some(value) => value.downcast_ref::<T>(),
            None => None,
        }
    }
}

impl DIContext {
    fn import_from_modules(&mut self, root_module: Box<dyn crate::IModule>) {
        let providers = root_module.providers();

        for provider in providers {
            self.wait_list.push(provider);
        }

        let child_modules = root_module.child_modules();

        for child_module in child_modules {
            self.import_from_modules(child_module);
        }
    }

    pub fn initialize(&mut self, root_module: Box<dyn crate::IModule>) {
        self.import_from_modules(root_module);

        while self.wait_list.len() > 0 {
            let mut has_ready_provider = false;

            let mut i = 0;
            for provider in self.wait_list.iter() {
                let dependencies = provider.dependencies();

                let mut is_ready = true;

                for dependency in dependencies {
                    if !self.containers.contains_key(&dependency) {
                        is_ready = false;
                        break;
                    }
                }

                if is_ready {
                    let provided_value = provider.provide(self);
                    self.register(provided_value);
                    self.wait_list.remove(i);
                    has_ready_provider = true;

                    break;
                }

                i += 1;
            }

            if !has_ready_provider {
                panic!("No provider is ready");
            }
        }
    }
}

pub trait IProvider {
    fn dependencies(&self) -> Vec<TypeId> {
        vec![]
    }

    fn provide(&self, di_context: &DIContext) -> Box<dyn Any>;
}