prism3_function/macros/
common_name_methods.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025.
4 *    3-Prism Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9
10//! # Common Name Methods Macro
11//!
12//! Generates common name management methods for function-like structs.
13//!
14//! # Author
15//!
16//! Haixing Hu
17
18/// Implements common name management methods for function-like structs.
19///
20/// This macro generates `name`, and `set_name` methods for structs that have
21/// an optional name field. These methods provide a standardized way to get
22/// and set names for debugging and logging purposes.
23///
24/// # Parameters
25///
26/// * `$type_desc:literal` - Description of the type (e.g., "consumer")
27///
28/// # Generated Methods
29///
30/// * `name(&self) -> Option<&str>` - Gets the current name if set
31/// * `set_name(&mut self, name: &str)` - Sets a new name for the instance
32///
33/// # Author
34///
35/// Haixing Hu
36macro_rules! impl_common_name_methods {
37    ($type_desc:literal) => {
38        #[doc = concat!("Gets the name of this ", $type_desc, ".")]
39        ///
40        /// # Returns
41        ///
42        /// Returns `Some(&str)` if a name was set, `None` otherwise.
43        pub fn name(&self) -> Option<&str> {
44            self.name.as_deref()
45        }
46
47        #[doc = concat!("Sets the name of this ", $type_desc, ".")]
48        ///
49        /// # Parameters
50        ///
51        #[doc = concat!("* `name` - The name to set for this ", $type_desc)]
52        pub fn set_name(&mut self, name: &str) {
53            self.name = Some(name.to_string());
54        }
55    };
56}
57
58pub(crate) use impl_common_name_methods;