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
//! Public macros for ergonomic service resolution.
// --- Foundational Macros ---
/// Resolves a service from a specific container instance, returning an `Option`.
///
/// This is the non-panicking, foundational macro for resolution. It allows you to
/// work with any container instance (including `Container` and `LocalContainer`).
///
/// # Returns
///
/// `Option<Arc<T>>` or `Option<Rc<T>>`, depending on the container type.
/// Returns `None` if the service is not found.
///
/// # Panics
///
/// This macro will still panic if a circular dependency is detected during resolution.
///
/// # Examples
///
/// ```
/// use fibre_ioc::{Container, maybe_resolve_from};
/// let container = Container::new();
/// container.add_instance(String::from("hello"));
///
/// // Resolve an existing service
/// let message = maybe_resolve_from!(&container, String);
/// assert_eq!(*message.unwrap(), "hello");
///
/// // Attempt to resolve a non-existent service
/// let missing = maybe_resolve_from!(&container, i32);
/// assert!(missing.is_none());
/// ```
/// Resolves a required service from a specific container instance.
///
/// This is the panicking, foundational macro for resolution. It is intended for
/// resolving dependencies that are essential for the application's function.
///
/// # Panics
///
/// This macro will panic if the service cannot be resolved or if a circular
/// dependency is detected.
///
/// # Examples
///
/// ```
/// use fibre_ioc::{Container, resolve_from};
/// let container = Container::new();
/// container.add_instance(String::from("hello"));
///
/// // Resolve an existing service
/// let message = resolve_from!(&container, String);
/// assert_eq!(*message, "hello");
/// ```
// --- Global Container Macros ---
/// Resolves an optional service from the global container.
///
/// This macro is the primary way to get optional dependencies. It returns an `Option`
/// containing the service if it is registered, or `None` if it is not.
///
/// # Returns
///
/// `Option<Arc<T>>`.
///
/// # Examples
///
/// ```
/// use fibre_ioc::{global, maybe_resolve};
///
/// global().add_instance(String::from("hello"));
///
/// // Resolve an existing service
/// let message = maybe_resolve!(String);
/// assert!(message.is_some());
///
/// // Attempt to resolve a non-existent service
/// let missing = maybe_resolve!(i32);
/// assert!(missing.is_none());
/// ```
/// Resolves a required service from the global container.
///
/// This macro is the primary way to get essential dependencies. It panics if the
/// requested service is not registered, ensuring that all required
/// dependencies are present at runtime.
///
/// This macro is a convenience wrapper around `resolve_from!(global(), ...)`.
///
/// # Panics
///
/// This macro will panic if the service cannot be resolved. For a non-panicking
/// version, use `maybe_resolve!(...)`.
///
/// # Examples
///
/// ```
/// use fibre_ioc::{global, resolve};
/// use std::sync::Arc;
///
/// // Register a simple type
/// global().add_singleton(None, || String::from("hello"));
///
/// // Resolve it
/// let message = resolve!(String);
/// assert_eq!(*message, "hello");
/// ```