From 46f2d4c47ebe0ed659a42cbde656d55ee3b6cacd Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 16 Feb 2020 12:31:09 -0500 Subject: [PATCH] Proper error messages for too many arguments --- src/builtin/color/other.rs | 1 + src/builtin/color/rgb.rs | 4 ++-- src/builtin/macros.rs | 8 ++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/builtin/color/other.rs b/src/builtin/color/other.rs index d6e8408..00febc7 100644 --- a/src/builtin/color/other.rs +++ b/src/builtin/color/other.rs @@ -174,6 +174,7 @@ pub(crate) fn register(f: &mut BTreeMap) { })) }); decl!(f "ie-hex-str", |args, _| { + max_args!(args, 1); let color = match arg!(args, 0, "color").eval() { Value::Color(c) => c.clone(), v => return Err(format!("$color: {} is not a color.", v).into()), diff --git a/src/builtin/color/rgb.rs b/src/builtin/color/rgb.rs index c554c8b..6c41815 100644 --- a/src/builtin/color/rgb.rs +++ b/src/builtin/color/rgb.rs @@ -175,12 +175,12 @@ pub(crate) fn register(f: &mut BTreeMap) { decl!(f "mix", |args, _| { let color1 = match arg!(args, 0, "color1").eval() { Value::Color(c) => c, - v => return Err(format!("$color: {} is not a color.", v).into()), + v => return Err(format!("$color1: {} is not a color.", v).into()), }; let color2 = match arg!(args, 1, "color2").eval() { Value::Color(c) => c, - v => return Err(format!("$color: {} is not a color.", v).into()), + v => return Err(format!("$color2: {} is not a color.", v).into()), }; let weight = match arg!(args, 2, "weight"=Value::Dimension(Number::from(50), Unit::None)) { diff --git a/src/builtin/macros.rs b/src/builtin/macros.rs index f38d4e5..5d50fe7 100644 --- a/src/builtin/macros.rs +++ b/src/builtin/macros.rs @@ -24,3 +24,11 @@ macro_rules! decl { $f.insert($name.to_owned(), Box::new($body)); }; } + +macro_rules! max_args { + ($args:ident, $count:literal) => { + if $args.len() > $count { + return Err(format!("Only {} argument allowed, but {} were passed.", $count, $args.len()).into()); + } + }; +} \ No newline at end of file