From ef5f9a97493fe4a616b49a744d5a571a99ead8e9 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 20 Jun 2022 18:41:11 +0200 Subject: Refactor some code --- src/construct/partial_destination.rs | 63 ++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 32 deletions(-) (limited to 'src/construct/partial_destination.rs') diff --git a/src/construct/partial_destination.rs b/src/construct/partial_destination.rs index 901a10d..82e83fe 100644 --- a/src/construct/partial_destination.rs +++ b/src/construct/partial_destination.rs @@ -140,51 +140,50 @@ fn enclosed_escape(tokenizer: &mut Tokenizer, code: Code) -> StateFnResult { /// ```markdown /// a|b /// ``` -// To do: these arms can be improved? fn raw(tokenizer: &mut Tokenizer, code: Code, balance: usize) -> StateFnResult { // To do: configurable. let limit = usize::MAX; match code { - Code::Char('(') if balance >= limit => (State::Nok, None), Code::Char('(') => { - tokenizer.consume(code); - ( - State::Fn(Box::new(move |t, c| raw(t, c, balance + 1))), - None, - ) - } - Code::Char(')') if balance == 0 => { - tokenizer.exit(TokenType::ChunkString); - tokenizer.exit(TokenType::DefinitionDestinationString); - tokenizer.exit(TokenType::DefinitionDestinationRaw); - tokenizer.exit(TokenType::DefinitionDestination); - (State::Ok, Some(vec![code])) + if balance >= limit { + (State::Nok, None) + } else { + tokenizer.consume(code); + ( + State::Fn(Box::new(move |t, c| raw(t, c, balance + 1))), + None, + ) + } } Code::Char(')') => { - tokenizer.consume(code); - ( - State::Fn(Box::new(move |t, c| raw(t, c, balance - 1))), - None, - ) - } - Code::None - | Code::CarriageReturnLineFeed - | Code::VirtualSpace - | Code::Char('\t' | '\r' | '\n' | ' ') - if balance > 0 => - { - (State::Nok, None) + if balance == 0 { + tokenizer.exit(TokenType::ChunkString); + tokenizer.exit(TokenType::DefinitionDestinationString); + tokenizer.exit(TokenType::DefinitionDestinationRaw); + tokenizer.exit(TokenType::DefinitionDestination); + (State::Ok, Some(vec![code])) + } else { + tokenizer.consume(code); + ( + State::Fn(Box::new(move |t, c| raw(t, c, balance - 1))), + None, + ) + } } Code::None | Code::CarriageReturnLineFeed | Code::VirtualSpace | Code::Char('\t' | '\r' | '\n' | ' ') => { - tokenizer.exit(TokenType::ChunkString); - tokenizer.exit(TokenType::DefinitionDestinationString); - tokenizer.exit(TokenType::DefinitionDestinationRaw); - tokenizer.exit(TokenType::DefinitionDestination); - (State::Ok, Some(vec![code])) + if balance > 0 { + (State::Nok, None) + } else { + tokenizer.exit(TokenType::ChunkString); + tokenizer.exit(TokenType::DefinitionDestinationString); + tokenizer.exit(TokenType::DefinitionDestinationRaw); + tokenizer.exit(TokenType::DefinitionDestination); + (State::Ok, Some(vec![code])) + } } Code::Char(char) if char.is_ascii_control() => (State::Nok, None), Code::Char('\\') => { -- cgit