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

View File

@ -14,7 +14,7 @@ use crate::{
macro_rules! opt_rgba { macro_rules! opt_rgba {
($args:ident, $name:ident, $arg:literal, $low:literal, $high:literal, $parser:ident) => { ($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::Dimension(n, u) => Some(bound!($args, $arg, n, u, $low, $high)),
Value::Null => None, Value::Null => None,
v => { v => {
@ -34,7 +34,7 @@ macro_rules! opt_rgba {
macro_rules! opt_hsl { macro_rules! opt_hsl {
($args:ident, $name:ident, $arg:literal, $low:literal, $high:literal, $parser:ident) => { ($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) => { Value::Dimension(n, u) => {
Some(bound!($args, $arg, n, u, $low, $high) / Number::from(100)) 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::Dimension(n, _) => Some(n),
Value::Null => None, Value::Null => None,
v => { 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::Dimension(n, _) => Some(n),
Value::Null => None, Value::Null => None,
v => { v => {
@ -205,7 +205,7 @@ fn scale_color(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
macro_rules! opt_scale_arg { macro_rules! opt_scale_arg {
($args:ident, $name:ident, $arg:literal, $low:literal, $high:literal, $parser:ident) => { ($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) => { Value::Dimension(n, Unit::Percent) => {
Some(bound!($args, $arg, n, Unit::Percent, $low, $high) / Number::from(100)) 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, 3,
"alpha", "alpha",
Value::Dimension(Number::one(), Unit::None), Value::Dimension(Number::one(), Unit::None),
)? { ) {
Value::Dimension(n, Unit::None) => n, Value::Dimension(n, Unit::None) => n,
Value::Dimension(n, Unit::Percent) => n / Number::from(100), Value::Dimension(n, Unit::Percent) => n / Number::from(100),
v @ Value::Dimension(..) => { v @ Value::Dimension(..) => {
@ -416,7 +416,7 @@ fn mix(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
2, 2,
"weight", "weight",
Value::Dimension(Number::from(50), Unit::None), Value::Dimension(Number::from(50), Unit::None),
)? { ) {
Value::Dimension(n, u) => bound!(args, "weight", n, u, 0, 100) / Number::from(100), Value::Dimension(n, u) => bound!(args, "weight", n, u, 0, 100) / Number::from(100),
v => { v => {
return Err(( return Err((

View File

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

View File

@ -138,7 +138,7 @@ fn comparable(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
#[cfg(feature = "random")] #[cfg(feature = "random")]
fn random(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> { fn random(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?; 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::Dimension(n, _) => n,
Value::Null => { Value::Null => {
let mut rng = rand::thread_rng(); 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 let css = parser
.default_arg(&mut args, 1, "css", Value::False)? .default_arg(&mut args, 1, "css", Value::False)
.is_true(args.span())?; .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::String(s, ..) => Some(s),
Value::Null => None, Value::Null => None,
v => { v => {
@ -246,10 +246,7 @@ fn content_exists(args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value>
.into()); .into());
} }
Ok(Value::bool( Ok(Value::bool(
parser parser.content.last().map_or(false, |c| c.content.is_some()),
.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()) .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() => { Value::Dimension(n, Unit::None) if n.is_decimal() => {
return Err((format!("{} is not an int.", n), args.span()).into()) return Err((format!("{} is not an int.", n), args.span()).into())
} }

View File

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