Skip to content

Commit d53a861

Browse files
committed
refacto: simplify macros
1 parent 2043307 commit d53a861

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

internal/mithril-cli-helper/src/source_config.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
//! Utilities to register config parameters.
22
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-
143
/// Register a optional parameter in the config map when it's not None.
154
#[macro_export]
165
macro_rules! register_config_value_option {
176
( $map:ident, $namespace:expr, $self:ident.$command:ident ) => {{
187
if let Some(value) = $self.$command.clone() {
19-
register!($map, $namespace, $command, value);
8+
register_config_value!($map, $namespace, $command = value);
209
}
2110
}};
2211
( $map:ident, $namespace:expr, $self:ident.$command:ident, $mapping:expr ) => {{
2312
if let Some(value) = $self.$command.clone() {
24-
register!($map, $namespace, $command, $mapping(value));
13+
register_config_value!($map, $namespace, $command = $mapping(value));
2514
}
2615
}};
2716
}
@@ -31,19 +20,33 @@ macro_rules! register_config_value_option {
3120
macro_rules! register_config_value_bool {
3221
( $map:ident, $namespace:expr, $self:ident.$command:ident ) => {{
3322
if $self.$command {
34-
register!($map, $namespace, $command, true);
23+
register_config_value!($map, $namespace, $command = true);
3524
}
3625
}};
3726
}
3827

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)
4036
#[macro_export]
4137
macro_rules! register_config_value {
4238
( $map:ident, $namespace:expr, $self:ident.$command:ident ) => {{
43-
register!($map, $namespace, $command, $self.$command);
39+
register_config_value!($map, $namespace, $command = $self.$command);
4440
}};
4541
( $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+
);
4750
}};
4851
}
4952

@@ -53,13 +56,12 @@ mod tests {
5356
use std::collections::HashMap;
5457

5558
#[test]
56-
fn test_register_macro() {
59+
fn test_register_config_value_macro_with_the_value() {
5760
let mut map = HashMap::new();
58-
register!(
61+
register_config_value!(
5962
map,
6063
&"namespace".to_string(),
61-
server_ip,
62-
Some("value_server_ip".to_string())
64+
server_ip = Some("value_server_ip".to_string())
6365
);
6466

6567
let expected = HashMap::from([(

mithril-aggregator/src/commands/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod tools_command;
77
use anyhow::anyhow;
88
use clap::{CommandFactory, Parser, Subcommand};
99
use config::{builder::DefaultState, ConfigBuilder, Map, Source, Value};
10-
use mithril_cli_helper::{register, register_config_value_option};
10+
use mithril_cli_helper::{register_config_value, register_config_value_option};
1111
use mithril_common::StdResult;
1212
use mithril_doc::{Documenter, DocumenterDefault, StructDoc};
1313
use slog::{debug, Level, Logger};

mithril-aggregator/src/commands/serve_command.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use config::{builder::DefaultState, ConfigBuilder, Map, Source, Value};
1313
use slog::{crit, debug, info, warn, Logger};
1414
use tokio::{sync::oneshot, task::JoinSet};
1515

16-
use mithril_cli_helper::{register, register_config_value_bool, register_config_value_option};
16+
use mithril_cli_helper::{
17+
register_config_value, register_config_value_bool, register_config_value_option,
18+
};
1719
use mithril_common::StdResult;
1820
use mithril_metric::MetricsServer;
1921

mithril-aggregator/src/configuration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::collections::{BTreeSet, HashMap};
55
use std::path::PathBuf;
66
use std::str::FromStr;
77

8-
use mithril_cli_helper::{register, register_config_value};
8+
use mithril_cli_helper::register_config_value;
99
use mithril_common::chain_observer::ChainObserverType;
1010
use mithril_common::crypto_helper::ProtocolGenesisSigner;
1111
use mithril_common::entities::{

mithril-signer/src/configuration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use mithril_doc::{Documenter, DocumenterDefault, StructDoc};
44
use serde::{Deserialize, Serialize};
55
use std::{path::PathBuf, sync::Arc};
66

7-
use mithril_cli_helper::{register, register_config_value};
7+
use mithril_cli_helper::register_config_value;
88
use mithril_common::{
99
chain_observer::ChainObserver,
1010
crypto_helper::tests_setup,

0 commit comments

Comments
 (0)