diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-06-21 16:06:50 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-06-21 16:06:50 +0200 |
commit | c6f92eaedf197beafef461ee6c2bd067e7160c49 (patch) | |
tree | c919113a51065c5d8126991cde1edf7fd6609e8b /src/construct/heading_setext.rs | |
parent | bcc4676b84a06af5e38ebaa31f0217cae090be08 (diff) | |
download | markdown-rs-c6f92eaedf197beafef461ee6c2bd067e7160c49.tar.gz markdown-rs-c6f92eaedf197beafef461ee6c2bd067e7160c49.tar.bz2 markdown-rs-c6f92eaedf197beafef461ee6c2bd067e7160c49.zip |
Refactor to improve a bunch of states
* Improve passing stuff around
* Add traits to enums for markers and such
* Fix “life time” stuff I didn’t understand
Diffstat (limited to '')
-rw-r--r-- | src/construct/heading_setext.rs | 56 |
1 files changed, 42 insertions, 14 deletions
diff --git a/src/construct/heading_setext.rs b/src/construct/heading_setext.rs index a418041..7c41855 100644 --- a/src/construct/heading_setext.rs +++ b/src/construct/heading_setext.rs @@ -57,12 +57,49 @@ use crate::util::{link::link, span::from_exit_event}; /// Kind of underline. #[derive(Debug, Clone, PartialEq)] pub enum Kind { - /// Grave accent (tick) code. + /// Dash (rank 2) heading. + /// + /// ## Example + /// + /// ```markdown + /// alpha + /// ----- + /// ``` Dash, - /// Tilde code. + + /// Equals to (rank 1) heading. + /// + /// ## Example + /// + /// ```markdown + /// alpha + /// ===== + /// ``` EqualsTo, } +impl Kind { + /// Turn the kind into a [char]. + fn as_char(&self) -> char { + match self { + Kind::Dash => '-', + Kind::EqualsTo => '=', + } + } + /// Turn a [char] into a kind. + /// + /// ## Panics + /// + /// Panics if `char` is not `-` or `=`. + fn from_char(char: char) -> Kind { + match char { + '-' => Kind::Dash, + '=' => Kind::EqualsTo, + _ => unreachable!("invalid char"), + } + } +} + /// Start of a heading (setext). /// /// ```markdown @@ -232,13 +269,8 @@ fn underline_sequence_start(tokenizer: &mut Tokenizer, code: Code) -> StateFnRes match code { Code::Char(char) if char == '-' || char == '=' => { - let marker = if char == '-' { - Kind::Dash - } else { - Kind::EqualsTo - }; tokenizer.enter(TokenType::HeadingSetextUnderline); - underline_sequence_inside(tokenizer, code, marker) + underline_sequence_inside(tokenizer, code, Kind::from_char(char)) } _ => (State::Nok, None), } @@ -251,15 +283,11 @@ fn underline_sequence_start(tokenizer: &mut Tokenizer, code: Code) -> StateFnRes /// =|= /// ``` fn underline_sequence_inside(tokenizer: &mut Tokenizer, code: Code, kind: Kind) -> StateFnResult { - let marker = if kind == Kind::Dash { '-' } else { '=' }; - match code { - Code::Char(char) if char == marker => { + Code::Char(char) if char == kind.as_char() => { tokenizer.consume(code); ( - State::Fn(Box::new(move |tokenizer, code| { - underline_sequence_inside(tokenizer, code, kind) - })), + State::Fn(Box::new(move |t, c| underline_sequence_inside(t, c, kind))), None, ) } |