1
1
//! Utilities to register config parameters.
2
2
3
- /// Register a parameter in the config map.
4
- #[ macro_export]
5
- macro_rules! register {
6
- ( $map: ident, $namespace: expr, $command: ident, $value: expr ) => { {
7
- $map. insert(
8
- stringify!( $command) . to_string( ) ,
9
- config:: Value :: new( Some ( $namespace) , $value) ,
10
- ) ;
11
- } } ;
12
- }
13
-
14
3
/// Register a optional parameter in the config map when it's not None.
15
4
#[ macro_export]
16
5
macro_rules! register_config_value_option {
17
6
( $map: ident, $namespace: expr, $self: ident. $command: ident ) => { {
18
7
if let Some ( value) = $self. $command. clone( ) {
19
- register !( $map, $namespace, $command, value) ;
8
+ register_config_value !( $map, $namespace, $command = value) ;
20
9
}
21
10
} } ;
22
11
( $map: ident, $namespace: expr, $self: ident. $command: ident, $mapping: expr ) => { {
23
12
if let Some ( value) = $self. $command. clone( ) {
24
- register !( $map, $namespace, $command, $mapping( value) ) ;
13
+ register_config_value !( $map, $namespace, $command = $mapping( value) ) ;
25
14
}
26
15
} } ;
27
16
}
@@ -31,19 +20,33 @@ macro_rules! register_config_value_option {
31
20
macro_rules! register_config_value_bool {
32
21
( $map: ident, $namespace: expr, $self: ident. $command: ident ) => { {
33
22
if $self. $command {
34
- register !( $map, $namespace, $command, true ) ;
23
+ register_config_value !( $map, $namespace, $command = true ) ;
35
24
}
36
25
} } ;
37
26
}
38
27
39
- /// Register a parameter in the config map.
28
+ /// Register a parameter in the config map using the identifier as key.
29
+ /// Example:
30
+ /// register_config_value(map, namespace, self.identifier)
31
+ ///
32
+ /// The same macro, with a different syntax, is used to insert the given value without transformation.
33
+ /// Iit is designed to be used by other macros.
34
+ /// Example:
35
+ /// register_config_value!(map, namespace, identifier = value)
40
36
#[ macro_export]
41
37
macro_rules! register_config_value {
42
38
( $map: ident, $namespace: expr, $self: ident. $command: ident ) => { {
43
- register !( $map, $namespace, $command, $self. $command) ;
39
+ register_config_value !( $map, $namespace, $command = $self. $command) ;
44
40
} } ;
45
41
( $map: ident, $namespace: expr, $self: ident. $command: ident, $mapping: expr ) => { {
46
- register!( $map, $namespace, $command, $mapping( $self. $command) ) ;
42
+ register_config_value!( $map, $namespace, $command = $mapping( $self. $command) ) ;
43
+ } } ;
44
+
45
+ ( $map: ident, $namespace: expr, $command: ident = $value: expr ) => { {
46
+ $map. insert(
47
+ stringify!( $command) . to_string( ) ,
48
+ config:: Value :: new( Some ( $namespace) , $value) ,
49
+ ) ;
47
50
} } ;
48
51
}
49
52
@@ -53,13 +56,12 @@ mod tests {
53
56
use std:: collections:: HashMap ;
54
57
55
58
#[ test]
56
- fn test_register_macro ( ) {
59
+ fn test_register_config_value_macro_with_the_value ( ) {
57
60
let mut map = HashMap :: new ( ) ;
58
- register ! (
61
+ register_config_value ! (
59
62
map,
60
63
& "namespace" . to_string( ) ,
61
- server_ip,
62
- Some ( "value_server_ip" . to_string( ) )
64
+ server_ip = Some ( "value_server_ip" . to_string( ) )
63
65
) ;
64
66
65
67
let expected = HashMap :: from ( [ (
0 commit comments