Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/codecov/codecov-ac…
Browse files Browse the repository at this point in the history
…tion-4
  • Loading branch information
Lurk authored Oct 14, 2023
2 parents 28d5b0f + 118828b commit 918fb9d
Show file tree
Hide file tree
Showing 25 changed files with 439 additions and 258 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ keywords = ["markdown", "parser"]
pretty_assertions = "1.4.0"

[dependencies]
chrono = "0.4.26"
serde = { version = "1.0.188", features = ["derive"] }
chrono = { version = "0.4.31", features = ["serde"] }
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
//! ```
use nodes::yamd::Yamd;
use toolkit::{deserializer::Deserializer, node::Node};
use toolkit::deserializer::Deserializer;

pub mod nodes;
mod toolkit;
Expand All @@ -219,7 +219,7 @@ pub fn deserialize(input: &str) -> Option<Yamd> {
/// let output = serialize(&yamd);
/// ```
pub fn serialize(input: &Yamd) -> String {
input.serialize()
input.to_string()
}

#[cfg(test)]
Expand Down
29 changes: 20 additions & 9 deletions src/nodes/accordion.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::fmt::Display;

use serde::Serialize;

use crate::toolkit::{
context::Context,
deserializer::{Branch, DefinitelyNode, Deserializer, MaybeNode},
Expand All @@ -7,18 +11,21 @@ use crate::toolkit::{

use super::accordion_tab::AccordionTab;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum AccordionNodes {
AccordionTab(AccordionTab),
}

impl Node for AccordionNodes {
fn serialize(&self) -> String {
impl Display for AccordionNodes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AccordionNodes::AccordionTab(tab) => tab.serialize(),
AccordionNodes::AccordionTab(tab) => write!(f, "{}", tab),
}
}
}

impl Node for AccordionNodes {
fn len(&self) -> usize {
match self {
AccordionNodes::AccordionTab(tab) => tab.len(),
Expand All @@ -32,8 +39,9 @@ impl From<AccordionTab> for AccordionNodes {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
pub struct Accordion {
#[serde(skip_serializing)]
consumed_all_input: bool,
pub nodes: Vec<AccordionNodes>,
}
Expand All @@ -51,20 +59,23 @@ impl Accordion {
}
}

impl Node for Accordion {
fn serialize(&self) -> String {
format!(
impl Display for Accordion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"///\n{nodes}\n\\\\\\{end}",
nodes = self
.nodes
.iter()
.map(|n| n.serialize())
.map(|n| n.to_string())
.collect::<Vec<String>>()
.join(""),
end = if self.consumed_all_input { "" } else { "\n\n" }
)
}
}

impl Node for Accordion {
fn len(&self) -> usize {
self.nodes.iter().map(|n| n.len()).sum::<usize>() + self.get_outer_token_length()
}
Expand Down
51 changes: 31 additions & 20 deletions src/nodes/accordion_tab.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::fmt::Display;

use serde::Serialize;

use crate::toolkit::{
context::Context,
deserializer::{Branch, DefinitelyNode, Deserializer, FallbackNode, MaybeNode},
Expand All @@ -11,7 +15,8 @@ use super::{
list::List, paragraph::Paragraph,
};

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum AccordionTabNodes {
Pargaraph(Paragraph),
Heading(Heading),
Expand All @@ -25,22 +30,24 @@ pub enum AccordionTabNodes {
Code(Code),
}

impl Node for AccordionTabNodes {
fn serialize(&self) -> String {
impl Display for AccordionTabNodes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AccordionTabNodes::Pargaraph(node) => node.serialize(),
AccordionTabNodes::Heading(node) => node.serialize(),
AccordionTabNodes::Image(node) => node.serialize(),
AccordionTabNodes::ImageGallery(node) => node.serialize(),
AccordionTabNodes::CloudinaryImageGallery(node) => node.serialize(),
AccordionTabNodes::List(node) => node.serialize(),
AccordionTabNodes::Embed(node) => node.serialize(),
AccordionTabNodes::Accordion(node) => node.serialize(),
AccordionTabNodes::Divider(node) => node.serialize(),
AccordionTabNodes::Code(node) => node.serialize(),
AccordionTabNodes::Pargaraph(node) => write!(f, "{}", node),
AccordionTabNodes::Heading(node) => write!(f, "{}", node),
AccordionTabNodes::Image(node) => write!(f, "{}", node),
AccordionTabNodes::ImageGallery(node) => write!(f, "{}", node),
AccordionTabNodes::CloudinaryImageGallery(node) => write!(f, "{}", node),
AccordionTabNodes::List(node) => write!(f, "{}", node),
AccordionTabNodes::Embed(node) => write!(f, "{}", node),
AccordionTabNodes::Accordion(node) => write!(f, "{}", node),
AccordionTabNodes::Divider(node) => write!(f, "{}", node),
AccordionTabNodes::Code(node) => write!(f, "{}", node),
}
}
}

impl Node for AccordionTabNodes {
fn len(&self) -> usize {
match self {
AccordionTabNodes::Pargaraph(node) => node.len(),
Expand Down Expand Up @@ -117,10 +124,11 @@ impl From<Code> for AccordionTabNodes {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
pub struct AccordionTab {
pub header: Option<String>,
pub nodes: Vec<AccordionTabNodes>,
#[serde(skip_serializing)]
consumed_all_input: bool,
}

Expand All @@ -141,9 +149,10 @@ impl AccordionTab {
}
}

impl Node for AccordionTab {
fn serialize(&self) -> String {
format!(
impl Display for AccordionTab {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"//\n{header}{nodes}\n\\\\{end}",
header = self
.header
Expand All @@ -152,13 +161,15 @@ impl Node for AccordionTab {
nodes = self
.nodes
.iter()
.map(|node| node.serialize())
.map(|node| node.to_string())
.collect::<Vec<String>>()
.join(""),
end = if self.consumed_all_input { "" } else { "\n" }
)
}
}

impl Node for AccordionTab {
fn len(&self) -> usize {
self.nodes.iter().map(|node| node.len()).sum::<usize>() + self.get_outer_token_length()
}
Expand Down Expand Up @@ -298,7 +309,7 @@ mod cfg {
Some("Header"),
vec![Heading::new(true, "Heading", 1).into()]
)
.serialize(),
.to_string(),
"//\n/ Header\n# Heading\n\\\\\n"
);
}
Expand Down Expand Up @@ -393,7 +404,7 @@ t**b**
CloudinaryImageGallery::new("username", "tag", true).into(),
],
);
assert_eq!(tab.serialize(), input);
assert_eq!(tab.to_string(), input);
assert_eq!(AccordionTab::deserialize(input), Some(tab));
}
}
17 changes: 12 additions & 5 deletions src/nodes/anchor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::fmt::{Display, Formatter};

use serde::Serialize;

use crate::{
toolkit::context::Context,
toolkit::deserializer::Deserializer,
toolkit::{matcher::Matcher, node::Node},
};

/// Representation of an anchor
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
pub struct Anchor {
pub text: String,
pub url: String,
Expand All @@ -20,10 +24,13 @@ impl Anchor {
}
}

impl Node for Anchor {
fn serialize(&self) -> String {
format!("[{}]({})", self.text, self.url)
impl Display for Anchor {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}]({})", self.text, self.url)
}
}

impl Node for Anchor {
fn len(&self) -> usize {
self.text.len() + self.url.len() + 4
}
Expand Down Expand Up @@ -56,7 +63,7 @@ mod tests {

#[test]
fn serialize() {
let a: String = Anchor::new("nice link", "https://test.io").serialize();
let a: String = Anchor::new("nice link", "https://test.io").to_string();
assert_eq!(a, "[nice link](https://test.io)".to_string());
}

Expand Down
37 changes: 24 additions & 13 deletions src/nodes/bold.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::fmt::Display;

use serde::Serialize;

use crate::{
nodes::italic::Italic,
nodes::strikethrough::Strikethrough,
Expand All @@ -10,7 +14,8 @@ use crate::{
},
};

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
#[serde(tag = "type")]
pub enum BoldNodes {
Text(Text),
I(Italic),
Expand All @@ -35,15 +40,17 @@ impl From<Strikethrough> for BoldNodes {
}
}

impl Node for BoldNodes {
fn serialize(&self) -> String {
impl Display for BoldNodes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BoldNodes::Text(v) => v.serialize(),
BoldNodes::I(v) => v.serialize(),
BoldNodes::S(v) => v.serialize(),
BoldNodes::Text(node) => write!(f, "{}", node),
BoldNodes::I(node) => write!(f, "{}", node),
BoldNodes::S(node) => write!(f, "{}", node),
}
}
}

impl Node for BoldNodes {
fn len(&self) -> usize {
match self {
BoldNodes::Text(node) => node.len(),
Expand All @@ -53,7 +60,7 @@ impl Node for BoldNodes {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
pub struct Bold {
pub nodes: Vec<BoldNodes>,
}
Expand Down Expand Up @@ -91,17 +98,21 @@ impl Default for Bold {
}
}

impl Node for Bold {
fn serialize(&self) -> String {
format!(
impl Display for Bold {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"**{}**",
self.nodes
.iter()
.map(|element| { element.serialize() })
.map(|element| { element.to_string() })
.collect::<Vec<String>>()
.concat()
)
}
}

impl Node for Bold {
fn len(&self) -> usize {
self.nodes.iter().map(|node| node.len()).sum::<usize>() + self.get_outer_token_length()
}
Expand Down Expand Up @@ -135,7 +146,7 @@ mod tests {
fn only_text() {
let mut b = Bold::new();
b.push(Text::new("B as bold"));
let str = b.serialize();
let str = b.to_string();
assert_eq!(str, "**B as bold**".to_string());
}

Expand All @@ -146,7 +157,7 @@ mod tests {
Italic::new("Italic").into(),
Strikethrough::new("Strikethrough").into(),
])
.serialize();
.to_string();
assert_eq!(b, "**B as bold _Italic_~~Strikethrough~~**".to_string());
}

Expand Down
23 changes: 13 additions & 10 deletions src/nodes/cloudinary_image_gallery.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::fmt::{Display, Formatter};

use serde::Serialize;

use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matcher, node::Node};

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Serialize)]
pub struct CloudinaryImageGallery {
username: String,
pub tag: String,
#[serde(skip_serializing)]
pub consumed_all_input: bool,
}

Expand All @@ -17,16 +22,14 @@ impl CloudinaryImageGallery {
}
}

impl Node for CloudinaryImageGallery {
fn serialize(&self) -> String {
format!(
"!!!!\n! {username}\n! {tag}\n!!!!{end}",
username = self.username,
tag = self.tag,
end = if self.consumed_all_input { "" } else { "\n\n" }
)
impl Display for CloudinaryImageGallery {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let end = if self.consumed_all_input { "" } else { "\n\n" };
write!(f, "!!!!\n! {}\n! {}\n!!!!{}", self.username, self.tag, end)
}
}

impl Node for CloudinaryImageGallery {
fn len(&self) -> usize {
self.username.len() + self.tag.len() + 15 + if self.consumed_all_input { 0 } else { 2 }
}
Expand Down Expand Up @@ -63,7 +66,7 @@ mod test {
CloudinaryImageGallery::deserialize(input),
Some(expected.clone()),
);
assert_eq!(expected.serialize(), input);
assert_eq!(expected.to_string(), input);
}

#[test]
Expand Down
Loading

0 comments on commit 918fb9d

Please sign in to comment.