1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
#[doc(hidden)]
18
pub mod __reexports {
19
    pub use frame_support::pallet_prelude::Get;
20
}
21

            
22
/// Define a `Get` which can have different value in "prod" or "fast" mode.
23
/// `Get` will return "fast" value if feature "fast-runtime" is enabled.
24
/// It also provides functions `prod`/`prod_if`/`fast` to write tests for both
25
/// values, regardless of the feature enabled when compiling the tests.
26
#[macro_export]
27
macro_rules! prod_or_fast_parameter_types {
28
    ($( #[ $attr:meta ] )* $vis:vis const $name:ident: $ty:ty = { prod: $prod:expr, fast: $fast:expr }; $($rest:tt)* ) => {
29
        $( #[ $attr ] )*
30
        $vis struct $name;
31

            
32
        impl $name {
33
            /// Get the value based on if "fast-runtime" feature is enabled.
34
209346
            pub const fn get() -> $ty {
35
209346
                Self::prod_if(!cfg!(feature = "fast-runtime"))
36
209346
            }
37

            
38
            /// Return prod value if condition is true, otherwise returns fast value.
39
209410
            pub const fn prod_if(b: bool) -> $ty {
40
209410
                if b { Self::prod() } else { Self::fast() }
41
209410
            }
42
209410

            
43
209410
            /// Always return prod value.
44
209410
            pub const fn prod() -> $ty {
45
12
                $prod
46
12
            }
47

            
48
            /// Always return fast value.
49
209378
            pub const fn fast() -> $ty {
50
209378
                $fast
51
209378
            }
52
        }
53

            
54
        impl<_I: From<$ty>> $crate::prod_or_fast::__reexports::Get<_I> for $name {
55
20097
            fn get() -> _I {
56
20097
                _I::from(Self::get())
57
20097
            }
58
        }
59

            
60
        $crate::prod_or_fast_parameter_types!($($rest)*);
61
    };
62
    ($( #[ $attr:meta ] )* $vis:vis $name:ident: $ty:ty = { prod: $prod:expr, fast: $fast:expr }; $($rest:tt)* ) => {
63
        $( #[ $attr ] )*
64
        $vis struct $name;
65

            
66
        impl $name {
67
            /// Get the value based on if "fast-runtime" feature is enabled.
68
            pub fn get() -> $ty {
69
                Self::prod_if(!cfg!(feature = "fast-runtime"))
70
            }
71

            
72
            /// Return prod value if condition is true, otherwise returns fast value.
73
            pub fn prod_if(b: bool) -> $ty {
74
                if b { Self::prod() } else { Self::fast() }
75
            }
76

            
77
            /// Always return prod value.
78
            pub fn prod() -> $ty {
79
                $prod
80
            }
81

            
82
            /// Always return fast value.
83
            pub fn fast() -> $ty {
84
                $fast
85
            }
86
        }
87

            
88
        impl<_I: From<$ty>> $crate::prod_or_fast::__reexports::Get<_I> for $name {
89
            fn get() -> _I {
90
                _I::from(Self::get())
91
            }
92
        }
93

            
94
        $crate::prod_or_fast_parameter_types!($($rest)*);
95
    };
96
    () => {}
97
}