grass/src/builtin/macros.rs

63 lines
1.7 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 {
2020-02-16 15:03:28 -05:00
return Err(format!(
"Only {} arguments allowed, but {} were passed.",
$count,
$args.len()
)
.into());
} else {
2020-02-16 15:03:28 -05:00
return Err(format!(
"Only {} argument allowed, but {} were passed.",
$count,
$args.len()
)
.into());
}
}
};
2020-02-16 15:03:28 -05:00
}
macro_rules! bound {
($arg:ident, $unit:ident, $low:literal, $high:literal) => {
if $arg > Number::from($high) || $arg < Number::from($low) {
return Err(format!(
"Expected {}{} to be within {}{} and {}{}.",
$arg, $unit, $low, $unit, $high, $unit,
)
.into());
} else {
$arg
}
};
}