diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-08-02 14:27:31 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-08-02 14:27:31 +0200 |
commit | 8ffed1822bcbc1b6ce6647b840fb03996b0635ea (patch) | |
tree | dc01319d360b6e5ec08c893413ff056d3abd43bc /src/construct/thematic_break.rs | |
parent | 13cf914b5162fb12c842e983538b42d4b60cdcb8 (diff) | |
download | markdown-rs-8ffed1822bcbc1b6ce6647b840fb03996b0635ea.tar.gz markdown-rs-8ffed1822bcbc1b6ce6647b840fb03996b0635ea.tar.bz2 markdown-rs-8ffed1822bcbc1b6ce6647b840fb03996b0635ea.zip |
Refactor to remove most closures
Diffstat (limited to '')
-rw-r--r-- | src/construct/thematic_break.rs | 50 |
1 files changed, 24 insertions, 26 deletions
diff --git a/src/construct/thematic_break.rs b/src/construct/thematic_break.rs index 785d132..2ed2046 100644 --- a/src/construct/thematic_break.rs +++ b/src/construct/thematic_break.rs @@ -53,15 +53,6 @@ use crate::constant::{TAB_SIZE, THEMATIC_BREAK_MARKER_COUNT_MIN}; use crate::token::Token; use crate::tokenizer::{State, Tokenizer}; -/// State needed to parse thematic breaks. -#[derive(Debug)] -struct Info { - /// Marker. - marker: u8, - /// Number of markers. - size: usize, -} - /// Start of a thematic break. /// /// ```markdown @@ -95,13 +86,10 @@ pub fn start(tokenizer: &mut Tokenizer) -> State { /// ``` fn before(tokenizer: &mut Tokenizer) -> State { match tokenizer.current { - Some(b'*' | b'-' | b'_') => at_break( - tokenizer, - Info { - marker: tokenizer.current.unwrap(), - size: 0, - }, - ), + Some(b'*' | b'-' | b'_') => { + tokenizer.tokenize_state.marker = tokenizer.current.unwrap(); + at_break(tokenizer) + } _ => State::Nok, } } @@ -112,19 +100,27 @@ fn before(tokenizer: &mut Tokenizer) -> State { /// > | *** /// ^ /// ``` -fn at_break(tokenizer: &mut Tokenizer, info: Info) -> State { +fn at_break(tokenizer: &mut Tokenizer) -> State { match tokenizer.current { - None | Some(b'\n') if info.size >= THEMATIC_BREAK_MARKER_COUNT_MIN => { + None | Some(b'\n') if tokenizer.tokenize_state.size >= THEMATIC_BREAK_MARKER_COUNT_MIN => { + tokenizer.tokenize_state.marker = 0; + tokenizer.tokenize_state.size = 0; tokenizer.exit(Token::ThematicBreak); // Feel free to interrupt. tokenizer.interrupt = false; State::Ok } - Some(b'*' | b'-' | b'_') if tokenizer.current.unwrap() == info.marker => { + Some(b'*' | b'-' | b'_') + if tokenizer.current.unwrap() == tokenizer.tokenize_state.marker => + { tokenizer.enter(Token::ThematicBreakSequence); - sequence(tokenizer, info) + sequence(tokenizer) + } + _ => { + tokenizer.tokenize_state.marker = 0; + tokenizer.tokenize_state.size = 0; + State::Nok } - _ => State::Nok, } } @@ -134,16 +130,18 @@ fn at_break(tokenizer: &mut Tokenizer, info: Info) -> State { /// > | *** /// ^ /// ``` -fn sequence(tokenizer: &mut Tokenizer, mut info: Info) -> State { +fn sequence(tokenizer: &mut Tokenizer) -> State { match tokenizer.current { - Some(b'*' | b'-' | b'_') if tokenizer.current.unwrap() == info.marker => { + Some(b'*' | b'-' | b'_') + if tokenizer.current.unwrap() == tokenizer.tokenize_state.marker => + { tokenizer.consume(); - info.size += 1; - State::Fn(Box::new(|t| sequence(t, info))) + tokenizer.tokenize_state.size += 1; + State::Fn(Box::new(sequence)) } _ => { tokenizer.exit(Token::ThematicBreakSequence); - tokenizer.attempt_opt(space_or_tab(), |t| at_break(t, info))(tokenizer) + tokenizer.attempt_opt(space_or_tab(), at_break)(tokenizer) } } } |