aboutsummaryrefslogtreecommitdiffstats
path: root/src/construct/heading_setext.rs
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-07-29 18:22:59 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-07-29 18:22:59 +0200
commit0eeff9148e327183e532752f46421a75506dd7a6 (patch)
tree4f0aed04f90aa759ce96a2e87aa719e7fa95c450 /src/construct/heading_setext.rs
parent148ede7f0f42f0ccb1620b13d91f35d0c7d04c2f (diff)
downloadmarkdown-rs-0eeff9148e327183e532752f46421a75506dd7a6.tar.gz
markdown-rs-0eeff9148e327183e532752f46421a75506dd7a6.tar.bz2
markdown-rs-0eeff9148e327183e532752f46421a75506dd7a6.zip
Refactor to improve states
* Remove custom kind wrappers, use plain bytes instead * Remove `Into`s, use the explicit expected types instead * Refactor to use `slice.as_str` in most places * Remove unneeded unique check before adding a definition * Use a shared CDATA prefix in constants * Inline byte checks into matches * Pass bytes back from parser instead of whole parse state * Refactor to work more often on bytes * Rename custom `size` to `len`
Diffstat (limited to 'src/construct/heading_setext.rs')
-rw-r--r--src/construct/heading_setext.rs96
1 files changed, 28 insertions, 68 deletions
diff --git a/src/construct/heading_setext.rs b/src/construct/heading_setext.rs
index 2a4adbf..98d7843 100644
--- a/src/construct/heading_setext.rs
+++ b/src/construct/heading_setext.rs
@@ -63,52 +63,6 @@ use crate::token::Token;
use crate::tokenizer::{EventType, State, Tokenizer};
use crate::util::skip::opt_back as skip_opt_back;
-/// Kind of underline.
-#[derive(Debug, Clone, PartialEq)]
-pub enum Kind {
- /// Dash (rank 2) heading.
- ///
- /// ## Example
- ///
- /// ```markdown
- /// alpha
- /// -----
- /// ```
- Dash,
-
- /// Equals to (rank 1) heading.
- ///
- /// ## Example
- ///
- /// ```markdown
- /// alpha
- /// =====
- /// ```
- EqualsTo,
-}
-
-impl Kind {
- /// Turn the kind into a byte ([u8]).
- fn as_byte(&self) -> u8 {
- match self {
- Kind::Dash => b'-',
- Kind::EqualsTo => b'=',
- }
- }
- /// Turn a byte ([u8]) into a kind.
- ///
- /// ## Panics
- ///
- /// Panics if `byte` is not `-` or `=`.
- fn from_byte(byte: u8) -> Kind {
- match byte {
- b'-' => Kind::Dash,
- b'=' => Kind::EqualsTo,
- _ => unreachable!("invalid byte"),
- }
- }
-}
-
/// At a line ending, presumably an underline.
///
/// ```markdown
@@ -117,23 +71,29 @@ impl Kind {
/// ^
/// ```
pub fn start(tokenizer: &mut Tokenizer) -> State {
- let max = if tokenizer.parse_state.constructs.code_indented {
- TAB_SIZE - 1
- } else {
- usize::MAX
- };
- let paragraph_before = !tokenizer.events.is_empty()
- && tokenizer.events[skip_opt_back(
- &tokenizer.events,
- tokenizer.events.len() - 1,
- &[Token::LineEnding, Token::SpaceOrTab],
- )]
- .token_type
- == Token::Paragraph;
-
- // Require a paragraph before and do not allow on a lazy line.
- if paragraph_before && !tokenizer.lazy && tokenizer.parse_state.constructs.heading_setext {
- tokenizer.go(space_or_tab_min_max(0, max), before)(tokenizer)
+ if tokenizer.parse_state.constructs.heading_setext
+ && !tokenizer.lazy
+ // Require a paragraph before.
+ && (!tokenizer.events.is_empty()
+ && tokenizer.events[skip_opt_back(
+ &tokenizer.events,
+ tokenizer.events.len() - 1,
+ &[Token::LineEnding, Token::SpaceOrTab],
+ )]
+ .token_type
+ == Token::Paragraph)
+ {
+ tokenizer.go(
+ space_or_tab_min_max(
+ 0,
+ if tokenizer.parse_state.constructs.code_indented {
+ TAB_SIZE - 1
+ } else {
+ usize::MAX
+ },
+ ),
+ before,
+ )(tokenizer)
} else {
State::Nok
}
@@ -148,9 +108,9 @@ pub fn start(tokenizer: &mut Tokenizer) -> State {
/// ```
fn before(tokenizer: &mut Tokenizer) -> State {
match tokenizer.current {
- Some(byte) if matches!(byte, b'-' | b'=') => {
+ Some(b'-' | b'=') => {
tokenizer.enter(Token::HeadingSetextUnderline);
- inside(tokenizer, Kind::from_byte(byte))
+ inside(tokenizer, tokenizer.current.unwrap())
}
_ => State::Nok,
}
@@ -163,11 +123,11 @@ fn before(tokenizer: &mut Tokenizer) -> State {
/// > | ==
/// ^
/// ```
-fn inside(tokenizer: &mut Tokenizer, kind: Kind) -> State {
+fn inside(tokenizer: &mut Tokenizer, marker: u8) -> State {
match tokenizer.current {
- Some(byte) if byte == kind.as_byte() => {
+ Some(b'-' | b'=') if tokenizer.current.unwrap() == marker => {
tokenizer.consume();
- State::Fn(Box::new(move |t| inside(t, kind)))
+ State::Fn(Box::new(move |t| inside(t, marker)))
}
_ => {
tokenizer.exit(Token::HeadingSetextUnderline);