diff --git a/src/builtin/meta.rs b/src/builtin/meta.rs index 6717275..a77743a 100644 --- a/src/builtin/meta.rs +++ b/src/builtin/meta.rs @@ -4,17 +4,14 @@ use super::Builtin; use crate::value::Value; pub(crate) fn register(f: &mut BTreeMap) { - f.insert( - "if".to_owned(), - Box::new(|args| { - let cond: &Value = arg!(args, 0, "condition"); - let if_true = arg!(args, 1, "if-true").clone(); - let if_false = arg!(args, 2, "if-false").clone(); - if cond.is_true() { - Some(if_true) - } else { - Some(if_false) - } - }), - ); + decl!(f "if", |args| { + let cond: &Value = arg!(args, 0, "condition"); + let if_true = arg!(args, 1, "if-true").clone(); + let if_false = arg!(args, 2, "if-false").clone(); + if cond.is_true() { + Some(if_true) + } else { + Some(if_false) + } + }); } \ No newline at end of file diff --git a/src/builtin/mod.rs b/src/builtin/mod.rs index 2f72ab3..77e920c 100644 --- a/src/builtin/mod.rs +++ b/src/builtin/mod.rs @@ -4,26 +4,8 @@ use std::collections::BTreeMap; use crate::args::CallArgs; use crate::value::Value; -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, - 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, - }, - }; - }; -} +#[macro_use] +mod macros; mod color; diff --git a/tests/meta.rs b/tests/meta.rs new file mode 100644 index 0000000..331c99b --- /dev/null +++ b/tests/meta.rs @@ -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" +);