ixa/macros/
define_global_property.rs

1/// Defines a global property with the following parameters:
2/// * `$global_property`: Name for the identifier type of the global property
3/// * `$value`: The type of the property's value
4/// * `$validate`: A function (or closure) that checks the validity of the property (optional)
5#[macro_export]
6macro_rules! define_global_property {
7    ($global_property:ident, $value:ty, $validate: expr) => {
8        #[derive(Copy, Clone)]
9        pub struct $global_property;
10
11        impl $crate::global_properties::GlobalProperty for $global_property {
12            type Value = $value;
13
14            fn new() -> Self {
15                $global_property
16            }
17
18            fn validate(val: &$value) -> Result<(), $crate::error::IxaError> {
19                $validate(val)
20            }
21        }
22
23        $crate::paste::paste! {
24            $crate::ctor::declarative::ctor!{
25                #[ctor]
26                fn [<$global_property:snake _register>]() {
27                    let module = module_path!();
28                    let mut name = module.split("::").next().unwrap().to_string();
29                    name += ".";
30                    name += stringify!($global_property);
31                    $crate::global_properties::add_global_property::<$global_property>(&name);
32                }
33            }
34        }
35    };
36
37    ($global_property: ident, $value: ty) => {
38        define_global_property!($global_property, $value, |_| { Ok(()) });
39    };
40}