Skip to content

Expand cgp_preset! into a preset module#72

Merged
soareschen merged 19 commits intomainfrom
preset-module
Feb 22, 2025
Merged

Expand cgp_preset! into a preset module#72
soareschen merged 19 commits intomainfrom
preset-module

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented Feb 22, 2025

Summary

This PR performs some redesign on the unstable cgp_preset! macro to make the definition of presets slightly more ergonomic. The main changes is that instead of being a struct, the defined preset is now a Rust module that contains several CGP constructs related to the preset.

Use with #[cgp::re_export_imports]

The cgp_preset! macro now requires the module to be wrapped around a mod with #[cgp::re_export_imports], in order to help re-exporting all imported component constructs.

A preset is now defined in the form:

#[cgp::re_export_imports]
mod preset {
    use cgp::prelude::*;
    use foo::{FooComponent, FooProvider};
    use bar::{BarComponent, BarProvider};

    cgp_preset! {
        MyPreset {
            FooComponent: FooProvider,
            BarComponent: BarProvider,
        }
    }
}

which would be roughly expanded into:

mod preset {
    use cgp::prelude::*;
    use foo::{FooComponent, FooProvider};
    use bar::{BarComponent, BarProvider};

    mod MyPreset {
        use super::*;

        pub struct Provider;

        delegate_components! {
            Provider {
                FooComponent: FooProvider,
                BarComponent: BarProvider,
            }
        }

        pub trait IsPreset<Component> {}

        impl<T> IsPreset<FooComponent> for T {}
        impl<T> IsPreset<BarComponent> for T {}

        #[macro_export]
        macro_rules! with_my_preset {
            ($($body:tt)*) => {
                replace_with! { [FooComponent, BarComponent], $($body)* }
            };
        }

        pub use with_my_preset as with_components;

        pub mod re_exports {
            pub use super::super::super::re_exports::*;
        }
    }
}

mod re_exports {
    pub use cgp::prelude::*;
    pub use foo::{FooComponent, FooProvider};
    pub use bar::{BarComponent, BarProvider};
}

pub use preset::*;

Changes

  • The provider name format has been renamed from MyPreset to MyPreset::Provider.
  • The component match trait has been renamed from IsMyPreset to MyPreset::IsPreset.
  • The substitution macro has been renamed from with_my_preset! to MyPreset::with_components!.

@soareschen soareschen merged commit bed2170 into main Feb 22, 2025
5 checks passed
@soareschen soareschen deleted the preset-module branch February 22, 2025 21: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.

1 participant