Skip to content

Commit 18bd2b3

Browse files
committed
Restore the panicking behaviour of the params macro
1 parent 288e1de commit 18bd2b3

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/lib.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,20 @@ pub use uuid;
8181
macro_rules! params {
8282
() => {};
8383
(@to_pair $map:expr, $name:expr => $value:expr) => (
84-
$map.insert(
85-
std::string::String::from($name),
86-
$crate::value::Value::from($value),
87-
)
84+
let entry = $map.entry(std::string::String::from($name));
85+
if let std::collections::hash_map::Entry::Occupied(_) = entry {
86+
panic!("Redefinition of named parameter `{}'", entry.key());
87+
} else {
88+
entry.or_insert($crate::value::Value::from($value));
89+
}
8890
);
8991
(@to_pair $map:expr, $name:ident) => (
90-
$map.insert(
91-
std::string::String::from(stringify!($name)),
92-
$crate::value::Value::from($name),
93-
)
92+
let entry = $map.entry(std::string::String::from(stringify!($name)));
93+
if let std::collections::hash_map::Entry::Occupied(_) = entry {
94+
panic!("Redefinition of named parameter `{}'", entry.key());
95+
} else {
96+
entry.or_insert($crate::value::Value::from($name));
97+
}
9498
);
9599
(@expand $map:expr;) => {};
96100
(@expand $map:expr; $name:expr => $value:expr, $($tail:tt)*) => {
@@ -246,3 +250,9 @@ fn params_macro_test() {
246250
params! { "foo" => foo, bar, }
247251
);
248252
}
253+
254+
#[test]
255+
#[should_panic(expected = "Redefinition of named parameter `a'")]
256+
fn params_macro_should_panic_on_named_param_redefinition() {
257+
params! {"a" => 1, "b" => 2, "a" => 3};
258+
}

0 commit comments

Comments
 (0)