move builtin fns to outer scope to reduce nesting

This commit is contained in:
ConnorSkees 2020-04-30 19:36:34 -04:00
parent 170759239a
commit 0aed492123
9 changed files with 2759 additions and 2861 deletions

View File

@ -12,7 +12,6 @@ use crate::selector::Selector;
use crate::unit::Unit;
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) {
fn hsl(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
if args.is_empty() {
return Err(("Missing argument $channels.", args.span()).into());
@ -437,11 +436,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
}
fn saturation(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn saturation(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.saturation(), Unit::Percent)),
@ -453,11 +448,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
}
fn lightness(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn lightness(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => Ok(Value::Dimension(c.lightness(), Unit::Percent)),
@ -469,11 +460,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
}
fn adjust_hue(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn adjust_hue(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
@ -601,11 +588,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::Color(Box::new(color.saturate(amount))))
}
fn desaturate(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn desaturate(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
@ -633,11 +616,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::Color(Box::new(color.desaturate(amount))))
}
fn grayscale(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn grayscale(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
@ -658,11 +637,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::Color(Box::new(color.desaturate(Number::one()))))
}
fn complement(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn complement(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
@ -716,6 +691,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
}
pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert("hsl", Builtin::new(hsl));
f.insert("hsla", Builtin::new(hsla));
f.insert("hue", Builtin::new(hue));

View File

@ -10,7 +10,6 @@ use crate::unit::Unit;
use crate::value::Number;
use crate::value::Value;
pub(crate) fn register(f: &mut GlobalFunctionMap) {
fn alpha(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "color") {
@ -155,6 +154,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::Color(Box::new(color.fade_out(amount))))
}
pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert("alpha", Builtin::new(alpha));
f.insert("opacity", Builtin::new(opacity));
f.insert("opacify", Builtin::new(opacify));

View File

@ -54,16 +54,13 @@ macro_rules! opt_hsl {
};
}
pub(crate) fn register(f: &mut GlobalFunctionMap) {
fn change_color(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn change_color(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
if args.get_positional(1, scope, super_selector).is_some() {
return Err(
("Only one positional argument is allowed. All other arguments must be passed by name.", args.span()
).into());
return Err((
"Only one positional argument is allowed. All other arguments must be passed by name.",
args.span(),
)
.into());
}
let color = match arg!(args, scope, super_selector, 0, "color") {
@ -132,11 +129,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}))
}
fn adjust_color(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn adjust_color(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
v => {
@ -212,11 +205,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}))
}
fn scale_color(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn scale_color(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
fn scale(val: Number, by: Number, max: Number) -> Number {
if by.is_zero() {
return val;
@ -345,11 +334,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}))
}
fn ie_hex_str(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn ie_hex_str(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
let color = match arg!(args, scope, super_selector, 0, "color") {
Value::Color(c) => c,
@ -364,6 +349,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::Ident(color.to_ie_hex_str(), QuoteKind::None))
}
pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert("change-color", Builtin::new(change_color));
f.insert("adjust-color", Builtin::new(adjust_color));
f.insert("scale-color", Builtin::new(scale_color));

View File

@ -12,7 +12,6 @@ use crate::selector::Selector;
use crate::unit::Unit;
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) {
fn rgb(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
if args.is_empty() {
return Err(("Missing argument $channels.", args.span()).into());
@ -37,9 +36,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
let blue = match channels.pop() {
Some(Value::Dimension(n, Unit::None)) => n,
Some(Value::Dimension(n, Unit::Percent)) => {
(n / Number::from(100)) * Number::from(255)
}
Some(Value::Dimension(n, Unit::Percent)) => (n / Number::from(100)) * Number::from(255),
Some(v) if v.is_special_function() => {
let green = channels.pop().unwrap();
let red = channels.pop().unwrap();
@ -65,9 +62,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
let green = match channels.pop() {
Some(Value::Dimension(n, Unit::None)) => n,
Some(Value::Dimension(n, Unit::Percent)) => {
(n / Number::from(100)) * Number::from(255)
}
Some(Value::Dimension(n, Unit::Percent)) => (n / Number::from(100)) * Number::from(255),
Some(v) if v.is_special_function() => {
let string = match channels.pop() {
Some(red) => format!(
@ -92,9 +87,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
let red = match channels.pop() {
Some(Value::Dimension(n, Unit::None)) => n,
Some(Value::Dimension(n, Unit::Percent)) => {
(n / Number::from(100)) * Number::from(255)
}
Some(Value::Dimension(n, Unit::Percent)) => (n / Number::from(100)) * Number::from(255),
Some(v) if v.is_special_function() => {
return Ok(Value::Ident(
format!(
@ -356,9 +349,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
let blue = match channels.pop() {
Some(Value::Dimension(n, Unit::None)) => n,
Some(Value::Dimension(n, Unit::Percent)) => {
(n / Number::from(100)) * Number::from(255)
}
Some(Value::Dimension(n, Unit::Percent)) => (n / Number::from(100)) * Number::from(255),
Some(v) if v.is_special_function() => {
let green = channels.pop().unwrap();
let red = channels.pop().unwrap();
@ -384,9 +375,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
let green = match channels.pop() {
Some(Value::Dimension(n, Unit::None)) => n,
Some(Value::Dimension(n, Unit::Percent)) => {
(n / Number::from(100)) * Number::from(255)
}
Some(Value::Dimension(n, Unit::Percent)) => (n / Number::from(100)) * Number::from(255),
Some(v) if v.is_special_function() => {
let string = match channels.pop() {
Some(red) => format!(
@ -411,9 +400,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
let red = match channels.pop() {
Some(Value::Dimension(n, Unit::None)) => n,
Some(Value::Dimension(n, Unit::Percent)) => {
(n / Number::from(100)) * Number::from(255)
}
Some(Value::Dimension(n, Unit::Percent)) => (n / Number::from(100)) * Number::from(255),
Some(v) if v.is_special_function() => {
return Ok(Value::Ident(
format!(
@ -733,6 +720,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::Color(Box::new(color1.mix(&color2, weight))))
}
pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert("rgb", Builtin::new(rgb));
f.insert("rgba", Builtin::new(rgba));
f.insert("red", Builtin::new(red));

View File

@ -11,7 +11,6 @@ use crate::selector::Selector;
use crate::unit::Unit;
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) {
fn length(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
let len = match arg!(args, scope, super_selector, 0, "list") {
@ -247,11 +246,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::List(list1, sep, brackets))
}
fn is_bracketed(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn is_bracketed(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
Ok(Value::bool(
match arg!(args, scope, super_selector, 0, "list") {
@ -321,6 +316,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::List(result, ListSeparator::Comma, Brackets::None))
}
pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert("length", Builtin::new(length));
f.insert("nth", Builtin::new(nth));
f.insert("list-separator", Builtin::new(list_separator));

View File

@ -8,7 +8,6 @@ use crate::scope::Scope;
use crate::selector::Selector;
use crate::value::{SassMap, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) {
fn map_get(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
let key = arg!(args, scope, super_selector, 1, "key");
@ -26,11 +25,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(map.get(&key, args.span())?.unwrap_or(Value::Null))
}
fn map_has_key(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn map_has_key(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
let key = arg!(args, scope, super_selector, 1, "key");
let map = match arg!(args, scope, super_selector, 0, "map") {
@ -67,11 +62,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
))
}
fn map_values(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn map_values(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
let map = match arg!(args, scope, super_selector, 0, "map") {
Value::Map(m) => m,
@ -91,11 +82,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
))
}
fn map_merge(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn map_merge(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
let mut map1 = match arg!(args, scope, super_selector, 0, "map1") {
Value::Map(m) => m,
@ -123,11 +110,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::Map(map1))
}
fn map_remove(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn map_remove(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
let mut map = match arg!(args, scope, super_selector, 0, "map") {
Value::Map(m) => m,
Value::List(v, ..) if v.is_empty() => SassMap::new(),
@ -146,6 +129,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::Map(map))
}
pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert("map-get", Builtin::new(map_get));
f.insert("map-has-key", Builtin::new(map_has_key));
f.insert("map-keys", Builtin::new(map_keys));

View File

@ -13,12 +13,7 @@ use crate::selector::Selector;
use crate::unit::Unit;
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) {
fn percentage(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn percentage(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
let num = match arg!(args, scope, super_selector, 0, "number") {
Value::Dimension(n, Unit::None) => n * Number::from(100),
@ -106,11 +101,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
}
fn comparable(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn comparable(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
let unit1 = match arg!(args, scope, super_selector, 0, "number1") {
Value::Dimension(_, u) => u,
@ -198,6 +189,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
))
}
pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert("percentage", Builtin::new(percentage));
f.insert("round", Builtin::new(round));
f.insert("ceil", Builtin::new(ceil));

View File

@ -12,7 +12,6 @@ use crate::selector::Selector;
use crate::unit::Unit;
use crate::value::{SassFunction, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) {
fn if_(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(3)?;
if arg!(args, scope, super_selector, 0, "condition").is_true(args.span())? {
@ -84,6 +83,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
QuoteKind::None,
))
}
fn unitless(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "number") {
@ -133,11 +133,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
}
fn mixin_exists(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn mixin_exists(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(2)?;
match arg!(args, scope, super_selector, 0, "name") {
Value::Ident(s, _) => Ok(Value::bool(scope.mixin_exists(&s))),
@ -167,11 +163,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
}
fn get_function(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
fn get_function(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(3)?;
let name = match arg!(args, scope, super_selector, 0, "name") {
Value::Ident(s, _) => s,
@ -183,8 +175,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
.into())
}
};
let css =
arg!(args, scope, super_selector, 1, "css" = Value::False).is_true(args.span())?;
let css = arg!(args, scope, super_selector, 1, "css" = Value::False).is_true(args.span())?;
let module = match arg!(args, scope, super_selector, 2, "module" = Value::Null) {
Value::Ident(s, ..) => Some(s),
Value::Null => None,
@ -239,6 +230,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
func.call(args.decrement(), scope, super_selector)
}
pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert("if", Builtin::new(if_));
f.insert("feature-exists", Builtin::new(feature_exists));
f.insert("unit", Builtin::new(unit));

View File

@ -15,7 +15,6 @@ use crate::selector::Selector;
use crate::unit::Unit;
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) {
fn to_upper_case(
mut args: CallArgs,
scope: &Scope,
@ -60,11 +59,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
}
fn str_length(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
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(
@ -112,11 +107,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
}
fn str_slice(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
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),
@ -218,11 +209,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
}
fn str_index(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
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,
@ -258,11 +245,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
})
}
fn str_insert(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
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),
@ -374,6 +357,7 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Ok(Value::Ident(string, QuoteKind::None))
}
pub(crate) fn register(f: &mut GlobalFunctionMap) {
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));