grass/src/builtin/list.rs

328 lines
11 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2020-02-02 21:09:29 -05:00
2020-03-22 22:28:54 -04:00
use num_traits::{One, Signed, ToPrimitive, Zero};
2020-03-20 12:09:08 -04:00
2020-02-14 10:10:51 -05:00
use super::Builtin;
use crate::common::{Brackets, ListSeparator, QuoteKind};
use crate::unit::Unit;
2020-02-14 10:10:51 -05:00
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
f.insert(
"length".to_owned(),
Builtin::new(|mut args, scope, super_selector| {
2020-03-20 10:03:54 -04:00
max_args!(args, 1);
let len = match arg!(args, scope, super_selector, 0, "list") {
Value::List(v, ..) => Number::from(v.len()),
2020-03-30 16:01:44 -04:00
Value::Map(m) => Number::from(m.len()),
2020-03-22 22:28:54 -04:00
_ => Number::one(),
};
Ok(Value::Dimension(len, Unit::None))
}),
);
2020-03-20 12:09:08 -04:00
f.insert(
"nth".to_owned(),
Builtin::new(|mut args, scope, super_selector| {
2020-03-20 12:09:08 -04:00
max_args!(args, 2);
let list = match arg!(args, scope, super_selector, 0, "list") {
Value::List(v, ..) => v,
2020-04-02 15:22:18 -04:00
Value::Map(m) => m.entries(),
2020-03-20 12:36:31 -04:00
v => vec![v],
2020-03-20 12:09:08 -04:00
};
let n = match arg!(args, scope, super_selector, 1, "n") {
2020-03-20 12:09:08 -04:00
Value::Dimension(num, _) => num,
2020-04-12 19:37:12 -04:00
v => {
return Err((
format!("$n: {} is not a number.", v.to_css_string(args.span())?),
args.span(),
)
.into())
}
2020-03-20 12:09:08 -04:00
};
2020-03-22 22:28:54 -04:00
if n.is_zero() {
2020-04-12 19:37:12 -04:00
return Err(("$n: List index may not be 0.", args.span()).into());
2020-03-20 12:09:08 -04:00
}
if n.abs() > Number::from(list.len()) {
2020-04-12 19:37:12 -04:00
return Err((
format!(
"$n: Invalid index {} for a list with {} elements.",
n,
list.len()
),
args.span(),
2020-03-20 12:09:08 -04:00
)
2020-04-12 19:37:12 -04:00
.into());
2020-03-20 12:09:08 -04:00
}
if n.is_decimal() {
2020-04-12 19:37:12 -04:00
return Err((format!("$n: {} is not an int.", n), args.span()).into());
2020-03-20 12:09:08 -04:00
}
2020-03-22 22:28:54 -04:00
if n.is_positive() {
2020-03-20 12:09:08 -04:00
Ok(list[n.to_integer().to_usize().unwrap() - 1].clone())
} else {
Ok(list[list.len() - n.abs().to_integer().to_usize().unwrap()].clone())
}
}),
);
f.insert(
"list-separator".to_owned(),
Builtin::new(|mut args, scope, super_selector| {
max_args!(args, 1);
Ok(Value::Ident(
match arg!(args, scope, super_selector, 0, "list") {
Value::List(_, sep, ..) => sep.name(),
_ => ListSeparator::Space.name(),
}
.to_owned(),
QuoteKind::None,
))
}),
);
2020-03-20 19:27:26 -04:00
f.insert(
"set-nth".to_owned(),
Builtin::new(|mut args, scope, super_selector| {
2020-03-20 19:27:26 -04:00
max_args!(args, 3);
let (mut list, sep, brackets) = match arg!(args, scope, super_selector, 0, "list") {
2020-04-02 15:31:59 -04:00
Value::List(v, sep, b) => (v, sep, b),
Value::Map(m) => (m.entries(), ListSeparator::Comma, Brackets::None),
v => (vec![v], ListSeparator::Space, Brackets::None),
2020-03-20 19:27:26 -04:00
};
let n = match arg!(args, scope, super_selector, 1, "n") {
2020-03-20 19:27:26 -04:00
Value::Dimension(num, _) => num,
2020-04-12 19:37:12 -04:00
v => {
return Err((
format!("$n: {} is not a number.", v.to_css_string(args.span())?),
args.span(),
)
.into())
}
2020-03-20 19:27:26 -04:00
};
2020-03-22 22:28:54 -04:00
if n.is_zero() {
2020-04-12 19:37:12 -04:00
return Err(("$n: List index may not be 0.", args.span()).into());
2020-03-20 19:27:26 -04:00
}
let len = list.len();
if n.abs() > Number::from(len) {
2020-04-12 19:37:12 -04:00
return Err((
format!("$n: Invalid index {} for a list with {} elements.", n, len),
args.span(),
)
.into());
2020-03-20 19:27:26 -04:00
}
if n.is_decimal() {
2020-04-12 19:37:12 -04:00
return Err((format!("$n: {} is not an int.", n), args.span()).into());
2020-03-20 19:27:26 -04:00
}
let val = arg!(args, scope, super_selector, 2, "value");
2020-03-20 19:27:26 -04:00
2020-03-22 22:28:54 -04:00
if n.is_positive() {
2020-03-20 19:27:26 -04:00
list[n.to_integer().to_usize().unwrap() - 1] = val;
} else {
list[len - n.abs().to_integer().to_usize().unwrap()] = val;
}
2020-04-02 15:31:59 -04:00
Ok(Value::List(list, sep, brackets))
2020-03-20 19:50:23 -04:00
}),
);
f.insert(
"append".to_owned(),
Builtin::new(|mut args, scope, super_selector| {
2020-03-20 19:50:23 -04:00
max_args!(args, 3);
let (mut list, sep, brackets) = match arg!(args, scope, super_selector, 0, "list") {
2020-04-02 15:12:13 -04:00
Value::List(v, sep, b) => (v, sep, b),
v => (vec![v], ListSeparator::Space, Brackets::None),
2020-03-20 19:50:23 -04:00
};
let val = arg!(args, scope, super_selector, 1, "val");
2020-03-20 19:50:23 -04:00
let sep = match arg!(
args,
scope,
super_selector,
2020-03-20 19:50:23 -04:00
2,
"separator" = Value::Ident("auto".to_owned(), QuoteKind::None)
) {
Value::Ident(s, ..) => match s.as_str() {
"auto" => sep,
"comma" => ListSeparator::Comma,
"space" => ListSeparator::Space,
_ => {
2020-04-12 19:37:12 -04:00
return Err((
"$separator: Must be \"space\", \"comma\", or \"auto\".",
args.span(),
)
.into())
2020-03-20 19:50:23 -04:00
}
},
2020-04-12 19:37:12 -04:00
v => {
return Err((
format!(
"$separator: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
}
2020-03-20 19:50:23 -04:00
};
list.push(val);
2020-04-02 15:12:13 -04:00
Ok(Value::List(list, sep, brackets))
2020-03-20 19:27:26 -04:00
}),
);
2020-03-22 14:04:16 -04:00
f.insert(
"join".to_owned(),
Builtin::new(|mut args, scope, super_selector| {
2020-04-02 18:04:44 -04:00
max_args!(args, 4);
let (mut list1, sep1, brackets) = match arg!(args, scope, super_selector, 0, "list1") {
Value::List(v, sep, brackets) => (v, sep, brackets),
2020-04-02 18:23:57 -04:00
Value::Map(m) => (m.entries(), ListSeparator::Comma, Brackets::None),
v => (vec![v], ListSeparator::Space, Brackets::None),
2020-03-22 14:04:16 -04:00
};
let (list2, sep2) = match arg!(args, scope, super_selector, 1, "list2") {
2020-04-02 18:16:59 -04:00
Value::List(v, sep, ..) => (v, sep),
2020-04-02 18:23:57 -04:00
Value::Map(m) => (m.entries(), ListSeparator::Comma),
2020-04-02 18:16:59 -04:00
v => (vec![v], ListSeparator::Space),
2020-03-22 14:04:16 -04:00
};
let sep = match arg!(
args,
scope,
super_selector,
2020-03-22 14:04:16 -04:00
2,
"separator" = Value::Ident("auto".to_owned(), QuoteKind::None)
) {
Value::Ident(s, ..) => match s.as_str() {
"auto" => {
if list1.is_empty() || (list1.len() == 1 && sep1 == ListSeparator::Space) {
2020-04-02 18:16:59 -04:00
sep2
2020-03-22 14:04:16 -04:00
} else {
sep1
}
2020-03-22 15:08:13 -04:00
}
2020-03-22 14:04:16 -04:00
"comma" => ListSeparator::Comma,
"space" => ListSeparator::Space,
_ => {
2020-04-12 19:37:12 -04:00
return Err((
"$separator: Must be \"space\", \"comma\", or \"auto\".",
args.span(),
)
.into())
2020-03-22 14:04:16 -04:00
}
},
2020-04-12 19:37:12 -04:00
v => {
return Err((
format!(
"$separator: {} is not a string.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
}
2020-03-22 14:04:16 -04:00
};
2020-04-02 18:04:44 -04:00
let brackets = match arg!(
args,
scope,
super_selector,
2020-04-02 18:04:44 -04:00
3,
"bracketed" = Value::Ident("auto".to_owned(), QuoteKind::None)
) {
Value::Ident(s, ..) => match s.as_str() {
"auto" => brackets,
_ => Brackets::Bracketed,
},
v => {
2020-04-12 19:37:12 -04:00
if v.is_true(args.span())? {
2020-04-02 18:04:44 -04:00
Brackets::Bracketed
} else {
Brackets::None
}
}
};
2020-03-22 14:04:16 -04:00
list1.extend(list2);
Ok(Value::List(list1, sep, brackets))
2020-03-22 14:04:16 -04:00
}),
);
f.insert(
"is-bracketed".to_owned(),
Builtin::new(|mut args, scope, super_selector| {
max_args!(args, 1);
Ok(Value::bool(
match arg!(args, scope, super_selector, 0, "list") {
Value::List(.., brackets) => match brackets {
Brackets::Bracketed => true,
Brackets::None => false,
},
_ => false,
},
))
}),
);
2020-04-02 14:22:38 -04:00
f.insert(
"index".to_owned(),
Builtin::new(|mut args, scope, super_selector| {
2020-04-02 14:22:38 -04:00
max_args!(args, 2);
let list = match arg!(args, scope, super_selector, 0, "list") {
2020-04-02 14:22:38 -04:00
Value::List(v, ..) => v,
2020-04-03 13:43:34 -04:00
Value::Map(m) => m.entries(),
2020-04-02 14:22:38 -04:00
v => vec![v],
};
let value = arg!(args, scope, super_selector, 1, "value");
2020-04-03 14:38:22 -04:00
// TODO: find a way around this unwrap.
// It should be impossible to hit as the arg is
// evaluated prior to checking equality, but
2020-04-03 15:54:59 -04:00
// it is still dirty.
2020-04-03 14:38:22 -04:00
// Potential input to fuzz: index(1px 1in 1cm, 96px + 1rem)
2020-04-03 14:34:59 -04:00
let index = match list
.into_iter()
2020-04-12 19:37:12 -04:00
.position(|v| v.equals(value.clone(), args.span()).unwrap())
2020-04-03 14:34:59 -04:00
{
2020-04-02 14:22:38 -04:00
Some(v) => Number::from(v + 1),
None => return Ok(Value::Null),
};
Ok(Value::Dimension(index, Unit::None))
}),
);
2020-04-03 15:54:59 -04:00
f.insert(
"zip".to_owned(),
Builtin::new(|args, scope, super_selector| {
2020-04-03 15:54:59 -04:00
let lists = args
.get_variadic(scope, super_selector)?
2020-04-03 15:54:59 -04:00
.into_iter()
2020-04-12 19:37:12 -04:00
.map(|x| match x.node {
2020-04-03 15:54:59 -04:00
Value::List(v, ..) => v,
Value::Map(m) => m.entries(),
v => vec![v],
})
.collect::<Vec<Vec<Value>>>();
let len = lists.iter().map(|l| l.len()).min().unwrap_or(0);
if len == 0 {
return Ok(Value::List(
Vec::new(),
ListSeparator::Comma,
Brackets::None,
));
}
let result = (0..len)
.map(|i| {
let items = lists.iter().map(|v| v[i].clone()).collect();
Value::List(items, ListSeparator::Space, Brackets::None)
})
.collect();
Ok(Value::List(result, ListSeparator::Comma, Brackets::None))
}),
);
2020-02-14 10:10:51 -05:00
}