11use 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 } ;
44
55use crate :: derive_component:: component_name:: derive_component_name_struct;
66use crate :: derive_component:: consumer_impl:: derive_consumer_impl;
77use crate :: derive_component:: provider_impl:: derive_provider_impl;
88use 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;
911use crate :: parse:: ComponentSpec ;
1012
1113pub fn derive_component_with_ast (
@@ -15,12 +17,14 @@ pub fn derive_component_with_ast(
1517 let provider_name = & spec. provider_name ;
1618 let context_type = & spec. context_type ;
1719
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) ?;
2024
2125 let provider_trait = derive_provider_trait (
22- & spec . component_name ,
23- & spec . component_params ,
26+ component_name,
27+ component_params,
2428 & consumer_trait,
2529 provider_name,
2630 context_type,
@@ -32,8 +36,17 @@ pub fn derive_component_with_ast(
3236 context_type,
3337 & consumer_trait,
3438 & 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,
3750 ) ?;
3851
3952 let derived = DerivedComponent {
@@ -42,6 +55,8 @@ pub fn derive_component_with_ast(
4255 provider_trait,
4356 consumer_impl,
4457 provider_impl,
58+ use_context_impl,
59+ use_context_is_provider_impl,
4560 } ;
4661
4762 Ok ( derived)
@@ -53,6 +68,8 @@ pub struct DerivedComponent {
5368 pub provider_trait : ItemTrait ,
5469 pub consumer_impl : ItemImpl ,
5570 pub provider_impl : ItemImpl ,
71+ pub use_context_impl : ItemImpl ,
72+ pub use_context_is_provider_impl : ItemImpl ,
5673}
5774
5875impl ToTokens for DerivedComponent {
@@ -62,5 +79,7 @@ impl ToTokens for DerivedComponent {
6279 self . provider_trait . to_tokens ( tokens) ;
6380 self . consumer_impl . to_tokens ( tokens) ;
6481 self . provider_impl . to_tokens ( tokens) ;
82+ self . use_context_impl . to_tokens ( tokens) ;
83+ self . use_context_is_provider_impl . to_tokens ( tokens) ;
6584 }
6685}
0 commit comments