From 284bc8ea6d17b7cc60d7f0e71d53c32eb9da9c7d Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 20 Mar 2020 12:32:33 -0400 Subject: [PATCH] Implement builtin function `list-separator()` --- src/builtin/list.rs | 15 +++++++++++++++ src/common.rs | 2 -- tests/list.rs | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/builtin/list.rs b/src/builtin/list.rs index ef977bb..745bcd1 100644 --- a/src/builtin/list.rs +++ b/src/builtin/list.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use num_traits::cast::ToPrimitive; use super::Builtin; +use crate::common::{ListSeparator, QuoteKind}; use crate::unit::Unit; use crate::value::{Number, Value}; @@ -55,4 +56,18 @@ pub(crate) fn register(f: &mut HashMap) { } }), ); + f.insert( + "list-separator".to_owned(), + Box::new(|args, _| { + max_args!(args, 1); + Ok(Value::Ident( + match arg!(args, 0, "list") { + Value::List(_, sep) => sep.name(), + _ => ListSeparator::Space.name(), + } + .to_owned(), + QuoteKind::None, + )) + }), + ); } diff --git a/src/common.rs b/src/common.rs index d7c5e25..927a4ba 100644 --- a/src/common.rs +++ b/src/common.rs @@ -382,8 +382,6 @@ impl ListSeparator { } } - // Used in currently unimplemented builtin function - #[allow(dead_code)] pub fn name(self) -> &'static str { match self { Self::Space => "space", diff --git a/tests/list.rs b/tests/list.rs index d6852ac..f31fd91 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -33,3 +33,25 @@ test!( "a {\n color: nth((a, b, c), 3);\n}\n", "a {\n color: c;\n}\n" ); +test!( + list_separator_space_separated, + "a {\n color: list-separator(a b c);\n}\n", + "a {\n color: space;\n}\n" +); +test!( + list_separator_foo, + "a {\n color: list-separator(foo);\n}\n", + "a {\n color: space;\n}\n" +); +test!( + list_separator_comma_separated, + "a {\n color: list-separator((a, b, c));\n}\n", + "a {\n color: comma;\n}\n" +); +// blocked on better parsing of comma separated lists with +// space separated lists inside +// test!( +// list_separator_comma_separated_with_space, +// "a {\n color: list-separator(((a b, c d)));\n}\n", +// "a {\n color: comma;\n}\n" +// );