From 4588e17f7ddacd8391a5c881a209735e08d48797 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Tue, 4 Aug 2026 13:26:48 +0800 Subject: feat: add entry metadata system with compile-time typed values Add `Metadata` trait and `#[metadata(Entry)]` attribute macro to attach arbitrary, compile-time-typed metadata to entries. Implement `ProgramCollect::get_metadata()` for runtime retrieval, backed by a global registry populated by `register_metadata!`. Extend `pathf` with `MetadataPattern` to resolve metadata types across modules at build time. Add two examples demonstrating metadata usage with and without pathf integration. --- mingling_core/src/asset.rs | 1 + mingling_core/src/asset/metadata.rs | 14 ++++++++++++++ mingling_core/src/lib.rs | 1 + mingling_core/src/program/collection.rs | 12 ++++++++++++ 4 files changed, 28 insertions(+) create mode 100644 mingling_core/src/asset/metadata.rs (limited to 'mingling_core/src') diff --git a/mingling_core/src/asset.rs b/mingling_core/src/asset.rs index 8c709ac..fc1c81b 100644 --- a/mingling_core/src/asset.rs +++ b/mingling_core/src/asset.rs @@ -5,6 +5,7 @@ pub(crate) mod enum_tag; pub(crate) mod global_resource; pub(crate) mod help; pub(crate) mod lazy_resource; +pub(crate) mod metadata; pub(crate) mod node; pub(crate) mod renderer; pub(crate) mod routable; diff --git a/mingling_core/src/asset/metadata.rs b/mingling_core/src/asset/metadata.rs new file mode 100644 index 0000000..996b34c --- /dev/null +++ b/mingling_core/src/asset/metadata.rs @@ -0,0 +1,14 @@ +/// Provides metadata for an Entry. +/// +/// Any type can be attached to an Entry as metadata, allowing the program to +/// carry compile-time-typed, arbitrary description data alongside each +/// registered entry. The [`Metadata`] trait bridges an Entry type (`Self`) to +/// an arbitrary metadata type `B`. +/// +/// It is recommended to use the `#[metadata(Entry)]` attribute macro from +/// [mingling_macros](https://crates.io/crates/mingling_macros) to implement this +/// trait and register the entry via `register_metadata!`. +pub trait Metadata { + /// Initializes and returns the metadata value of type `B` for this entry. + fn init_metadata() -> B; +} diff --git a/mingling_core/src/lib.rs b/mingling_core/src/lib.rs index cd42842..3a5acf4 100644 --- a/mingling_core/src/lib.rs +++ b/mingling_core/src/lib.rs @@ -70,6 +70,7 @@ pub use crate::asset::enum_tag::*; pub use crate::asset::global_resource::*; pub use crate::asset::help::*; pub use crate::asset::lazy_resource::*; +pub use crate::asset::metadata::*; pub use crate::asset::node::*; pub use crate::asset::renderer::*; pub use crate::asset::routable::*; diff --git a/mingling_core/src/program/collection.rs b/mingling_core/src/program/collection.rs index 1b4d7dd..5b1152a 100644 --- a/mingling_core/src/program/collection.rs +++ b/mingling_core/src/program/collection.rs @@ -66,6 +66,18 @@ pub trait ProgramCollect { /// Render help for Entry fn render_help(any: AnyOutput) -> RenderResult; + /// Retrieves compile-time registered metadata of type `T` for the given + /// enum member, if any was registered via `#[metadata(Entry)]`. + /// + /// Returns `None` when no metadata of type `T` has been registered for the + /// provided member, or when the requested `T` does not match the registered + /// metadata type. The concrete implementation of this method is generated + /// by the `gen_program!` macro. + fn get_metadata(member_id: Self::Enum) -> Option { + let _ = member_id; + None + } + /// Find a matching chain to continue execution based on the input [AnyOutput](./struct.AnyOutput.html), returning a new [AnyOutput](./struct.AnyOutput.html) #[cfg(feature = "async")] fn do_chain( -- cgit