Refactor builtin function declaration
This commit is contained in:
parent
efc62a2433
commit
8eb9620a1a
@ -4,17 +4,14 @@ use super::Builtin;
|
|||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
|
|
||||||
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||||
f.insert(
|
decl!(f "if", |args| {
|
||||||
"if".to_owned(),
|
let cond: &Value = arg!(args, 0, "condition");
|
||||||
Box::new(|args| {
|
let if_true = arg!(args, 1, "if-true").clone();
|
||||||
let cond: &Value = arg!(args, 0, "condition");
|
let if_false = arg!(args, 2, "if-false").clone();
|
||||||
let if_true = arg!(args, 1, "if-true").clone();
|
if cond.is_true() {
|
||||||
let if_false = arg!(args, 2, "if-false").clone();
|
Some(if_true)
|
||||||
if cond.is_true() {
|
} else {
|
||||||
Some(if_true)
|
Some(if_false)
|
||||||
} else {
|
}
|
||||||
Some(if_false)
|
});
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
@ -4,26 +4,8 @@ use std::collections::BTreeMap;
|
|||||||
use crate::args::CallArgs;
|
use crate::args::CallArgs;
|
||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
|
|
||||||
macro_rules! arg {
|
#[macro_use]
|
||||||
($args:ident, $idx:literal, $name:literal) => {
|
mod macros;
|
||||||
match $args.get(stringify!($idx)) {
|
|
||||||
Some(v) => v,
|
|
||||||
None => match $args.get($name) {
|
|
||||||
Some(v) => v,
|
|
||||||
None => panic!("missing variable"),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
($args:ident, $idx:literal, $name:literal, $default:literal) => {
|
|
||||||
match $args.get(stringify!($idx)) {
|
|
||||||
Some(v) => v,
|
|
||||||
None => match $args.get($name) {
|
|
||||||
Some(v) => v,
|
|
||||||
None => $default,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
mod color;
|
mod color;
|
||||||
|
20
tests/meta.rs
Normal file
20
tests/meta.rs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#![cfg(test)]
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod macros;
|
||||||
|
|
||||||
|
test!(
|
||||||
|
if_true,
|
||||||
|
"a {\n color: if(true, 1, 2)\n}\n",
|
||||||
|
"a {\n color: 1;\n}\n"
|
||||||
|
);
|
||||||
|
test!(
|
||||||
|
if_named_args,
|
||||||
|
"a {\n color: if($condition: true, $if-true: 1, $if-false: 2)\n}\n",
|
||||||
|
"a {\n color: 1;\n}\n"
|
||||||
|
);
|
||||||
|
test!(
|
||||||
|
if_false,
|
||||||
|
"a {\n color: if(false, 1, 2);\n}\n",
|
||||||
|
"a {\n color: 2;\n}\n"
|
||||||
|
);
|
Loading…
x
Reference in New Issue
Block a user