grass/src/builtin/macros.rs

111 lines
3.6 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.remove_positional($idx) {
2020-02-17 10:27:04 -05:00
Some(v) => v.eval()?,
None => match $args.remove_named($name.to_owned()) {
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) => {
match $args.remove_positional($idx) {
2020-02-17 10:27:04 -05:00
Some(v) => v.eval()?,
None => match $args.remove_named($name.to_owned()) {
2020-02-17 10:27:04 -05:00
Some(v) => v.eval()?,
2020-02-03 07:22:20 -05:00
None => $default,
},
};
};
}
macro_rules! named_arg {
($args:ident, $name:literal) => {
match $args.remove_named($name.to_owned()) {
Some(v) => v.eval()?,
None => return Err(concat!("Missing argument $", $name, ".").into()),
};
};
($args:ident, $name:literal=$default:expr) => {
match $args.remove_named($name.to_owned()) {
Some(v) => v.eval()?,
None => $default,
};
};
}
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 {
2020-02-16 15:17:01 -05:00
($name:literal, $arg:ident, $unit:ident, $low:literal, $high:literal) => {
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,
)
.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
}
};
($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
}
};
}