aboutsummaryrefslogtreecommitdiff
path: root/macros/src/dependency_type.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-07-27 19:10:30 +0200
committerHampusM <hampus@hampusmat.com>2022-07-31 12:17:51 +0200
commitf75a7d58135825c4f9094c1e10f36de4a952f455 (patch)
treebd8bae08591b7355407ee44002e0494bdd8d2268 /macros/src/dependency_type.rs
parent3388f857b32cf1893d7b54582c8fd16e4965550b (diff)
feat: add injecting singletons into constructors
Diffstat (limited to 'macros/src/dependency_type.rs')
-rw-r--r--macros/src/dependency_type.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/macros/src/dependency_type.rs b/macros/src/dependency_type.rs
new file mode 100644
index 0000000..35f810e
--- /dev/null
+++ b/macros/src/dependency_type.rs
@@ -0,0 +1,40 @@
+use proc_macro2::Ident;
+use syn::{GenericArgument, PathArguments, Type, TypePath};
+
+pub struct DependencyType
+{
+ pub interface: Type,
+ pub ptr: Ident,
+}
+
+impl DependencyType
+{
+ pub fn from_type_path(type_path: &TypePath) -> Option<Self>
+ {
+ // Assume the type path has a last segment.
+ let last_path_segment = type_path.path.segments.last().unwrap();
+
+ let ptr = &last_path_segment.ident;
+
+ match &last_path_segment.arguments {
+ PathArguments::AngleBracketed(angle_bracketed_generic_args) => {
+ let generic_args = &angle_bracketed_generic_args.args;
+
+ let opt_first_generic_arg = generic_args.first();
+
+ // Assume a first generic argument exists because TransientPtr,
+ // SingletonPtr and FactoryPtr requires one
+ let first_generic_arg = opt_first_generic_arg.as_ref().unwrap();
+
+ match first_generic_arg {
+ GenericArgument::Type(first_generic_arg_type) => Some(Self {
+ interface: first_generic_arg_type.clone(),
+ ptr: ptr.clone(),
+ }),
+ &_ => None,
+ }
+ }
+ &_ => None,
+ }
+ }
+}