inline: use ? to flatten methods

This commit is contained in:
Noah Hellman 2023-02-15 17:07:53 +01:00
parent 491c5f2866
commit f192ea2aa6

View file

@ -147,7 +147,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
} }
fn parse_verbatim(&mut self, first: &lex::Token) -> Option<()> { fn parse_verbatim(&mut self, first: &lex::Token) -> Option<()> {
match first.kind { let (mut kind, opener_len) = match first.kind {
lex::Kind::Seq(lex::Sequence::Dollar) => { lex::Kind::Seq(lex::Sequence::Dollar) => {
let math_opt = (first.len <= 2) let math_opt = (first.len <= 2)
.then(|| { .then(|| {
@ -176,8 +176,8 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
} }
lex::Kind::Seq(lex::Sequence::Backtick) => Some((Verbatim, first.len)), lex::Kind::Seq(lex::Sequence::Backtick) => Some((Verbatim, first.len)),
_ => None, _ => None,
} }?;
.map(|(mut kind, opener_len)| {
let e_attr = self.events.len(); let e_attr = self.events.len();
self.push_sp(EventKind::Placeholder, Span::empty_at(self.span.start())); self.push_sp(EventKind::Placeholder, Span::empty_at(self.span.start()));
let opener_event = self.events.len(); let opener_event = self.events.len();
@ -190,8 +190,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
let mut non_whitespace_last = None; let mut non_whitespace_last = None;
while let Some(t) = self.eat() { while let Some(t) = self.eat() {
if matches!(t.kind, lex::Kind::Seq(lex::Sequence::Backtick)) && t.len == opener_len if matches!(t.kind, lex::Kind::Seq(lex::Sequence::Backtick)) && t.len == opener_len {
{
if matches!(kind, Verbatim) if matches!(kind, Verbatim)
&& matches!( && matches!(
self.lexer.peek().map(|t| &t.kind), self.lexer.peek().map(|t| &t.kind),
@ -262,7 +261,8 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
}; };
} }
} }
})
Some(())
} }
fn parse_attributes(&mut self, first: &lex::Token) -> Option<()> { fn parse_attributes(&mut self, first: &lex::Token) -> Option<()> {
@ -414,7 +414,8 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
} }
fn parse_container(&mut self, first: &lex::Token) -> Option<()> { fn parse_container(&mut self, first: &lex::Token) -> Option<()> {
Delim::from_token(first.kind).and_then(|(delim, dir)| { let (delim, dir) = Delim::from_token(first.kind)?;
self.openers self.openers
.iter() .iter()
.rposition(|(d, _)| { .rposition(|(d, _)| {
@ -459,9 +460,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
if event_closer.span.is_empty() if event_closer.span.is_empty()
&& matches!( && matches!(
event_closer.kind, event_closer.kind,
EventKind::Exit( EventKind::Exit(Container::ReferenceLink | Container::ReferenceImage)
Container::ReferenceLink | Container::ReferenceImage
)
) )
{ {
event_closer.span = inner_span; event_closer.span = inner_span;
@ -522,7 +521,6 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
_ => EventKind::Str, _ => EventKind::Str,
}) })
}) })
})
} }
fn ahead_attributes(&mut self) -> Option<(bool, Span)> { fn ahead_attributes(&mut self) -> Option<(bool, Span)> {
@ -546,7 +544,7 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
fn post_span(&mut self, ty: SpanType, opener_event: usize) -> Option<()> { fn post_span(&mut self, ty: SpanType, opener_event: usize) -> Option<()> {
let mut ahead = self.lexer.chars(); let mut ahead = self.lexer.chars();
match ahead.next() { let (kind, span) = match ahead.next() {
Some(opener @ ('[' | '(')) => { Some(opener @ ('[' | '(')) => {
let img = ty == SpanType::Image; let img = ty == SpanType::Image;
let (closer, kind) = match opener { let (closer, kind) = match opener {
@ -573,14 +571,13 @@ impl<I: Iterator<Item = char> + Clone> Parser<I> {
}) })
} }
_ => None, _ => None,
} }?;
.map(|(kind, span)| {
self.lexer = lex::Lexer::new(ahead); self.lexer = lex::Lexer::new(ahead);
self.events[opener_event].kind = EventKind::Enter(kind); self.events[opener_event].kind = EventKind::Enter(kind);
self.events[opener_event].span = span; self.events[opener_event].span = span;
self.span = span.translate(1); self.span = span.translate(1);
self.push_sp(EventKind::Exit(kind), span); self.push_sp(EventKind::Exit(kind), span)
})
} }
fn parse_atom(&mut self, first: &lex::Token) -> Option<()> { fn parse_atom(&mut self, first: &lex::Token) -> Option<()> {