strategy_pattern_rs/lib.rs
1use impls::{strategy_pattern_fn::strategy_pattern_fn_impl, strategy_pattern_type::strategy_pattern_type_impl};
2use proc_macro::TokenStream;
3
4mod impls;
5mod util;
6
7/// Attribute macro to define a strategy type.
8///
9/// This macro should be applied to a struct that will serve as a strategy container.
10/// It allows defining a type that will store function pointers and execute them dynamically.
11///
12/// # Parameters
13/// - `search`: Defines the search mode for function keys. It can be:
14/// - `"Exact"` → Exact match.
15/// - `"IgnoreCase"` → Case-insensitive match.
16/// - `"RegExp"` → Uses regular expressions.
17///
18/// # Example
19/// ```
20/// #[strategy_pattern_type(search = "IgnoreCase")]
21/// pub struct GreetingStrategy(fn(String) -> String);
22/// ```
23#[proc_macro_attribute]
24pub fn strategy_pattern_type( args : TokenStream, item : TokenStream ) -> TokenStream
25{
26 strategy_pattern_type_impl( args, item )
27}
28
29/// Attribute macro to register a function as part of a strategy pattern.
30///
31/// This macro should be applied to functions that will be executed dynamically based on a key.
32///
33/// # Parameters
34/// - `strategy`: The name of the struct that represents the strategy.
35/// - `key`: A string used to identify the function within the strategy.
36///
37/// # Example
38/// ```
39/// #[strategy_pattern_fn(strategy = GreetingStrategy, key = "formal_greeting")]
40/// pub fn formal_greeting(name: String) -> String {
41/// format!("Good day, {name}")
42/// }
43/// ```
44#[proc_macro_attribute]
45pub fn strategy_pattern_fn( args : TokenStream, item : TokenStream ) -> TokenStream
46{
47 strategy_pattern_fn_impl( args, item )
48}
49