Skip to content

Commit

Permalink
Simplify list parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Lurk committed Sep 25, 2024
1 parent f48d099 commit 5277e08
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 149 deletions.
12 changes: 11 additions & 1 deletion src/nodes/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ pub enum ListTypes {
Ordered,
}

impl Display for ListTypes {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ListTypes::Unordered => f.write_str("-"),
ListTypes::Ordered => f.write_str("+"),

Check warning on line 21 in src/nodes/list.rs

View check run for this annotation

Codecov / codecov/patch

src/nodes/list.rs#L18-L21

Added lines #L18 - L21 were not covered by tests
}
}

Check warning on line 23 in src/nodes/list.rs

View check run for this annotation

Codecov / codecov/patch

src/nodes/list.rs#L23

Added line #L23 was not covered by tests
}

/// # List
///
/// ## Types
Expand Down Expand Up @@ -140,8 +149,9 @@ impl List {

impl Display for List {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let level = String::from(" ").repeat(self.level);
for n in self.body.iter() {
f.write_str(n.to_string().as_str())?;
f.write_str(format!("{}{} {}", level, self.list_type, n.to_string()).as_str())?;

Check warning on line 154 in src/nodes/list.rs

View workflow job for this annotation

GitHub Actions / clippy

`to_string` applied to a type that implements `Display` in `format!` args

warning: `to_string` applied to a type that implements `Display` in `format!` args --> src/nodes/list.rs:154:68 | 154 | f.write_str(format!("{}{} {}", level, self.list_type, n.to_string()).as_str())?; | ^^^^^^^^^^^^ help: remove this | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#to_string_in_format_args = note: `#[warn(clippy::to_string_in_format_args)]` on by default

Check warning on line 154 in src/nodes/list.rs

View check run for this annotation

Codecov / codecov/patch

src/nodes/list.rs#L151-L154

Added lines #L151 - L154 were not covered by tests
}
Ok(())

Check warning on line 156 in src/nodes/list.rs

View check run for this annotation

Codecov / codecov/patch

src/nodes/list.rs#L156

Added line #L156 was not covered by tests
}
Expand Down
26 changes: 4 additions & 22 deletions src/nodes/list_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,25 @@ use std::fmt::Display;

use serde::Serialize;

use super::{List, ListTypes, ParagraphNodes};
use super::{List, ParagraphNodes};

#[derive(Debug, PartialEq, Serialize, Clone, Eq)]
pub struct ListItem {
pub list_type: ListTypes,
pub level: usize,
pub text: Vec<ParagraphNodes>,
pub nested_list: Option<List>,
}

impl ListItem {
pub fn new(
list_type: ListTypes,
level: usize,
text: Vec<ParagraphNodes>,
nested_list: Option<List>,
) -> Self {
Self {
list_type,
level,
text,
nested_list,
}
pub fn new(text: Vec<ParagraphNodes>, nested_list: Option<List>) -> Self {
Self { text, nested_list }
}
}

impl Display for ListItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let list_type = match self.list_type {
ListTypes::Unordered => '-',
ListTypes::Ordered => '+',
};
write!(
f,
"{}{} {}{}",
String::from(' ').repeat(self.level),
list_type,
"{}{}",
self.text.iter().map(|n| n.to_string()).collect::<String>(),

Check warning on line 24 in src/nodes/list_item.rs

View check run for this annotation

Codecov / codecov/patch

src/nodes/list_item.rs#L23-L24

Added lines #L23 - L24 were not covered by tests
self.nested_list
.as_ref()
Expand Down
Loading

0 comments on commit 5277e08

Please sign in to comment.