1
1
use proc_macro2:: TokenStream ;
2
- use quote:: ToTokens ;
3
- use syn:: { ItemImpl , ItemStruct , ItemTrait } ;
2
+ use quote:: { quote , ToTokens } ;
3
+ use syn:: { parse2 , ItemImpl , ItemStruct , ItemTrait } ;
4
4
5
5
use crate :: derive_component:: component_name:: derive_component_name_struct;
6
6
use crate :: derive_component:: consumer_impl:: derive_consumer_impl;
7
7
use crate :: derive_component:: provider_impl:: derive_provider_impl;
8
8
use crate :: derive_component:: provider_trait:: derive_provider_trait;
9
+ use crate :: derive_component:: use_context_impl:: derive_use_context_impl;
10
+ use crate :: derive_provider:: derive_is_provider_for;
9
11
use crate :: parse:: ComponentSpec ;
10
12
11
13
pub fn derive_component_with_ast (
@@ -15,12 +17,14 @@ pub fn derive_component_with_ast(
15
17
let provider_name = & spec. provider_name ;
16
18
let context_type = & spec. context_type ;
17
19
18
- let component_struct =
19
- derive_component_name_struct ( & spec. component_name , & spec. component_params ) ?;
20
+ let component_name = & spec. component_name ;
21
+ let component_params = & spec. component_params ;
22
+
23
+ let component_struct = derive_component_name_struct ( component_name, component_params) ?;
20
24
21
25
let provider_trait = derive_provider_trait (
22
- & spec . component_name ,
23
- & spec . component_params ,
26
+ component_name,
27
+ component_params,
24
28
& consumer_trait,
25
29
provider_name,
26
30
context_type,
@@ -32,8 +36,17 @@ pub fn derive_component_with_ast(
32
36
context_type,
33
37
& consumer_trait,
34
38
& provider_trait,
35
- & spec. component_name ,
36
- & spec. component_params ,
39
+ component_name,
40
+ component_params,
41
+ ) ?;
42
+
43
+ let use_context_impl = derive_use_context_impl ( context_type, & consumer_trait, & provider_trait) ?;
44
+
45
+ let use_context_is_provider_impl = derive_is_provider_for (
46
+ & parse2 ( quote ! {
47
+ #component_name < #component_params >
48
+ } ) ?,
49
+ & use_context_impl,
37
50
) ?;
38
51
39
52
let derived = DerivedComponent {
@@ -42,6 +55,8 @@ pub fn derive_component_with_ast(
42
55
provider_trait,
43
56
consumer_impl,
44
57
provider_impl,
58
+ use_context_impl,
59
+ use_context_is_provider_impl,
45
60
} ;
46
61
47
62
Ok ( derived)
@@ -53,6 +68,8 @@ pub struct DerivedComponent {
53
68
pub provider_trait : ItemTrait ,
54
69
pub consumer_impl : ItemImpl ,
55
70
pub provider_impl : ItemImpl ,
71
+ pub use_context_impl : ItemImpl ,
72
+ pub use_context_is_provider_impl : ItemImpl ,
56
73
}
57
74
58
75
impl ToTokens for DerivedComponent {
@@ -62,5 +79,7 @@ impl ToTokens for DerivedComponent {
62
79
self . provider_trait . to_tokens ( tokens) ;
63
80
self . consumer_impl . to_tokens ( tokens) ;
64
81
self . provider_impl . to_tokens ( tokens) ;
82
+ self . use_context_impl . to_tokens ( tokens) ;
83
+ self . use_context_is_provider_impl . to_tokens ( tokens) ;
65
84
}
66
85
}
0 commit comments