aboutsummaryrefslogtreecommitdiff
path: root/macros/src/lib.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-27 23:41:41 +0200
committerHampusM <hampus@hampusmat.com>2022-08-27 23:41:41 +0200
commite0f90a8e384615c79d7d51c66d19294d75e79391 (patch)
treef3df3d1cd92f7d4a978feaa5a9a5f773dd0901ee /macros/src/lib.rs
parentd4078c84a83d121a4e3492955359cedb3b404476 (diff)
feat: implement named bindings
Diffstat (limited to 'macros/src/lib.rs')
-rw-r--r--macros/src/lib.rs107
1 files changed, 101 insertions, 6 deletions
diff --git a/macros/src/lib.rs b/macros/src/lib.rs
index 9b97be6..c7157c8 100644
--- a/macros/src/lib.rs
+++ b/macros/src/lib.rs
@@ -9,11 +9,12 @@ use quote::quote;
use syn::{parse, parse_macro_input};
mod declare_interface_args;
-mod dependency_type;
+mod dependency;
mod factory_type_alias;
mod injectable_impl;
mod injectable_macro_args;
mod libs;
+mod named_attr_input;
mod util;
use declare_interface_args::DeclareInterfaceArgs;
@@ -38,15 +39,60 @@ use libs::intertrait_macros::gen_caster::generate_caster;
/// declare the interface with the [`declare_interface!`] macro or use
/// the [`di_container_bind`] macro to create a DI container binding.
///
-/// # Example
+/// # Attributes
+/// Attributes specific to impls with this attribute macro.
+///
+/// ### Named
+/// Used inside the `new` method before a dependency argument. Declares the name of the
+/// dependency. Should be given the name quoted inside parenthesis.
+///
+/// The [`macro@named`] ghost attribute macro can be used for intellisense and
+/// autocompletion for this attribute.
+///
+/// For example:
/// ```
-/// use syrette::injectable;
+/// # use syrette::ptr::TransientPtr;
+/// # use syrette::injectable;
+/// #
+/// # trait IArmor {}
+/// #
+/// # trait IKnight {}
+/// #
+/// # struct Knight
+/// # {
+/// # tough_armor: TransientPtr<dyn IArmor>,
+/// # light_armor: TransientPtr<dyn IArmor>,
+/// # }
+/// #
+/// #[injectable(IKnight)]
+/// impl Knight
+/// {
+/// pub fn new(
+/// #[named("tough")]
+/// tough_armor: TransientPtr<dyn IArmor>,
///
-/// struct PasswordManager {}
+/// #[named("light")]
+/// light_armor: TransientPtr<dyn IArmor>
+/// ) -> Self
+/// {
+/// Self { tough_armor, light_armor }
+/// }
+/// }
+/// #
+/// # impl IKnight for Knight {}
+/// ```
///
+/// # Example
+/// ```
+/// # use syrette::injectable;
+/// #
+/// # struct PasswordManager {}
+/// #
/// #[injectable]
-/// impl PasswordManager {
-/// pub fn new() -> Self {
+/// impl PasswordManager
+/// {
+/// pub fn new() -> Self
+/// {
/// Self {}
/// }
/// }
@@ -191,3 +237,52 @@ pub fn declare_interface(input: TokenStream) -> TokenStream
generate_caster(&implementation, &interface).into()
}
+
+/// Declares the name of a dependency.
+///
+/// This macro attribute doesn't actually do anything. It only exists for the
+/// convenience of having intellisense and autocompletion.
+/// You might as well just use `named` if you don't care about that.
+///
+/// Only means something inside a `new` method inside a impl with
+/// the [`macro@injectable`] macro attribute.
+///
+/// # Examples
+/// ```
+/// # use syrette::ptr::TransientPtr;
+/// # use syrette::injectable;
+/// #
+/// # trait INinja {}
+/// # trait IWeapon {}
+/// #
+/// # struct Ninja
+/// # {
+/// # strong_weapon: TransientPtr<dyn IWeapon>,
+/// # weak_weapon: TransientPtr<dyn IWeapon>,
+/// # }
+/// #
+/// #[injectable(INinja)]
+/// impl Ninja
+/// {
+/// pub fn new(
+/// #[syrette::named("strong")]
+/// strong_weapon: TransientPtr<dyn IWeapon>,
+///
+/// #[syrette::named("weak")]
+/// weak_weapon: TransientPtr<dyn IWeapon>,
+/// ) -> Self
+/// {
+/// Self {
+/// strong_weapon,
+/// weak_weapon,
+/// }
+/// }
+/// }
+/// #
+/// # impl INinja for Ninja {}
+/// ```
+#[proc_macro_attribute]
+pub fn named(_: TokenStream, _: TokenStream) -> TokenStream
+{
+ TokenStream::new()
+}