aboutsummaryrefslogtreecommitdiff
path: root/macros/src/dependency_type.rs
blob: 35f810e05bc9155f7527f87b6f53ff6f2a794f54 (plain)
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
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,
        }
    }
}