From 94d94abf7ab31c0bb6d4dadcc93ab34f6349e1d4 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Thu, 2 Jul 2020 16:37:24 -0400 Subject: [PATCH] default arg fns are infallible --- src/builtin/color/hsl.rs | 4 ++-- src/builtin/color/other.rs | 10 +++++----- src/builtin/color/rgb.rs | 4 ++-- src/builtin/list.rs | 6 +++--- src/builtin/math.rs | 2 +- src/builtin/meta.rs | 9 +++------ src/builtin/string.rs | 2 +- src/parse/args.rs | 12 ++++++------ 8 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/builtin/color/hsl.rs b/src/builtin/color/hsl.rs index d4ab7ec..79bd612 100644 --- a/src/builtin/color/hsl.rs +++ b/src/builtin/color/hsl.rs @@ -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 { 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(( diff --git a/src/builtin/color/other.rs b/src/builtin/color/other.rs index 115c50d..38d69cd 100644 --- a/src/builtin/color/other.rs +++ b/src/builtin/color/other.rs @@ -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 Some(n), Value::Null => None, v => { @@ -147,7 +147,7 @@ fn adjust_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult Some(n), Value::Null => None, v => { @@ -205,7 +205,7 @@ fn scale_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult 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)) } diff --git a/src/builtin/color/rgb.rs b/src/builtin/color/rgb.rs index 110c5e7..4fdc5e6 100644 --- a/src/builtin/color/rgb.rs +++ b/src/builtin/color/rgb.rs @@ -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 { 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(( diff --git a/src/builtin/list.rs b/src/builtin/list.rs index b808da2..17ebe2c 100644 --- a/src/builtin/list.rs +++ b/src/builtin/list.rs @@ -131,7 +131,7 @@ fn append(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { 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 { 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 { 3, "bracketed", Value::String("auto".to_owned(), QuoteKind::None), - )? { + ) { Value::String(s, ..) => match s.as_str() { "auto" => brackets, _ => Brackets::Bracketed, diff --git a/src/builtin/math.rs b/src/builtin/math.rs index 5fc13f8..dacf5f5 100644 --- a/src/builtin/math.rs +++ b/src/builtin/math.rs @@ -138,7 +138,7 @@ fn comparable(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult #[cfg(feature = "random")] fn random(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { 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(); diff --git a/src/builtin/meta.rs b/src/builtin/meta.rs index 570da32..248e3f0 100644 --- a/src/builtin/meta.rs +++ b/src/builtin/meta.rs @@ -176,9 +176,9 @@ fn get_function(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult Some(s), Value::Null => None, v => { @@ -246,10 +246,7 @@ fn content_exists(args: CallArgs, parser: &mut Parser<'_>) -> SassResult .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()), )) } diff --git a/src/builtin/string.rs b/src/builtin/string.rs index 521ba54..70c8ad1 100644 --- a/src/builtin/string.rs +++ b/src/builtin/string.rs @@ -148,7 +148,7 @@ fn str_slice(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { .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()) } diff --git a/src/parse/args.rs b/src/parse/args.rs index 9bd48d8..d602a1c 100644 --- a/src/parse/args.rs +++ b/src/parse/args.rs @@ -237,11 +237,11 @@ impl<'a> Parser<'a> { position: usize, name: &'static str, default: Value, - ) -> SassResult { - 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 { - 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>> {