2020-02-03 07:22:20 -05:00
|
|
|
macro_rules! arg {
|
|
|
|
($args:ident, $idx:literal, $name:literal) => {
|
2020-02-16 22:04:54 -05:00
|
|
|
match $args.remove(stringify!($idx)) {
|
2020-02-17 10:27:04 -05:00
|
|
|
Some(v) => v.eval()?,
|
2020-02-16 22:04:54 -05:00
|
|
|
None => match $args.remove($name) {
|
2020-02-17 10:27:04 -05:00
|
|
|
Some(v) => v.eval()?,
|
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-17 09:08:28 -05:00
|
|
|
match $args.remove(stringify!($idx)) {
|
2020-02-17 10:27:04 -05:00
|
|
|
Some(v) => v.eval()?,
|
2020-02-17 09:08:28 -05:00
|
|
|
None => match $args.remove($name) {
|
2020-02-17 10:27:04 -05:00
|
|
|
Some(v) => v.eval()?,
|
2020-02-03 07:22:20 -05:00
|
|
|
None => $default,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-16 12:31:09 -05:00
|
|
|
macro_rules! max_args {
|
|
|
|
($args:ident, $count:literal) => {
|
|
|
|
if $args.len() > $count {
|
2020-02-16 12:39:48 -05:00
|
|
|
if $count > 1 {
|
2020-02-16 15:03:28 -05:00
|
|
|
return Err(format!(
|
|
|
|
"Only {} arguments allowed, but {} were passed.",
|
|
|
|
$count,
|
|
|
|
$args.len()
|
|
|
|
)
|
|
|
|
.into());
|
2020-02-16 12:39:48 -05:00
|
|
|
} 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 12:39:48 -05:00
|
|
|
}
|
2020-02-16 12:31:09 -05:00
|
|
|
}
|
|
|
|
};
|
2020-02-16 15:03:28 -05:00
|
|
|
}
|
2020-02-16 15:14:14 -05:00
|
|
|
|
|
|
|
macro_rules! bound {
|
2020-02-16 15:17:01 -05:00
|
|
|
($name:literal, $arg:ident, $unit:ident, $low:literal, $high:literal) => {
|
2020-02-16 15:14:14 -05:00
|
|
|
if $arg > Number::from($high) || $arg < Number::from($low) {
|
|
|
|
return Err(format!(
|
2020-02-16 15:17:01 -05:00
|
|
|
"${}: Expected {}{} to be within {}{} and {}{}.",
|
|
|
|
$name, $arg, $unit, $low, $unit, $high, $unit,
|
2020-02-16 15:14:14 -05:00
|
|
|
)
|
|
|
|
.into());
|
|
|
|
} else {
|
|
|
|
$arg
|
|
|
|
}
|
|
|
|
};
|
2020-03-16 16:44:52 -04:00
|
|
|
// HACK: we accept `$low` as an ident here in order to work around
|
|
|
|
// a bug in the nightly compiler.
|
|
|
|
// https://github.com/rust-lang/rust/issues/70050
|
|
|
|
($name:literal, $arg:ident, $unit:ident, $low:ident, $high:literal) => {
|
|
|
|
if $arg > Number::from($high) || $arg < Number::from($low) {
|
|
|
|
return Err(format!(
|
|
|
|
"${}: Expected {}{} to be within {}{} and {}{}.",
|
|
|
|
$name, $arg, $unit, $low, $unit, $high, $unit,
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
} else {
|
|
|
|
$arg
|
|
|
|
}
|
|
|
|
};
|
2020-02-16 22:30:29 -05:00
|
|
|
($name:literal, $arg:ident, $unit:path, $low:literal, $high:literal) => {
|
|
|
|
if $arg > Number::from($high) || $arg < Number::from($low) {
|
|
|
|
return Err(format!(
|
|
|
|
"${}: Expected {}{} to be within {}{} and {}{}.",
|
|
|
|
$name, $arg, $unit, $low, $unit, $high, $unit,
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
} else {
|
|
|
|
$arg
|
|
|
|
}
|
|
|
|
};
|
2020-03-16 16:44:52 -04:00
|
|
|
// HACK: we accept `$low` as an ident here in order to work around
|
|
|
|
// a bug in the nightly compiler.
|
|
|
|
// https://github.com/rust-lang/rust/issues/70050
|
|
|
|
($name:literal, $arg:ident, $unit:path, $low:ident, $high:literal) => {
|
|
|
|
if $arg > Number::from($high) || $arg < Number::from($low) {
|
|
|
|
return Err(format!(
|
|
|
|
"${}: Expected {}{} to be within {}{} and {}{}.",
|
|
|
|
$name, $arg, $unit, $low, $unit, $high, $unit,
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
} else {
|
|
|
|
$arg
|
|
|
|
}
|
|
|
|
};
|
2020-02-16 15:14:14 -05:00
|
|
|
}
|