grass/src/builtin/macros.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

2020-02-03 07:22:20 -05:00
macro_rules! arg {
($args:ident, $idx:literal, $name:literal) => {
match $args.get(stringify!($idx)) {
Some(v) => v,
None => match $args.get($name) {
Some(v) => v,
2020-02-16 12:17:34 -05:00
None => return Err(concat!("Missing argument $", $name, ".").into()),
2020-02-03 07:22:20 -05:00
},
};
};
2020-02-09 13:44:27 -05:00
($args:ident, $idx:literal, $name:literal=$default:expr) => {
2020-02-03 07:22:20 -05:00
match $args.get(stringify!($idx)) {
2020-02-09 13:44:27 -05:00
Some(v) => v.clone(),
2020-02-03 07:22:20 -05:00
None => match $args.get($name) {
2020-02-09 13:44:27 -05:00
Some(v) => v.clone(),
2020-02-03 07:22:20 -05:00
None => $default,
},
};
};
}
macro_rules! decl {
($f:ident $name:literal, $body:expr) => {
$f.insert($name.to_owned(), Box::new($body));
};
2020-02-07 00:10:43 -05:00
}
macro_rules! max_args {
($args:ident, $count:literal) => {
if $args.len() > $count {
if $count > 1 {
return Err(format!("Only {} arguments allowed, but {} were passed.", $count, $args.len()).into());
} else {
return Err(format!("Only {} argument allowed, but {} were passed.", $count, $args.len()).into());
}
}
};
}