Remove unwrap from FuncArgs

This commit is contained in:
ConnorSkees 2020-02-17 09:28:25 -05:00
parent dbe73fc2ac
commit 585011c621
4 changed files with 21 additions and 18 deletions

View File

@ -46,7 +46,7 @@ impl CallArgs {
pub(crate) fn eat_func_args<I: Iterator<Item = Token>>( pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
toks: &mut Peekable<I>, toks: &mut Peekable<I>,
scope: &Scope, scope: &Scope,
) -> FuncArgs { ) -> SassResult<FuncArgs> {
let mut args: Vec<FuncArg> = Vec::new(); let mut args: Vec<FuncArg> = Vec::new();
devour_whitespace(toks); devour_whitespace(toks);
@ -71,20 +71,20 @@ pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
toks.next(); toks.next();
args.push(FuncArg { args.push(FuncArg {
name, name,
default: Some( default: Some(Value::from_tokens(
Value::from_tokens(&mut default.into_iter().peekable(), scope) &mut default.into_iter().peekable(),
.unwrap(), scope,
), )?),
}); });
break; break;
} }
TokenKind::Symbol(Symbol::CloseParen) => { TokenKind::Symbol(Symbol::CloseParen) => {
args.push(FuncArg { args.push(FuncArg {
name, name,
default: Some( default: Some(Value::from_tokens(
Value::from_tokens(&mut default.into_iter().peekable(), scope) &mut default.into_iter().peekable(),
.unwrap(), scope,
), )?),
}); });
break; break;
} }
@ -102,9 +102,10 @@ pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
default: if default.is_empty() { default: if default.is_empty() {
None None
} else { } else {
Some( Some(Value::from_tokens(
Value::from_tokens(&mut default.into_iter().peekable(), scope).unwrap(), &mut default.into_iter().peekable(),
) scope,
)?)
}, },
}); });
break; break;
@ -126,7 +127,7 @@ pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(
} else { } else {
todo!("expected `{{` after args") todo!("expected `{{` after args")
} }
FuncArgs(args) Ok(FuncArgs(args))
} }
pub(crate) fn eat_call_args<I: Iterator<Item = Token>>( pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(

View File

@ -37,7 +37,7 @@ impl Function {
Some(Token { Some(Token {
kind: TokenKind::Symbol(Symbol::OpenParen), kind: TokenKind::Symbol(Symbol::OpenParen),
.. ..
}) => eat_func_args(toks, scope), }) => eat_func_args(toks, scope)?,
_ => return Err("expected \"(\".".into()), _ => return Err("expected \"(\".".into()),
}; };
@ -78,7 +78,7 @@ impl Function {
Ok(self) Ok(self)
} }
pub fn call(&self) -> Value { pub fn call(&self) -> SassResult<Value> {
for rule in &self.body { for rule in &self.body {
match rule { match rule {
AtRule::Return(toks) => { AtRule::Return(toks) => {
@ -86,7 +86,6 @@ impl Function {
&mut toks.clone().into_iter().peekable(), &mut toks.clone().into_iter().peekable(),
&self.scope, &self.scope,
) )
.unwrap()
} }
_ => todo!("unimplemented at rule in function body"), _ => todo!("unimplemented at rule in function body"),
} }

View File

@ -38,7 +38,7 @@ impl Mixin {
Some(Token { Some(Token {
kind: TokenKind::Symbol(Symbol::OpenParen), kind: TokenKind::Symbol(Symbol::OpenParen),
.. ..
}) => eat_func_args(toks, scope), }) => eat_func_args(toks, scope)?,
Some(Token { Some(Token {
kind: TokenKind::Symbol(Symbol::OpenCurlyBrace), kind: TokenKind::Symbol(Symbol::OpenCurlyBrace),
.. ..

View File

@ -260,7 +260,10 @@ impl Value {
} }
}, },
}; };
Ok(func.clone().args(&mut eat_call_args(toks, scope)?)?.call()) Ok(func
.clone()
.args(&mut eat_call_args(toks, scope)?)?
.call()?)
} }
_ => { _ => {
if let Ok(c) = crate::color::ColorName::try_from(s.as_ref()) { if let Ok(c) = crate::color::ColorName::try_from(s.as_ref()) {