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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#![allow(dead_code)]

use crate::{helpers::service_container, service::Service, Handler, Injectable, Singleton};
use std::{
    any::{Any, TypeId},
    collections::HashMap,
    sync::{Arc, OnceLock, RwLock},
};

pub(crate) static SERVICE_CONTAINER: OnceLock<Arc<ServiceContainer>> = OnceLock::new();

pub struct ServiceContainer {
    services: RwLock<HashMap<TypeId, Box<dyn Any + Send + Sync + 'static>>>,
    in_proxy_mode: bool,
}

impl Default for ServiceContainer {
    fn default() -> Self {
        Self::new()
    }
}

impl ServiceContainer {
    pub(crate) fn new() -> Self {
        Self {
            services: RwLock::new(HashMap::new()),
            in_proxy_mode: false,
        }
    }

    /// Create an instance of the container in proxy mode
    /// A proxy container is a container that creates a
    /// limited scope but will reach out to the global service
    /// container when an instance of a type does not exist locally.
    ///
    /// This allows a new instance of a type to be created and use in
    /// a specific scope
    pub fn proxy() -> Self {
        Self {
            services: RwLock::new(HashMap::new()),
            in_proxy_mode: true,
        }
    }

    /// Returns the proxy state of the current container
    pub fn is_proxy(&self) -> bool {
        self.in_proxy_mode
    }

    /// Checks if the current container is in proxy mode.
    /// If that is the case, it tries to find the instance of the
    /// type, falls back to the main service container
    pub fn proxy_value<T: Clone + 'static>(&self) -> Option<T> {
        if self.is_proxy() {
            self.get_type::<T>()
        } else {
            None
        }
    }

    /// Tries to find the instance of the type wrapped in `Service<T>`
    pub fn get<T: 'static>(&self) -> Option<Service<T>> {
        self.get_type::<Service<T>>()
    }

    /// Tries to find the instance of the type wrapped in `Service<T>`
    /// if an instance does not exist, one will be injected
    pub async fn get_or_inject<T: Injectable + Send + Sync + 'static>(&self) -> Service<T> {
        let result = self.get::<T>();

        if result.is_none() {
            let instance = T::inject(self).await;
            return self.set(instance).get::<T>().unwrap();
        }

        result.unwrap()
    }

    /// Tries to find the instance of the type T
    /// if an instance does not exist, one will be injected
    pub async fn get_type_or_inject<T: Injectable + Clone + Send + Sync + 'static>(&self) -> T {
        let result = self.get_type::<T>();
        if result.is_none() {
            let instance = T::inject(self).await;
            self.set_type(instance.clone());
            return instance;
        }

        result.unwrap()
    }

    /// Tries to find the "raw" instance of the type
    pub fn get_type<T: Clone + 'static>(&self) -> Option<T> {
        if let Ok(services) = self.services.read() {
            let result: Option<&T> = services
                .get(&TypeId::of::<T>())
                .and_then(|b| b.downcast_ref());

            if let Some(service) = result {
                return Some(service.clone());
            } else if self.is_proxy() {
                return service_container().get_type();
            }
        }
        None
    }

    /// Stores the instance
    pub fn set_type<T: Clone + Send + Sync + 'static>(&self, ext: T) -> &Self {
        if let Ok(mut list) = self.services.write() {
            list.insert(TypeId::of::<T>(), Box::new(ext));
        }
        self
    }

    /// Stores the instance as `Service<T>`
    /// You need to use "get" in order to retrieve the instance
    pub fn set<T: Send + Sync + 'static>(&self, ext: T) -> &Self {
        self.set_type(Service::new(ext))
    }

    /// Takes an async function or closure and executes it
    /// Require arguments are injected during the call. All arguments must implement
    /// Injectable.
    ///
    /// This method does not check for existing instance
    pub async fn inject_and_call<F, Args>(&self, handler: F) -> F::Output
    where
        F: Handler<Args>,
        Args: Injectable + 'static,
    {
        let args = Args::inject(self).await;
        handler.call(args).await
    }

    /// Given a tuple of types, this method will try to resolve them
    /// and return a tuple of instances
    /// The types must implement Injectable.
    ///
    /// This method does not check for existing instance of the types.
    pub async fn inject_all<Args>(&self) -> Args
    where
        Args: Injectable + 'static,
    {
        Args::inject(self).await
    }

    /// Given a type, this method will try to call the `inject` method
    /// implemented on the type. It does not check the container for existing
    /// instance.
    pub async fn provide<T: Injectable + Send + Sync + 'static>(&self) -> T {
        T::inject(self).await
    }

    /// Given a type, this method will try to find an instance of the type
    /// wrapped in a `Service<T>` that is currently registered in the service
    /// container.
    pub async fn service<T: 'static>(&self) -> Service<T> {
        Service::inject(self).await
    }

    /// Given a type, this method will try to find an existing instance of the
    /// type. If that fails, an instance of the type is
    /// initialized, wrapped in a `Service`, stored and
    /// a copy is returned. Subsequent call requesting instance of that type will
    /// returned. If the this is a proxy container, the instance will be dropped with
    /// this container goes out of scope.
    pub async fn singleton<T: Injectable + Sized + Send + Sync + 'static>(&self) -> Singleton<T> {
        Singleton::inject(self).await
    }
}
pub struct ServiceContainerBuilder {
    items: HashMap<TypeId, Box<dyn Any + Send + Sync + 'static>>,
}

impl Default for ServiceContainerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ServiceContainerBuilder {
    pub fn new() -> Self {
        Self {
            items: HashMap::new(),
        }
    }

    pub fn register<T: Clone + Send + Sync + 'static>(mut self, ext: T) -> Self {
        self.items.insert(TypeId::of::<T>(), Box::new(ext));
        self
    }

    /// T is wrapped in a `Service`
    /// This means to get T back you need to specify `Service<T>`
    ///  or use the "get" method on the container
    pub fn service<T: Send + Sync + 'static>(mut self, ext: T) -> Self {
        self.items
            .insert(TypeId::of::<Service<T>>(), Box::new(Service::new(ext)));
        self
    }

    /// Instantiate and returns the service container
    pub fn build(self) -> Arc<ServiceContainer> {
        let container = SERVICE_CONTAINER.get_or_init(|| Arc::new(ServiceContainer::default()));
        if let Ok(mut services) = container.services.write() {
            for (k, v) in self.items {
                services.insert(k, v);
            }
        }
        container.clone()
    }
}

#[cfg(test)]
mod test {
    use async_trait::async_trait;

    use super::*;

    #[derive(Debug, Clone)]
    struct Counter {
        start_point: usize,
    }

    #[async_trait]
    impl Injectable for Counter {
        async fn inject(container: &ServiceContainer) -> Self {
            container
                .get_type()
                .unwrap_or_else(|| Counter { start_point: 44 })
        }
    }

    #[derive(Debug, Clone)]
    struct User {
        id: i32,
    }

    #[async_trait]
    impl Injectable for User {
        async fn inject(_: &ServiceContainer) -> Self {
            Self { id: 1000 }
        }
    }

    #[tokio::test]
    async fn test_builder() {
        let container = ServiceContainerBuilder::new()
            .service(5usize)
            .register(true)
            .build();

        assert_eq!(*container.get::<usize>().unwrap(), 5usize);
        assert_eq!(container.get_type::<bool>(), Some(true));
    }

    #[tokio::test]
    async fn test_empty_container() {
        let container = ServiceContainer::new();

        assert_eq!(container.get::<i32>().is_none(), true);
        assert_eq!(container.get_type::<i32>(), None);
    }

    #[tokio::test]
    async fn test_getting_raw_type() {
        let container = ServiceContainer::new();
        container.set_type(400);
        container.set_type(300f32);
        container.set_type(true);

        assert_eq!(container.get_type::<i32>(), Some(400));
        assert_eq!(container.get_type::<f32>(), Some(300f32));
        assert_eq!(container.get_type::<bool>(), Some(true));
    }

    #[tokio::test]
    async fn test_getting_service_type() {
        let container = ServiceContainer::new();
        container.set(400);
        container.set(300f32);
        container.set(true);

        assert_eq!(*container.get::<i32>().unwrap(), 400);
        assert_eq!(*container.get::<f32>().unwrap(), 300f32);
        assert_eq!(*container.get::<bool>().unwrap(), true);
    }

    #[tokio::test]
    async fn test_proxy_service() {
        service_container().set_type(true);
        let container = ServiceContainer::proxy();

        let is_true: Option<bool> = container.get_type();
        let an_i32: Option<i32> = container.get_type();

        assert_eq!(is_true, Some(true));
        assert_eq!(an_i32, None);

        container.set_type(30000);
        let rate_per_hour: Option<i32> = container.get_type();
        assert_eq!(rate_per_hour, Some(30000));
    }

    #[tokio::test]
    async fn test_injecting() {
        let container = ServiceContainer::new();
        let counter = container.inject_all::<Counter>().await;

        assert_eq!(counter.start_point, 44usize);
    }

    #[tokio::test]
    async fn test_injecting_stored_instance() {
        let container = ServiceContainer::new();
        container.set_type(Counter { start_point: 6000 });

        let counter = container.inject_all::<Counter>().await;
        assert_eq!(counter.start_point, 6000usize);
    }

    #[tokio::test]
    async fn test_singleton() {
        let container = ServiceContainer::new();

        let user = container.singleton::<User>().await;
        assert_eq!(user.id, 1000);

        container.set_type(User { id: 88 });
        let user = container.singleton::<User>().await;
        assert_eq!(user.id, 1000);
    }

    #[tokio::test]
    async fn test_inject_and_call() {
        let container = ServiceContainer::new();

        let result = container
            .inject_and_call(|user: User, counter: Counter| async move {
                assert_eq!(user.id, 1000);
                assert_eq!(counter.start_point, 44);
                (1, 2, 3)
            })
            .await;

        assert_eq!(result, (1, 2, 3));
    }

    #[tokio::test]
    async fn test_get_or_inject_raw_type() {
        let container = ServiceContainer::new();
        assert_eq!(container.get_type::<User>().is_none(), true);

        let a_user = container.get_type_or_inject::<User>().await;
        let a_user2 = container.get_type::<User>();

        assert_eq!(a_user.id, 1000);
        assert_eq!(a_user2.is_some(), true);
        assert_eq!(a_user2.unwrap().id, a_user.id);
    }

    #[tokio::test]
    async fn test_get_or_inject_service_type() {
        let container = ServiceContainer::new();

        assert_eq!(container.get::<User>().is_none(), true);

        let a_user = container.get_or_inject::<User>().await;
        let a_user2 = container.get::<User>();

        assert_eq!(a_user.id, 1000);
        assert_eq!(a_user2.is_some(), true);
        assert_eq!(a_user2.unwrap().id, a_user.id);
    }
}