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
//! Proc-macro crate for [`ferrunix`].
//!
//! See the [`derive_inject`] macro for documentation.
//!
//! [`ferrunix`]: https://crates.io/crates/ferrunix
use FromDeriveInput;
use ;
use DeriveAttrInput;
use derive_macro_impl;
/// `#[derive(Inject)]` proc-macro for [`ferrunix`].
///
/// The `Inject` derive macro supports the two following attributes:
///
/// - `#[provides]`: Customizing the object registration.
/// - `#[inject]`: Customizing how an injected member is created.
///
/// ```rust,ignore
/// #[derive(Inject)]
/// #[provides(PROPERTY...)]
/// struct Transient {
/// #[inject(PROPERTY...)]
/// field: UserType,
/// }
/// ```
///
/// ## `provides` Properties
///
/// - `transient [= "<TYPE-SIGNATURE>"]`
/// - The object is provided as a transient registered with `<TYPE-SIGNATURE>`
/// as key. If the signature is omitted, the concrete type is used as a key.
/// - `singleton [= "<TYPE-SIGNATURE>"]`
/// - The object is provided as a singleton registered with `<TYPE-SIGNATURE>`
/// as key. If the signature is omitted, the concrete type is used as a key.
/// - `ctor = <IDENTIFIER>`
/// - The object isn't constructed using member-wise construction, but it's
/// constructed using a custom constructor (e.g., `new`). The constructor
/// will be passed the members in order of declaration as parameters.
/// - `no_registration`
/// - The type isn't registered automatically and the generated
/// `Self::register(&ferrunix::Registry)` function needs to be called
/// manually to register the type.
///
/// ## `inject` Properties
///
/// - `default`
/// - Construct the field using the `Default` implementation.
/// - `ctor = "<RUST-CODE>"`
/// - Construct the field using the provided Rust code.
/// - `transient [= true]`
/// - Construct the field as a transient by retrieving it from the `Registry`.
/// - `singleton [= true]`
/// - Construct the field as a singleton by retrieving it from the `Registry`.
///
/// ```rust,ignore,no_run
/// # #![allow(unused)]
/// use ferrunix::Inject;
///
/// pub trait Logger {}
///
/// #[derive(Inject)]
/// #[provides(transient = "dyn Logger", no_registration)]
/// // ^^^^^^^^^^^^^^
/// // The explicit type can be omitted, if it matches the concrete type.
/// pub struct MyLogger {}
///
/// impl Logger for MyLogger {}
///
/// #[derive(Inject)]
/// #[provides(singleton, no_registration)]
/// pub struct MyConfig {
/// #[inject(default)]
/// // Use the `Default::default` impl for construction.
/// counter: u32,
///
/// #[inject(ctor = r#""log-prefix: ".to_owned()"#)]
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/// // The constructor must be valid Rust code. For strings, two sets of quotes
/// // are required.
/// prefix: String,
///
/// #[inject(transient /* = true */)]
/// // ^^^^^^^^^^^^
/// // The long form with `= true` is optional.
/// logger: Box<dyn Logger>,
/// }
///
/// fn main() {
/// let registry = ferrunix::Registry::empty();
/// MyLogger::register(®istry);
/// MyConfig::register(®istry);
/// }
/// ```
///
/// [`ferrunix`]: https://crates.io/crates/ferrunix