default arg fns are infallible

This commit is contained in:
Connor Skees 2020-07-02 16:37:24 -04:00
parent 927faf30c6
commit 94d94abf7a
8 changed files with 23 additions and 26 deletions

View File

@ -184,7 +184,7 @@ fn inner_hsl(name: &'static str, mut args: CallArgs, parser: &mut Parser<'_>) ->
3,
"alpha",
Value::Dimension(Number::one(), Unit::None),
)? {
) {
Value::Dimension(n, Unit::None) => n,
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
v @ Value::Dimension(..) => {
@ -469,7 +469,7 @@ fn invert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
1,
"weight",
Value::Dimension(Number::from(100), Unit::Percent),
)? {
) {
Value::Dimension(n, u) => bound!(args, "weight", n, u, 0, 100) / Number::from(100),
v => {
return Err((

View File

@ -14,7 +14,7 @@ use crate::{
macro_rules! opt_rgba {
($args:ident, $name:ident, $arg:literal, $low:literal, $high:literal, $parser:ident) => {
let $name = match $parser.default_named_arg(&mut $args, $arg, Value::Null)? {
let $name = match $parser.default_named_arg(&mut $args, $arg, Value::Null) {
Value::Dimension(n, u) => Some(bound!($args, $arg, n, u, $low, $high)),
Value::Null => None,
v => {
@ -34,7 +34,7 @@ macro_rules! opt_rgba {
macro_rules! opt_hsl {
($args:ident, $name:ident, $arg:literal, $low:literal, $high:literal, $parser:ident) => {
let $name = match $parser.default_named_arg(&mut $args, $arg, Value::Null)? {
let $name = match $parser.default_named_arg(&mut $args, $arg, Value::Null) {
Value::Dimension(n, u) => {
Some(bound!($args, $arg, n, u, $low, $high) / Number::from(100))
}
@ -88,7 +88,7 @@ fn change_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value
))));
}
let hue = match parser.default_named_arg(&mut args, "hue", Value::Null)? {
let hue = match parser.default_named_arg(&mut args, "hue", Value::Null) {
Value::Dimension(n, _) => Some(n),
Value::Null => None,
v => {
@ -147,7 +147,7 @@ fn adjust_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value
))));
}
let hue = match parser.default_named_arg(&mut args, "hue", Value::Null)? {
let hue = match parser.default_named_arg(&mut args, "hue", Value::Null) {
Value::Dimension(n, _) => Some(n),
Value::Null => None,
v => {
@ -205,7 +205,7 @@ fn scale_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
macro_rules! opt_scale_arg {
($args:ident, $name:ident, $arg:literal, $low:literal, $high:literal, $parser:ident) => {
let $name = match $parser.default_named_arg(&mut $args, $arg, Value::Null)? {
let $name = match $parser.default_named_arg(&mut $args, $arg, Value::Null) {
Value::Dimension(n, Unit::Percent) => {
Some(bound!($args, $arg, n, Unit::Percent, $low, $high) / Number::from(100))
}

View File

@ -305,7 +305,7 @@ fn inner_rgb(name: &'static str, mut args: CallArgs, parser: &mut Parser<'_>) ->
3,
"alpha",
Value::Dimension(Number::one(), Unit::None),
)? {
) {
Value::Dimension(n, Unit::None) => n,
Value::Dimension(n, Unit::Percent) => n / Number::from(100),
v @ Value::Dimension(..) => {
@ -416,7 +416,7 @@ fn mix(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
2,
"weight",
Value::Dimension(Number::from(50), Unit::None),
)? {
) {
Value::Dimension(n, u) => bound!(args, "weight", n, u, 0, 100) / Number::from(100),
v => {
return Err((

View File

@ -131,7 +131,7 @@ fn append(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
2,
"separator",
Value::String("auto".to_owned(), QuoteKind::None),
)? {
) {
Value::String(s, ..) => match s.as_str() {
"auto" => sep,
"comma" => ListSeparator::Comma,
@ -178,7 +178,7 @@ fn join(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
2,
"separator",
Value::String("auto".to_owned(), QuoteKind::None),
)? {
) {
Value::String(s, ..) => match s.as_str() {
"auto" => {
if list1.is_empty() || (list1.len() == 1 && sep1 == ListSeparator::Space) {
@ -214,7 +214,7 @@ fn join(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
3,
"bracketed",
Value::String("auto".to_owned(), QuoteKind::None),
)? {
) {
Value::String(s, ..) => match s.as_str() {
"auto" => brackets,
_ => Brackets::Bracketed,

View File

@ -138,7 +138,7 @@ fn comparable(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
#[cfg(feature = "random")]
fn random(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
let limit = match parser.default_arg(&mut args, 0, "limit", Value::Null)? {
let limit = match parser.default_arg(&mut args, 0, "limit", Value::Null) {
Value::Dimension(n, _) => n,
Value::Null => {
let mut rng = rand::thread_rng();

View File

@ -176,9 +176,9 @@ fn get_function(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value
}
};
let css = parser
.default_arg(&mut args, 1, "css", Value::False)?
.default_arg(&mut args, 1, "css", Value::False)
.is_true(args.span())?;
let module = match parser.default_arg(&mut args, 2, "module", Value::Null)? {
let module = match parser.default_arg(&mut args, 2, "module", Value::Null) {
Value::String(s, ..) => Some(s),
Value::Null => None,
v => {
@ -246,10 +246,7 @@ fn content_exists(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
.into());
}
Ok(Value::bool(
parser
.content
.last()
.map_or(false, |c| c.content.is_some())
parser.content.last().map_or(false, |c| c.content.is_some()),
))
}

View File

@ -148,7 +148,7 @@ fn str_slice(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
.into())
}
};
let mut end = match parser.default_arg(&mut args, 2, "end-at", Value::Null)? {
let mut end = match parser.default_arg(&mut args, 2, "end-at", Value::Null) {
Value::Dimension(n, Unit::None) if n.is_decimal() => {
return Err((format!("{} is not an int.", n), args.span()).into())
}

View File

@ -237,11 +237,11 @@ impl<'a> Parser<'a> {
position: usize,
name: &'static str,
default: Value,
) -> SassResult<Value> {
Ok(match args.get(position, name) {
) -> Value {
match args.get(position, name) {
Some(val) => val.node,
None => default,
})
}
}
pub fn positional_arg(
@ -262,11 +262,11 @@ impl<'a> Parser<'a> {
args: &mut CallArgs,
name: &'static str,
default: Value,
) -> SassResult<Value> {
Ok(match args.get_named(name) {
) -> Value {
match args.get_named(name) {
Some(val) => val.node,
None => default,
})
}
}
pub fn variadic_args(&mut self, args: CallArgs) -> SassResult<Vec<Spanned<Value>>> {