From bbf0cd7e51af60c44a3e655697e542927948d16e Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 16 Feb 2020 11:28:36 -0500 Subject: [PATCH] More error messages! --- src/builtin/color/opacity.rs | 12 ++++++------ src/builtin/color/other.rs | 8 ++++---- src/builtin/color/rgb.rs | 14 +++++++++----- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/builtin/color/opacity.rs b/src/builtin/color/opacity.rs index bdb2095..0bba625 100644 --- a/src/builtin/color/opacity.rs +++ b/src/builtin/color/opacity.rs @@ -9,20 +9,20 @@ pub(crate) fn register(f: &mut BTreeMap) { decl!(f "alpha", |args, _| { match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)), - _ => todo!("non-color given to builtin function `alpha()`") + v => return Err(format!("$color: {} is not a color.", v).into()), } }); decl!(f "opacity", |args, _| { match arg!(args, 0, "color") { Value::Color(c) => Ok(Value::Dimension(c.alpha(), Unit::None)), Value::Dimension(num, unit) => Ok(Value::Ident(format!("opacity({}{})", num , unit), QuoteKind::None)), - _ => todo!("non-color given to builtin function `opacity()`") + v => return Err(format!("$color: {} is not a color.", v).into()), } }); decl!(f "opacify", |args, _| { let color = match arg!(args, 0, "color").eval() { Value::Color(c) => c, - _ => todo!("non-color given to builtin function `opacify()`") + v => return Err(format!("$color: {} is not a color.", v).into()), }; let amount = match arg!(args, 1, "amount").eval() { Value::Dimension(n, Unit::None) => n, @@ -34,7 +34,7 @@ pub(crate) fn register(f: &mut BTreeMap) { decl!(f "fade-in", |args, _| { let color = match arg!(args, 0, "color").eval() { Value::Color(c) => c, - _ => todo!("non-color given to builtin function `fade-in()`") + v => return Err(format!("$color: {} is not a color.", v).into()), }; let amount = match arg!(args, 1, "amount").eval() { Value::Dimension(n, Unit::None) => n, @@ -46,7 +46,7 @@ pub(crate) fn register(f: &mut BTreeMap) { decl!(f "transparentize", |args, _| { let color = match arg!(args, 0, "color").eval() { Value::Color(c) => c, - _ => todo!("non-color given to builtin function `transparentize()`") + v => return Err(format!("$color: {} is not a color.", v).into()), }; let amount = match arg!(args, 1, "amount").eval() { Value::Dimension(n, Unit::None) => n, @@ -58,7 +58,7 @@ pub(crate) fn register(f: &mut BTreeMap) { decl!(f "fade-out", |args, _| { let color = match arg!(args, 0, "color").eval() { Value::Color(c) => c, - _ => todo!("non-color given to builtin function `fade-out()`") + v => return Err(format!("$color: {} is not a color.", v).into()), }; let amount = match arg!(args, 1, "amount").eval() { Value::Dimension(n, Unit::None) => n, diff --git a/src/builtin/color/other.rs b/src/builtin/color/other.rs index 417a991..d6e8408 100644 --- a/src/builtin/color/other.rs +++ b/src/builtin/color/other.rs @@ -30,7 +30,7 @@ pub(crate) fn register(f: &mut BTreeMap) { decl!(f "change-color", |args, _| { let color = match arg!(args, 0, "color").eval() { Value::Color(c) => c, - _ => todo!("non-color given to builtin function `change-color()`") + v => return Err(format!("$color: {} is not a color.", v).into()), }; opt_arg!(args, alpha, "alpha"); @@ -68,7 +68,7 @@ pub(crate) fn register(f: &mut BTreeMap) { decl!(f "adjust-color", |args, _| { let color = match arg!(args, 0, "color").eval() { Value::Color(c) => c.clone(), - _ => todo!("non-color given to builtin function `adjust-color()`") + v => return Err(format!("$color: {} is not a color.", v).into()), }; opt_arg!(args, alpha, "alpha"); @@ -122,7 +122,7 @@ pub(crate) fn register(f: &mut BTreeMap) { decl!(f "scale-color", |args, _| { let color = match arg!(args, 0, "color").eval() { Value::Color(c) => c.clone(), - _ => todo!("non-color given to builtin function `scale-color()`") + v => return Err(format!("$color: {} is not a color.", v).into()), }; opt_arg!(args, alpha, "alpha"); @@ -176,7 +176,7 @@ pub(crate) fn register(f: &mut BTreeMap) { decl!(f "ie-hex-str", |args, _| { let color = match arg!(args, 0, "color").eval() { Value::Color(c) => c.clone(), - _ => todo!("non-color given to builtin function `ie-hex-str()`") + v => return Err(format!("$color: {} is not a color.", v).into()), }; Ok(Value::Ident(color.to_ie_hex_str(), QuoteKind::None)) }); diff --git a/src/builtin/color/rgb.rs b/src/builtin/color/rgb.rs index ca2ba3b..4d6a7b6 100644 --- a/src/builtin/color/rgb.rs +++ b/src/builtin/color/rgb.rs @@ -10,7 +10,7 @@ pub(crate) fn register(f: &mut BTreeMap) { if args.len() == 1 { let mut channels = match arg!(args, 0, "channels").eval() { Value::List(v, _) => v, - _ => todo!("missing element $green") + _ => return Err("Missing argument $channels.".into()) }; assert_eq!(channels.len(), 3_usize); @@ -18,19 +18,22 @@ pub(crate) fn register(f: &mut BTreeMap) { let blue = match channels.pop() { Some(Value::Dimension(n, Unit::None)) => n, Some(Value::Dimension(n, Unit::Percent)) => n / Number::from(100), - _ => todo!("$blue: ___ is not a color") + Some(v) => return Err(format!("$blue: {} is not a color", v).into()), + None => return Err("Missing element $blue.".into()), }; let green = match channels.pop() { Some(Value::Dimension(n, Unit::None)) => n, Some(Value::Dimension(n, Unit::Percent)) => n / Number::from(100), - _ => todo!("$green: ___ is not a color") + Some(v) => return Err(format!("$green: {} is not a color", v).into()), + None => return Err("Missing element $green.".into()), }; let red = match channels.pop() { Some(Value::Dimension(n, Unit::None)) => n, Some(Value::Dimension(n, Unit::Percent)) => n / Number::from(100), - _ => todo!("$red: ___ is not a color") + Some(v) => return Err(format!("$red: {} is not a color", v).into()), + None => return Err("Missing element $red.".into()), }; let color = Color::from_rgba(red, green, blue, Number::from(1)); @@ -52,7 +55,8 @@ pub(crate) fn register(f: &mut BTreeMap) { let red = match arg!(args, 0, "red").eval() { Value::Dimension(n, Unit::None) => n, Value::Dimension(n, Unit::Percent) => (n / Number::from(100)) * Number::from(255), - _ => todo!("expected either unitless or % number for alpha"), + v @ Value::Dimension(..) => return Err(format!("Expected {} to have no units or \"%\".", v).into()), + v => return Err(format!("$red: {} is not a number.", v).into()), }; let green = match arg!(args, 1, "green").eval() { Value::Dimension(n, Unit::None) => n,