diff --git a/src/builtin/list.rs b/src/builtin/list.rs index 3b499fd..5920555 100644 --- a/src/builtin/list.rs +++ b/src/builtin/list.rs @@ -107,6 +107,36 @@ pub(crate) fn register(f: &mut HashMap) { list[len - n.abs().to_integer().to_usize().unwrap()] = val; } + Ok(Value::List(list, sep)) + }), + ); + f.insert( + "append".to_owned(), + Box::new(|args, _| { + max_args!(args, 3); + let (mut list, sep) = match arg!(args, 0, "list") { + Value::List(v, sep) => (v, sep), + v => (vec![v], ListSeparator::Space), + }; + let val = arg!(args, 1, "val"); + let sep = match arg!( + args, + 2, + "separator" = Value::Ident("auto".to_owned(), QuoteKind::None) + ) { + Value::Ident(s, ..) => match s.as_str() { + "auto" => sep, + "comma" => ListSeparator::Comma, + "space" => ListSeparator::Space, + _ => { + return Err("$separator: Must be \"space\", \"comma\", or \"auto\".".into()) + } + }, + _ => return Err("$separator: Must be \"space\", \"comma\", or \"auto\".".into()), + }; + + list.push(val); + Ok(Value::List(list, sep)) }), ); diff --git a/tests/list.rs b/tests/list.rs index b8b41b8..947f5c8 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -80,3 +80,28 @@ test!( "a {\n color: set-nth((a, b, c), 1, e);\n}\n", "a {\n color: e, b, c;\n}\n" ); +test!( + append_space_separated, + "a {\n color: append(a b, c);\n}\n", + "a {\n color: a b c;\n}\n" +); +test!( + append_comma_separated, + "a {\n color: append((a, b), c);\n}\n", + "a {\n color: a, b, c;\n}\n" +); +test!( + append_list, + "a {\n color: append(a b, c d);\n}\n", + "a {\n color: a b c d;\n}\n" +); +test!( + append_list_separator_comma, + "a {\n color: append(a, b, comma);\n}\n", + "a {\n color: a, b;\n}\n" +); +test!( + append_list_separator_space, + "a {\n color: append((a, b), c, space);\n}\n", + "a {\n color: a b c;\n}\n" +);