di 0.1.2

Dependency injection container.
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*!

Contains utilities such as `Registry` to build centralized value
construction `Container`.
*/

use std::any::Any;
use std::collections::{ BTreeMap, BTreeSet, HashMap };
use std::collections::btree_map;

use metafactory::{ ToMetaFactory, MetaFactory };
use metafactory::aggregate::{ Aggregate };

use container::Container;

use self::new_definition::{ NewDefinition };
use self::candidate::{ GroupCandidate, DefinitionCandidate };
use self::error::{ CompileError, CircularDependency };

mod candidate;

pub mod new_definition;
pub mod error;
pub mod validator;

/**
Use `Registry` to build and initialize `Container`.

The `Registry` is mutable container of value factory `definitions` and their
groups. Definition is added using `one` method variants, method group
is added by registering a definition that belongs to group using `one_of`
method variants.

Definition value must implement `ToMetafactory` trait. Currently possible
definition sources are any clonable value or closure.

## `One` definitions

```
let mut registry = di::Registry::new();

// Insert value from cloneable source immediately, but no arguments possible.
registry.insert_one("a", 5i);

// Same, but from closure.
registry.insert_one("a", || 5i);

// Same, but using the fluent interface.
registry
    .one("a", || 5i)
    .insert();

// When definition requires arguments, their source need to be explicitly
// defined using "with_arg", "with_args" or "add_arg":
registry
    .one("b", |a: int| a + 1i)
    .with_arg("a")
    .insert();

// The definition will have a value that can be known to compiler at compile
// time:
registry
    .one("sum", |a: int, b:int| a + b) // "sum" will be "int"
    .with_args(&["a", "b"])
    .insert();
```

## `One_of` definition groups

```
let mut registry = di::Registry::new();

// Definition can be assigned to a group.
registry
    .one("a", 5i)
    .in_group("integers")
    .insert();

// If the name "a" is not important, "one_of" method can be used:
registry.insert_one_of("integers", 5i);

// The same can be written using fluent interface:
registry
    .one_of("integers", 5i)
    .insert();

// If using the fluent interface, the name can be added back:
registry
    .one_of("integers", 5i)
    .with_id("a")
    .insert();

// Group members can depend on other definitions:
registry
    .one_of("integers", |previous: int| previous + 1)
    .with_arg("a")
    .with_id("b")
    .insert();

// Defined "integers" group can be used as Vec<int> argument in other
// definitions:
registry
    .one("sum", |items: Vec<int>| items.into_iter().fold(0i, |a, i| a + i))
    .with_arg("integers")
    .insert();
```

## Compiling the container

When the registry is configured, we can check for errors and build the
`Container`, that contains clonable factories for all the registered
definitions and definition groups.

This is recommended pattern:

```
let mut registry = di::Registry::new();

// Build the registry.
registry.insert_one("a", 5i32);
// < registry ... >

match registry.compile() {
    Ok(container) => {
        // Get a factory which will be used for the application lifetime
        // to construct a value.
        if let Some(a_factory) = container.get::<i32>("a") {
            assert_eq!(5, a_factory.take());
        }
    },
    Err(errors) => di::error_printer::pretty_print(&errors),
}
```

The errors contains detailed information about all errors at once. Console
applications can use provided pretty printer.
*/
pub struct Registry {
    /// Contains a list of group candidates.
    groups: BTreeMap<String, GroupCandidate>,
    /// Contains a list of definition candidates.
    definitions: BTreeMap<String, DefinitionCandidate>,
    /// Contains a list of definitions that were overriden while building
    /// the registry - so we can at least show some kind of warning.
    overriden_definitions: BTreeMap<String, Vec<DefinitionCandidate>>,
    /// Validator list that is run before the compilation.
    validators: Vec<Box<validator::Validator + 'static>>,
}

impl Registry {
    /// Produces a new `Registry`.
    ///
    /// ## Example
    ///
    /// ```
    /// let mut registry = di::Registry::new();
    /// ```
    pub fn new() -> Registry {
        let mut registry = Registry {
            groups: BTreeMap::new(),
            definitions: BTreeMap::new(),
            overriden_definitions: BTreeMap::new(),
            validators: Vec::new(),
        };

        registry.push_validator(validator::argument_count::ArgumentCountValidator);
        registry.push_validator(validator::overrides::NoOverridesValidator);
        registry.push_validator(validator::dependencies::DependencyValidator);

        registry
    }

    fn push_validator<T: validator::Validator + 'static>(
        &mut self,
        validator: T
    ) {
        self.validators.push(box validator);
    }

    /**
    Compile a new `Container` that contains validated factories for
    all definitions.

    ## Example

    ```
    # let mut registry = di::Registry::new();
    match registry.compile() {
        Ok(container) => {
            // Validated successfuly, can be used.
        },
        Err(errors) => di::error_printer::pretty_print(&errors),
    }
    ```
    */
    pub fn compile(&self) -> Result<Container, Vec<CompileError>> {
        let mut error_summary = Vec::<CompileError>::new();

        for validator in self.validators.iter() {
            validator.validate(self, &mut error_summary);
        }

        let mut factory_map = HashMap::<String, Box<Any>>::new();
        let groups = self.collect_group_dependencies();

        if error_summary.len() == 0 {
            // Compile definitions.
            for id in self.definitions.keys() {
                let create_factory_result = self.create_factory(
                    &groups,
                    &mut Vec::<String>::new(),
                    id.as_slice()
                );
                match create_factory_result {
                    Err(error) => {
                        error_summary.push(CompileError::CircularDependency(error));
                        break;
                    },
                    Ok(factory) => { factory_map.insert(id.to_string(), factory); },
                };
            }
            // Compile groups.
            for group in groups.keys() {
                let create_factory_result = self.create_factory(
                    &groups,
                    &mut Vec::<String>::new(),
                    group.as_slice()
                );
                match create_factory_result {
                    Err(error) => {
                        error_summary.push(CompileError::CircularDependency(error));
                        break;
                    },
                    Ok(factory) => { factory_map.insert(group.clone(), factory); },
                };
            }
        }

        if error_summary.len() == 0 {
            Ok(Container::new(factory_map))
        } else {
            Err(error_summary)
        }
    }

    /**
    Insert a new definition without arguments.

    ## Example

    ```
    # let mut registry = di::Registry::new();
    registry.insert_one("a", 5i32); // Clonable value
    registry.insert_one("b", || -> i32 { 5 }); // Closure

    match registry.compile() {
        Ok(container) => {
            if let Some(a) = container.get::<i32>("a") {
                assert_eq!(5, a.take());
            }
            if let Some(b) = container.get::<i32>("b") {
                assert_eq!(5, b.take());
            }
        },
        Err(errors) => di::error_printer::pretty_print(&errors),
    }
    ```

    */
    pub fn insert_one<T: 'static + ToMetaFactory>(&mut self, id: &str, value: T) {
        self.define(
            None,
            id,
            value.to_metafactory(),
            Vec::new()
        );
    }

    /**
    Insert a new definition into a group, without arguments.

    Definition group has the Vec<T> for the definition of T type.

    ## Example

    ```
    # let mut registry = di::Registry::new();
    registry.insert_one_of("a", 1i32); // Clonable value
    registry.insert_one_of("a", || -> i32 { 2 }); // Closure

    match registry.compile() {
        Ok(container) => {
            if let Some(a) = container.get::<Vec<i32>>("a") {
                assert_eq!(vec![ 1, 2 ], a.take());
            }
        },
        Err(errors) => di::error_printer::pretty_print(&errors),
    }
    ```
    */
    pub fn insert_one_of<T: 'static + ToMetaFactory>(&mut self, collection_id: &str, value: T) {
        let mut id;
        let metafactory = value.to_metafactory();

        self.define_group_if_not_exists(collection_id, metafactory.new_aggregate());

        if let Some(group) = self.groups.get_mut(collection_id) {
            group.member_count += 1;
            id = format!("{}`{}", collection_id, group.member_count);
        } else {
            panic!("Expected to find defined group.")
        }

        self.define(
            Some(collection_id.to_string()),
            id.as_slice(),
            metafactory,
            Vec::new()
        );
    }

    /**
    Insert a new definition using fluent interface.

    This allows to specify definition dependencies and use closures with
    arguments.

    Browse [`NewDefinition`](new_definition/struct.NewDefinition.html)
    documentation for complete examples.

    ## Example

    ```
    # let mut registry = di::Registry::new();
    registry
        .one("a", || -> i32 { 5 })
        .insert();

    registry
        .one("b", |a: i32| -> i32 { a + 3 })
        .with_arg("a")
        .insert();

    match registry.compile() {
        Ok(container) => {
            if let Some(a) = container.get::<i32>("a") {
                assert_eq!(5, a.take());
            }
            if let Some(b) = container.get::<i32>("b") {
                assert_eq!(5 + 3, b.take());
            }
        },
        Err(errors) => di::error_printer::pretty_print(&errors),
    }
    ```
    */
    pub fn one<'r, T: 'static + ToMetaFactory>(&'r mut self, id: &str, value: T)
        -> NewDefinition<'r>
    {
        NewDefinition::new(
            self,
            None,
            id,
            value.to_metafactory()
        )
    }

    /**
    Insert a new definition into a group using fluent interface.

    This allows to specify definition dependencies and use closures with
    arguments.

    Definition group has the Vec<T> for the definition of T type.

    ## Example

    ```
    # let mut registry = di::Registry::new();
    registry
        .one_of("values", 1i32)
        .with_id("a")
        .insert();

    registry
        .one_of("values", |a: i32| -> i32 { a + 2 })
        .with_arg("a")
        .insert();

    match registry.compile() {
        Ok(container) => {
            if let Some(a) = container.get::<Vec<i32>>("values") {
                assert_eq!(vec![ 1, 1 + 2 ], a.take());
            }
        },
        Err(errors) => di::error_printer::pretty_print(&errors),
    }
    ```
    */
    pub fn one_of<'r, T: 'static + ToMetaFactory>(&'r mut self, collection_id: &str, value: T)
        -> NewDefinition<'r>
    {
        let mut id;
        let metafactory = value.to_metafactory();

        self.define_group_if_not_exists(collection_id, metafactory.new_aggregate());

        if let Some(group) = self.groups.get_mut(collection_id) {
            group.member_count += 1;
            id = format!("{}`{}", collection_id, group.member_count);
        } else {
            panic!("Expected to find defined group.")
        }

        NewDefinition::new(
            self,
            Some(collection_id.to_string()),
            id.as_slice(),
            metafactory
        )
    }

    /**
    Specify that a group can be empty.

    ## Example

    ```
    # let mut registry = di::Registry::new();
    registry
        .one("sum", |values: Vec<i32>| values.into_iter().fold(0i32, |a, i| a + 1))
        .with_arg("values")
        .insert();

    // If there is no definition of any member of "values", our "sum"
    // will still work and receive empty list.
    registry.may_be_empty::<i32>("values");

    match registry.compile() {
        Ok(container) => {
            if let Some(sum) = container.get::<i32>("sum") {
                assert_eq!(0, sum.take());
            }
        },
        Err(errors) => di::error_printer::pretty_print(&errors),
    }
    ```
    */
    pub fn may_be_empty<T: 'static>(&mut self, collection_id: &str) {
        self.define_group_if_not_exists(collection_id, Aggregate::new::<T>());
    }

    fn collect_group_dependencies<'r>(&'r self) -> BTreeMap<String, BTreeSet<&'r str>> {
        let mut groups: BTreeMap<String, BTreeSet<&str>> = BTreeMap::new();

        for (id, value) in self.definitions.iter()
            .filter(|&(_, v)| v.collection_id != None)
        {
            match groups.entry(value.collection_id.clone().unwrap()) {
                btree_map::Entry::Occupied(mut entry) => {
                    entry.get_mut().insert(id.as_slice());
                },
                btree_map::Entry::Vacant(entry) => {
                    let mut set: BTreeSet<&str> = BTreeSet::new();
                    set.insert(id.as_slice());
                    entry.set(set);
                }
            }
        }

        for id in self.groups.keys() {
            if !groups.contains_key(id) {
                groups.insert(id.to_string(), BTreeSet::new());
            }
        }

        groups
    }

    fn define_group_if_not_exists(&mut self, collection_id: &str, type_aggregate: Aggregate<'static>) {
        if !self.groups.contains_key(collection_id) {
            self.groups.insert(
                collection_id.to_string(),
                GroupCandidate::new(type_aggregate)
            );
        }
    }

    fn define(&mut self, collection_id: Option<String>, id: &str, value: Box<MetaFactory + 'static>, args: Vec<String>) {
        if let Some(overriden_candidate) = self.definitions.remove(id) {
            match self.overriden_definitions.entry(id.to_string()) {
                btree_map::Entry::Vacant(entry) => { entry.set(vec![overriden_candidate]); },
                btree_map::Entry::Occupied(mut entry) => { entry.get_mut().push(overriden_candidate); },
            };
        }

        let candidate = DefinitionCandidate::new(
            value,
            args,
            collection_id
        );

        self.definitions.insert(
            id.to_string(),
            candidate
        );
    }

    fn create_factory(
        &self,
        groups: &BTreeMap<String, BTreeSet<&str>>,
        dependency_chain: &mut Vec<String>,
        id: &str
    ) -> Result<Box<Any>, CircularDependency> {
        dependency_chain.push(id.to_string());

        // Find if this is a definition or a group.
        let mut result = match self.definitions.get(id) {
            Some(definition) => {
                // Check for circular dependencies.

                for source in definition.arg_sources.iter() {
                    if dependency_chain.contains(source) {
                        dependency_chain.push(source.clone());
                        return Err(CircularDependency::new(dependency_chain.clone()));
                    }
                }

                // Create definition factory.

                Some(self.create_definition_factory(
                    groups,
                    dependency_chain,
                    id,
                    definition
                ))
            },
            None => None,
        };

        if let None = result {
            result = match self.groups.get(id) {
                Some(group) => {
                    // Collect users of this group.
                    let group_dependencies = groups.get(id).expect(format!("expected to find dependencies for group {}", id).as_slice());

                    // Check for circular dependencies.
                    for source in group_dependencies.iter() {
                        if dependency_chain.contains(&source.to_string()) {
                            dependency_chain.push(source.to_string());
                            return Err(CircularDependency::new(dependency_chain.clone()));
                        }
                    }

                    Some(self.create_group_factory(
                        groups,
                        dependency_chain,
                        group,
                        group_dependencies
                    ))
                },
                None => None,
            }
        }

        dependency_chain.pop();

        result.expect(format!("expected definition or group {} was not found", id).as_slice())
    }

    fn create_definition_factory(
        &self,
        groups: &BTreeMap<String, BTreeSet<&str>>,
        dependency_chain: &mut Vec<String>,
        id: &str,
        definition: &DefinitionCandidate
    ) -> Result<Box<Any>, CircularDependency> {

        let mut argument_factories = Vec::<Box<Any>>::with_capacity(definition.arg_sources.len());

        for source in definition.arg_sources.iter() {
            match self.create_factory(
                groups,
                dependency_chain,
                source.as_slice()
            ) {
                Err(error) => return Err(error),
                Ok(factory) => argument_factories.push(factory),
            };
        }

        Ok(
            definition.metafactory.new(argument_factories)
                .ok()
                .expect(
                    format!(
                        "failed to create factory {} with arguments \"{}\" - most likely argument types are not the same",
                        id,
                        definition.arg_sources.connect("\", \"")
                    ).as_slice()
                )
        )
    }

    fn create_group_factory(
        &self,
        groups: &BTreeMap<String, BTreeSet<&str>>,
        dependency_chain: &mut Vec<String>,
        group: &GroupCandidate,
        group_sources: &BTreeSet<&str>
    )
        -> Result<Box<Any>, CircularDependency>
    {
        let mut argument_factories = Vec::<Box<Any>>::with_capacity(group_sources.len());

        for source in group_sources.iter() {
            match self.create_factory(
                groups,
                dependency_chain,
                *source
            ) {
                Err(error) => return Err(error),
                Ok(factory) => argument_factories.push(factory),
            };
        }

        Ok(group.aggregate.new_factory(
            argument_factories
        ))
    }
}