simplify declaration of string fns

This commit is contained in:
ConnorSkees 2020-04-30 18:41:33 -04:00
parent 26aabb42ad
commit 22098ca684
2 changed files with 335 additions and 314 deletions

View File

@ -29,6 +29,7 @@ pub(crate) struct Builtin(
pub fn(CallArgs, &Scope, &Selector) -> SassResult<Value>, pub fn(CallArgs, &Scope, &Selector) -> SassResult<Value>,
usize, usize,
); );
impl Builtin { impl Builtin {
pub fn new(body: fn(CallArgs, &Scope, &Selector) -> SassResult<Value>) -> Builtin { pub fn new(body: fn(CallArgs, &Scope, &Selector) -> SassResult<Value>) -> Builtin {
let count = FUNCTION_COUNT.fetch_add(1, Ordering::Relaxed); let count = FUNCTION_COUNT.fetch_add(1, Ordering::Relaxed);

View File

@ -7,14 +7,20 @@ use num_traits::{Signed, ToPrimitive, Zero};
use rand::{distributions::Alphanumeric, thread_rng, Rng}; use rand::{distributions::Alphanumeric, thread_rng, Rng};
use super::Builtin; use super::Builtin;
use crate::args::CallArgs;
use crate::common::QuoteKind; use crate::common::QuoteKind;
use crate::error::SassResult;
use crate::scope::Scope;
use crate::selector::Selector;
use crate::unit::Unit; use crate::unit::Unit;
use crate::value::{Number, Value}; use crate::value::{Number, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) { pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert( fn to_upper_case(
"to-upper-case", mut args: CallArgs,
Builtin::new(|mut args, scope, super_selector| { scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "string") { match arg!(args, scope, super_selector, 0, "string") {
Value::Ident(mut i, q) => { Value::Ident(mut i, q) => {
@ -30,11 +36,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
) )
.into()), .into()),
} }
}), }
);
f.insert( fn to_lower_case(
"to-lower-case", mut args: CallArgs,
Builtin::new(|mut args, scope, super_selector| { scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "string") { match arg!(args, scope, super_selector, 0, "string") {
Value::Ident(mut i, q) => { Value::Ident(mut i, q) => {
@ -50,11 +58,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
) )
.into()), .into()),
} }
}), }
);
f.insert( fn str_length(
"str-length", mut args: CallArgs,
Builtin::new(|mut args, scope, super_selector| { scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "string") { match arg!(args, scope, super_selector, 0, "string") {
Value::Ident(i, _) => Ok(Value::Dimension( Value::Ident(i, _) => Ok(Value::Dimension(
@ -70,11 +80,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
) )
.into()), .into()),
} }
}), }
);
f.insert( fn quote(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
"quote",
Builtin::new(|mut args, scope, super_selector| {
args.max_args(1)?; args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "string") { match arg!(args, scope, super_selector, 0, "string") {
Value::Ident(i, _) => Ok(Value::Ident(i, QuoteKind::Quoted)), Value::Ident(i, _) => Ok(Value::Ident(i, QuoteKind::Quoted)),
@ -87,11 +95,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
) )
.into()), .into()),
} }
}), }
);
f.insert( fn unquote(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
"unquote",
Builtin::new(|mut args, scope, super_selector| {
args.max_args(1)?; args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "string") { match arg!(args, scope, super_selector, 0, "string") {
i @ Value::Ident(..) => Ok(i.unquote()), i @ Value::Ident(..) => Ok(i.unquote()),
@ -104,11 +110,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
) )
.into()), .into()),
} }
}), }
);
f.insert( fn str_slice(
"str-slice", mut args: CallArgs,
Builtin::new(|mut args, scope, super_selector| { scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(3)?; args.max_args(3)?;
let (string, quotes) = match arg!(args, scope, super_selector, 0, "string") { let (string, quotes) = match arg!(args, scope, super_selector, 0, "string") {
Value::Ident(s, q) => (s, q), Value::Ident(s, q) => (s, q),
@ -208,11 +216,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
quotes, quotes,
)) ))
} }
}), }
);
f.insert( fn str_index(
"str-index", mut args: CallArgs,
Builtin::new(|mut args, scope, super_selector| { scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
let s1 = match arg!(args, scope, super_selector, 0, "string") { let s1 = match arg!(args, scope, super_selector, 0, "string") {
Value::Ident(i, _) => i, Value::Ident(i, _) => i,
@ -246,11 +256,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Some(v) => Value::Dimension(Number::from(v + 1), Unit::None), Some(v) => Value::Dimension(Number::from(v + 1), Unit::None),
None => Value::Null, None => Value::Null,
}) })
}), }
);
f.insert( fn str_insert(
"str-insert", mut args: CallArgs,
Builtin::new(|mut args, scope, super_selector| { scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(3)?; args.max_args(3)?;
let (s1, quotes) = match arg!(args, scope, super_selector, 0, "string") { let (s1, quotes) = match arg!(args, scope, super_selector, 0, "string") {
Value::Ident(i, q) => (i, q), Value::Ident(i, q) => (i, q),
@ -349,12 +361,10 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}; };
Ok(Value::Ident(string, quotes)) Ok(Value::Ident(string, quotes))
}), }
);
#[cfg(feature = "random")] #[cfg(feature = "random")]
f.insert( fn unique_id(args: CallArgs, _: &Scope, _: &Selector) -> SassResult<Value> {
"unique-id",
Builtin::new(|args, _, _| {
args.max_args(0)?; args.max_args(0)?;
let mut rng = thread_rng(); let mut rng = thread_rng();
let string = std::iter::repeat(()) let string = std::iter::repeat(())
@ -362,6 +372,16 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
.take(7) .take(7)
.collect(); .collect();
Ok(Value::Ident(string, QuoteKind::None)) Ok(Value::Ident(string, QuoteKind::None))
}), }
);
f.insert("to-upper-case", Builtin::new(to_upper_case));
f.insert("to-lower-case", Builtin::new(to_lower_case));
f.insert("str-length", Builtin::new(str_length));
f.insert("quote", Builtin::new(quote));
f.insert("unquote", Builtin::new(unquote));
f.insert("str-slice", Builtin::new(str_slice));
f.insert("str-index", Builtin::new(str_index));
f.insert("str-insert", Builtin::new(str_insert));
#[cfg(feature = "random")]
f.insert("unique-id", Builtin::new(unique_id));
} }