Skip to content

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented May 6, 2025

Summary

This PR enhances the #[cgp_component, #[cgp_provider] and #[cgp_context] macros, so that the attribute arguments can be simplified.

#[cgp_component]

If the user only wants to specify the provider key, the expression can be simplified from #[cgp_component{ provider: ProviderName } to #[cgp_component(ProviderName].

#[cgp_provider]

If no attribute is provided, the component name is deduced from the provider name, in the format "{ProviderName}Component"

#[cgp_context]

If no attribute is provided, the context provider name is deduced from the context struct name, in the format "{ContextName}Components".

Example

Prior to this, the macro requires at least the component name and the provider name to be specified, such as:

#[cgp_component {
    provider: Greeter,
}]
pub trait CanGreet {
    fn greet(&self);
}

#[cgp_new_provider(GreeterComponent)]
impl<Context> Greeter<Context> for GreetHello {
    fn greet(&self) {
        println!("Hello, World!");
    }
}

#[cgp_context(MyContextComponents)]
pub struct MyContext;

delegate_components! {
    MyContextComponents {
        GreeterComponent: GreetHello,
    }
}

After this PR, the names can omitted and simplified to:

#[cgp_component(Greeter)]
pub trait CanGreet {
    fn greet(&self);
}

#[cgp_new_provider]
impl<Context> Greeter<Context> for GreetHello {
    fn greet(&self) {
        println!("Hello, World!");
    }
}

#[cgp_context]
pub struct MyContext;

delegate_components! {
    MyContextComponents {
        GreeterComponent: GreetHello,
    }
}

@soareschen soareschen changed the title Auto deduce provider and component name in #[cgp_provider] and #[cgp_context] Simplify attribute arguments for #[cgp_component], #[cgp_provider] and #[cgp_context] May 6, 2025
@soareschen soareschen merged commit 759bd22 into main May 6, 2025
5 checks passed
@soareschen soareschen deleted the implicit-provider-component-name branch May 6, 2025 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants