diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-10-13 18:14:23 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-10-13 18:14:23 +0200 |
commit | 2f21280db96e9c8086e123f756f5cad27cbfa0bf (patch) | |
tree | 6d3a9f55a0d441984393d612b4bd5f43ffcfbc76 /src/to_html.rs | |
parent | 5c7605629d70cc613cb3bee6ac419e7ee4d9cf7f (diff) | |
download | markdown-rs-2f21280db96e9c8086e123f756f5cad27cbfa0bf.tar.gz markdown-rs-2f21280db96e9c8086e123f756f5cad27cbfa0bf.tar.bz2 markdown-rs-2f21280db96e9c8086e123f756f5cad27cbfa0bf.zip |
Refactor some code to improve coverage
Diffstat (limited to 'src/to_html.rs')
-rw-r--r-- | src/to_html.rs | 42 |
1 files changed, 18 insertions, 24 deletions
diff --git a/src/to_html.rs b/src/to_html.rs index b4264c3..ab55376 100644 --- a/src/to_html.rs +++ b/src/to_html.rs @@ -180,10 +180,9 @@ impl<'a> CompileContext<'a> { /// Push a str to the last buffer. fn push(&mut self, value: &str) { - self.buffers - .last_mut() - .expect("Cannot push w/o buffer") - .push_str(value); + let last_buf_opt = self.buffers.last_mut(); + let last_buf = last_buf_opt.expect("at least one buffer should exist"); + last_buf.push_str(value); } /// Add a line ending. @@ -194,14 +193,11 @@ impl<'a> CompileContext<'a> { /// Add a line ending if needed (as in, there’s no eol/eof already). fn line_ending_if_needed(&mut self) { - let tail = self - .buffers - .last() - .expect("at least one buffer should exist") - .as_bytes() - .last(); - - if !matches!(tail, None | Some(b'\n' | b'\r')) { + let last_buf_opt = self.buffers.last(); + let last_buf = last_buf_opt.expect("at least one buffer should exist"); + let last_byte = last_buf.as_bytes().last(); + + if !matches!(last_byte, None | Some(b'\n' | b'\r')) { self.line_ending(); } } @@ -805,11 +801,9 @@ fn on_exit_raw_flow_chunk(context: &mut CompileContext) { /// Handle [`Exit`][Kind::Exit]:{[`CodeFencedFence`][Name::CodeFencedFence],[`MathFlowFence`][Name::MathFlowFence]}. fn on_exit_raw_flow_fence(context: &mut CompileContext) { - let count = if let Some(count) = context.raw_flow_fences_count { - count - } else { - 0 - }; + let count = context + .raw_flow_fences_count + .expect("expected `raw_flow_fences_count`"); if count == 0 { context.push(">"); @@ -1432,18 +1426,18 @@ fn on_exit_media(context: &mut CompileContext) { }); let definition_index = if media.destination.is_none() { - id.and_then(|id| { + id.map(|id| { let mut index = 0; - while index < context.definitions.len() { - if context.definitions[index].id == id { - return Some(index); - } - + while index < context.definitions.len() && context.definitions[index].id != id { index += 1; } - None + debug_assert!( + index < context.definitions.len(), + "expected defined definition" + ); + index }) } else { None |