Favor pattern-match over if-else when parsing assignment kinds

Equality on a union-type is potentially dangerous as the compiler won't
complain if we add a new case that we don't cover. Reversing the
assignment by yielding a `Token` for a given `AssignmentKind`. This way
we can use a pattern-match that got us covered for future cases.
This commit is contained in:
KtorZ 2023-07-05 14:01:13 +02:00
parent 93e010b345
commit 44eb501d78
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
1 changed files with 6 additions and 7 deletions

View File

@ -9,23 +9,22 @@ use crate::{
pub fn let_( pub fn let_(
r: Recursive<'_, Token, UntypedExpr, ParseError>, r: Recursive<'_, Token, UntypedExpr, ParseError>,
) -> impl Parser<Token, UntypedExpr, Error = ParseError> + '_ { ) -> impl Parser<Token, UntypedExpr, Error = ParseError> + '_ {
assignment(r, Token::Let) assignment(r, ast::AssignmentKind::Let)
} }
pub fn expect( pub fn expect(
r: Recursive<'_, Token, UntypedExpr, ParseError>, r: Recursive<'_, Token, UntypedExpr, ParseError>,
) -> impl Parser<Token, UntypedExpr, Error = ParseError> + '_ { ) -> impl Parser<Token, UntypedExpr, Error = ParseError> + '_ {
assignment(r, Token::Expect) assignment(r, ast::AssignmentKind::Expect)
} }
fn assignment( fn assignment(
r: Recursive<'_, Token, UntypedExpr, ParseError>, r: Recursive<'_, Token, UntypedExpr, ParseError>,
keyword: Token, kind: ast::AssignmentKind,
) -> impl Parser<Token, UntypedExpr, Error = ParseError> + '_ { ) -> impl Parser<Token, UntypedExpr, Error = ParseError> + '_ {
let kind = if keyword == Token::Let { let keyword = match kind {
ast::AssignmentKind::Let ast::AssignmentKind::Let => Token::Let,
} else { ast::AssignmentKind::Expect => Token::Expect,
ast::AssignmentKind::Expect
}; };
just(keyword) just(keyword)