Skip to content

Commit

Permalink
minor code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Canop committed Aug 4, 2021
1 parent 8a43754 commit 2837d11
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 36 deletions.
15 changes: 3 additions & 12 deletions src/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,15 @@ impl<'a> Composite<'a> {
}
#[inline(always)]
pub fn is_code(&self) -> bool {
match self.style {
CompositeStyle::Code { .. } => true,
_ => false,
}
matches!(self.style, CompositeStyle::Code { .. })
}
#[inline(always)]
pub fn is_list_item(&self) -> bool {
match self.style {
CompositeStyle::ListItem { .. } => true,
_ => false,
}
matches!(self.style, CompositeStyle::ListItem { .. })
}
#[inline(always)]
pub fn is_quote(&self) -> bool {
match self.style {
CompositeStyle::Quote { .. } => true,
_ => false,
}
matches!(self.style, CompositeStyle::Quote { .. })
}
/// return the total number of characters in the composite
///
Expand Down
6 changes: 2 additions & 4 deletions src/inline_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,8 @@ impl<'a> InlineTemplate<'a> {
compounds.push(compound.sub(idx - 1, start)); // placeholder
}
after_dollar = false;
} else {
if char == '$' {
after_dollar = true;
}
} else if char == '$' {
after_dollar = true;
}
}
let tail = compound.tail(start);
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ pub use {
pub use once_cell;

/// parse a markdown text
pub fn parse_text<'s>(md: &'s str) -> Text<'s> {
pub fn parse_text(md: &str) -> Text {
Text::from(md)
}

/// parse a line, which is meant to be part of a markdown text.
/// This function shouldn't usually be used: if you don't want
/// a text you probably need `parse_inline`
pub fn parse_line<'s>(md: &'s str) -> Line<'s> {
pub fn parse_line(md: &str) -> Line {
Line::from(md)
}

/// parse a monoline markdown snippet which isn't from a text.
/// Don't produce some types of line: TableRow, Code, ListItem
/// as they only make sense in a multi-line text.
pub fn parse_inline<'s>(md: &'s str) -> Composite<'s> {
pub fn parse_inline(md: &str) -> Composite {
Composite::from_inline(md)
}
5 changes: 1 addition & 4 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ impl Line<'_> {
}
#[inline(always)]
pub fn is_table_row(&self) -> bool {
match self {
Line::TableRow(_) => true,
_ => false,
}
matches!(self, Line::TableRow(_))
}
#[inline(always)]
pub fn is_table_part(&self) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions src/line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'s> LineParser<'s> {
fn close_compound(&mut self, end: usize, tag_length: usize, compounds: &mut Vec<Compound<'s>>) {
if end > self.idx {
compounds.push(Compound::new(
&self.src,
self.src,
self.idx,
end,
self.bold,
Expand All @@ -48,7 +48,7 @@ impl<'s> LineParser<'s> {
self.idx = end + tag_length;
}
fn code_compound_from_idx(&self, idx: usize) -> Compound<'s> {
Compound::new(&self.src, idx, self.src.len(), false, false, true, false)
Compound::new(self.src, idx, self.src.len(), false, false, true, false)
}
fn parse_compounds(&mut self, stop_on_pipe: bool) -> Vec<Compound<'s>> {
let mut compounds = Vec::new();
Expand Down
12 changes: 6 additions & 6 deletions src/owning_template_expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,26 @@ impl<'s> OwningTemplateExpander<'s> {
for op in &self.ops {
match op {
FillingOperation::Set { name, value } => {
expander.set(name, &value);
expander.set(name, value);
}
FillingOperation::SetMD { name, value } => {
expander.set_md(name, &value);
expander.set_md(name, value);
}
FillingOperation::SetLines { name, value } => {
expander.set_lines(name, &value);
expander.set_lines(name, value);
}
FillingOperation::SetLinesMD { name, value } => {
expander.set_lines_md(name, &value);
expander.set_lines_md(name, value);
}
FillingOperation::Sub { name, sub_expander } => {
let sub = expander.sub(name);
for op in &sub_expander.ops {
match op {
SubFillingOperation::Set { name, value } => {
sub.set(name, &value);
sub.set(name, value);
}
SubFillingOperation::SetMD { name, value } => {
sub.set_md(name, &value);
sub.set_md(name, value);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/text_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn find_args<'s>(
if c == '$' {
if let Some((bridx, c)) = iter.next() {
if c == '{' {
while let Some((idx, c)) = iter.next() {
for (idx, c) in &mut iter {
if c == '}' {
if idx - bridx > 1 {
if start + 1 < bridx {
Expand Down Expand Up @@ -297,7 +297,7 @@ fn set_all_md_in_text<'s, 'b>(
template: &TextTemplate<'s>,
text: &mut Text<'s>,
line_offset: usize,
md_replacements: &Vec<Replacement<'s, 'b>>,
md_replacements: &[Replacement<'s, 'b>],
) {
if md_replacements.is_empty() {
return; // no need to iterate over all compound_args
Expand Down Expand Up @@ -356,15 +356,15 @@ impl<'s, 'b> TextTemplateExpander<'s, 'b> {
/// replace placeholders with name `name` with the given value, non interpreted
/// (i.e. stars, backquotes, etc. don't mess the styling defined by the template)
pub fn set(&mut self, name: &str, value: &'s str) -> &mut TextTemplateExpander<'s, 'b> {
set_in_text(&self.template, &mut self.text, 0, Some(name), value);
set_in_text(self.template, &mut self.text, 0, Some(name), value);
self
}

/// replace all placeholders with the given value, non interpreted
/// (i.e. stars, backquotes, etc. don't mess the styling defined by the template).
/// This can be used at start to have a "default" value.
pub fn set_all(&mut self, value: &'s str) -> &mut TextTemplateExpander<'s, 'b> {
set_in_text(&self.template, &mut self.text, 0, None, value);
set_in_text(self.template, &mut self.text, 0, None, value);
self
}

Expand Down Expand Up @@ -488,7 +488,7 @@ impl<'s, 'b> TextTemplateExpander<'s, 'b> {
}
for repl in &sub_expansion.raw_replacements {
set_in_text(
&self.template,
self.template,
&mut sub_text,
sub_template.start_line_idx,
Some(repl.name),
Expand Down

0 comments on commit 2837d11

Please sign in to comment.