2024-01-03 03:22:36 +00:00
// SPDX-License-Identifier: MPL-2.0
2023-02-27 08:18:04 +00:00
//! This crate defines the component system related macros.
//!
#![ feature(proc_macro_diagnostic) ]
#![ allow(dead_code) ]
2024-05-22 09:46:27 +00:00
#![ deny(unsafe_code) ]
2023-02-27 08:18:04 +00:00
mod init_comp ;
mod priority ;
use init_comp ::ComponentInitFunction ;
use proc_macro ::TokenStream ;
use quote ::quote ;
use syn ::parse_macro_input ;
pub ( crate ) const COMPONENT_FILE_NAME : & str = " Components.toml " ;
/// Register a function to be called when the component system is initialized. The function should not public.
///
/// Example:
/// ```rust
/// #[init_component]
/// fn init() -> Result<(), component::ComponentInitError> {
/// Ok(())
/// }
///
/// ```
///
/// It will expand to
/// ```rust
/// fn init() -> Result<(), component::ComponentInitError> {
/// Ok(())
/// }
///
/// const fn file() -> &'static str{
/// file!()
/// }
///
/// component::submit!(component::ComponentRegistry::new(&init,file()));
/// ```
/// The priority will calculate automatically
///
#[ proc_macro_attribute ]
pub fn init_component ( _ : TokenStream , input : TokenStream ) -> proc_macro ::TokenStream {
let function = parse_macro_input! ( input as ComponentInitFunction ) ;
let function_name = & function . function_name ;
quote! {
#function
const fn file ( ) -> & 'static str {
file! ( )
}
component ::submit! ( component ::ComponentRegistry ::new ( & #function_name , file ( ) ) ) ;
}
. into ( )
}
/// Automatically generate all component information required by the component system.
2023-03-01 11:18:45 +00:00
///
2023-02-27 10:41:52 +00:00
/// It mainly uses the output of the command `cargo metadata` to automatically generate information about all components, and also checks whether `Components.toml` contains all the components.
2023-02-27 08:18:04 +00:00
///
2023-02-27 10:41:52 +00:00
/// It is often used with `component::init_all`.
2023-02-27 08:18:04 +00:00
///
/// Example:
///
/// ```rust
2023-02-27 10:41:52 +00:00
/// component::init_all(component::parse_metadata!());
2023-02-27 08:18:04 +00:00
/// ```
///
#[ proc_macro ]
2023-02-27 10:41:52 +00:00
pub fn parse_metadata ( _ : TokenStream ) -> proc_macro ::TokenStream {
2023-02-27 08:18:04 +00:00
let out = priority ::component_generate ( ) ;
let path = priority ::get_component_toml_path ( ) ;
quote! {
{
include_str! ( #path ) ;
extern crate alloc ;
alloc ::vec! [
#( component ::ComponentInfo ::new #out ) , *
]
}
}
. into ( )
}