simplify declaration of string fns
This commit is contained in:
parent
26aabb42ad
commit
22098ca684
@ -29,6 +29,7 @@ pub(crate) struct Builtin(
|
||||
pub fn(CallArgs, &Scope, &Selector) -> SassResult<Value>,
|
||||
usize,
|
||||
);
|
||||
|
||||
impl Builtin {
|
||||
pub fn new(body: fn(CallArgs, &Scope, &Selector) -> SassResult<Value>) -> Builtin {
|
||||
let count = FUNCTION_COUNT.fetch_add(1, Ordering::Relaxed);
|
||||
|
@ -7,14 +7,20 @@ use num_traits::{Signed, ToPrimitive, Zero};
|
||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||
|
||||
use super::Builtin;
|
||||
use crate::args::CallArgs;
|
||||
use crate::common::QuoteKind;
|
||||
use crate::error::SassResult;
|
||||
use crate::scope::Scope;
|
||||
use crate::selector::Selector;
|
||||
use crate::unit::Unit;
|
||||
use crate::value::{Number, Value};
|
||||
|
||||
pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
f.insert(
|
||||
"to-upper-case",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
fn to_upper_case(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
match arg!(args, scope, super_selector, 0, "string") {
|
||||
Value::Ident(mut i, q) => {
|
||||
@ -30,11 +36,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"to-lower-case",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn to_lower_case(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
match arg!(args, scope, super_selector, 0, "string") {
|
||||
Value::Ident(mut i, q) => {
|
||||
@ -50,11 +58,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"str-length",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn str_length(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
match arg!(args, scope, super_selector, 0, "string") {
|
||||
Value::Ident(i, _) => Ok(Value::Dimension(
|
||||
@ -70,11 +80,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"quote",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn quote(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
match arg!(args, scope, super_selector, 0, "string") {
|
||||
Value::Ident(i, _) => Ok(Value::Ident(i, QuoteKind::Quoted)),
|
||||
@ -87,11 +95,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"unquote",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn unquote(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
match arg!(args, scope, super_selector, 0, "string") {
|
||||
i @ Value::Ident(..) => Ok(i.unquote()),
|
||||
@ -104,11 +110,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"str-slice",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn str_slice(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(3)?;
|
||||
let (string, quotes) = match arg!(args, scope, super_selector, 0, "string") {
|
||||
Value::Ident(s, q) => (s, q),
|
||||
@ -208,11 +216,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
quotes,
|
||||
))
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"str-index",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn str_index(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(2)?;
|
||||
let s1 = match arg!(args, scope, super_selector, 0, "string") {
|
||||
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),
|
||||
None => Value::Null,
|
||||
})
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"str-insert",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn str_insert(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(3)?;
|
||||
let (s1, quotes) = match arg!(args, scope, super_selector, 0, "string") {
|
||||
Value::Ident(i, q) => (i, q),
|
||||
@ -349,12 +361,10 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
};
|
||||
|
||||
Ok(Value::Ident(string, quotes))
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(feature = "random")]
|
||||
f.insert(
|
||||
"unique-id",
|
||||
Builtin::new(|args, _, _| {
|
||||
fn unique_id(args: CallArgs, _: &Scope, _: &Selector) -> SassResult<Value> {
|
||||
args.max_args(0)?;
|
||||
let mut rng = thread_rng();
|
||||
let string = std::iter::repeat(())
|
||||
@ -362,6 +372,16 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
.take(7)
|
||||
.collect();
|
||||
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));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user