cgp_context

Attribute Macro cgp_context 

Source
#[cgp_context]
Expand description

The #[cgp_context] macro is used when defining a CGP context.

The macro can be used with a struct definition. An optional identifier can be provided to be used as the name of the provider for the context. If not provided, the context provider would be named in the format {struct_name}Components.

The macro generates a struct definition for the context provider, and implements HasCgpProvider for the context struct to point to the context provider.

§Example

Given the following context definition:

#[cgp_context]
pub struct MyApp {
    name: String,
}

would be equivalent to the following fully explicit definition:

#[cgp_context(MyAppComponents)]
pub struct MyApp {
    name: String,
}

which would generate the following constructs:

struct MyAppComponents;

impl HasCgpProvider for MyApp {
    type CgpProvider = MyAppComponents;
}

§Preset Inheritance

The macro also allows the context provider to inherit its component mappings from a specified preset. The preset can be specified following a :, after the context provider name is specified.

The context provider would implement DelegateComponent for all keys in the preset, with the Delegate target pointing to Preset::Provider. This is done through the IsPreset trait generated from the cgp_preset! macro.

For example, given the following definition:

#[cgp_context(MyAppComponents: MyPreset)]
pub struct MyApp {
    name: String,
}

The following blanket implementation would be generated:

impl<Name> DelegateComponent<Name> for MyAppComponents
where
    Self: MyPreset::IsPreset<Name>,
{
    type Delegate = MyPreset::Provider;
}