dilib_derive/
lib.rs

1mod constructor;
2mod dependency;
3mod helpers;
4mod keys;
5mod target;
6mod utils;
7
8use crate::target::parse_derive_inject;
9use proc_macro::TokenStream;
10
11/// Provides an implementation of the `Inject` trait for the given type.
12///
13/// # Configuration
14/// You can use `#[inject]` on the fields to configure how the values are injected.
15///
16/// - `#[inject(name="name")]`: To get a named dependency.
17/// - `#[inject(default)]`: To set `Default::default()` for the field.
18/// - `#[inject(default=literal)]`: To set the literal value for the field.
19/// - `#[inject(constructor=name(arg1, arg2, ...))]`: To set the constructor to use.
20/// - `#[inject(scope="scoped")]`: To get a scoped dependency. This is the default.
21/// - `#[inject(scope="singleton")]`: To get a singleton dependency. If the field type is `Arc<T>` or `Singleton<T>` this is the default.
22///
23/// # Example
24/// ```rust,no_run
25/// use std::sync::{Arc, Mutex};
26/// use dilib_derive::Inject;
27///
28/// #[derive(Inject)]
29/// #[inject(constructor="new(counter, api_key, id)")]
30/// struct MyService {
31///     #[inject(default=1_usize)]
32///     id: usize,
33///
34///     #[inject(name="API_KEY")]
35///     api_key: String,
36///
37///     #[inject(scope="singleton")]
38///     counter: Arc<Mutex<usize>>
39/// }
40///
41/// impl MyService {
42///     pub fn new(counter: Arc<Mutex<usize>>, api_key: String, id: usize) -> Self {
43///         MyService { counter, api_key, id }
44///     }
45/// }
46/// ```
47///
48#[proc_macro_derive(Inject, attributes(inject))]
49pub fn derive_injectable_attribute(item: TokenStream) -> TokenStream {
50    let input = syn::parse_macro_input!(item);
51    parse_derive_inject(input).expand().into()
52}