better error messages for map-get and map-has-key

This commit is contained in:
ConnorSkees 2020-03-30 16:41:00 -04:00
parent 4b1dc39705
commit 9e3029dbfb
2 changed files with 12 additions and 2 deletions

View File

@ -9,12 +9,12 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
"map-get".to_owned(),
Box::new(|args, _| {
max_args!(args, 2);
let key = arg!(args, 1, "key");
let map = match arg!(args, 0, "map") {
Value::Map(m) => m,
Value::List(v, ..) if v.is_empty() => SassMap::new(),
v => return Err(format!("$map: {} is not a map.", v).into()),
};
let key = arg!(args, 1, "key");
Ok(map.get(key)?.unwrap_or(Value::Null).clone())
}),
);
@ -22,12 +22,12 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
"map-has-key".to_owned(),
Box::new(|args, _| {
max_args!(args, 2);
let key = arg!(args, 1, "key");
let map = match arg!(args, 0, "map") {
Value::Map(m) => m,
Value::List(v, ..) if v.is_empty() => SassMap::new(),
v => return Err(format!("$map: {} is not a map.", v).into()),
};
let key = arg!(args, 1, "key");
Ok(Value::bool(map.get(key)?.is_some()))
}),
);

View File

@ -111,3 +111,13 @@ test!(
"a {\n color: length((a: b, c: d, e: f));\n}\n",
"a {\n color: 3;\n}\n"
);
error!(
map_get_one_arg,
"a {\n color: map-get(1);\n}\n",
"Error: Missing argument $key."
);
error!(
map_has_key_one_arg,
"a {\n color: map-has-key(1);\n}\n",
"Error: Missing argument $key."
);