Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

derive serde::Serialize, implement Display for Yamd #27

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 18 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,20 @@ use crate::toolkit::{

use super::accordion_tab::AccordionTab;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
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,7 +38,7 @@ impl From<AccordionTab> for AccordionNodes {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
pub struct Accordion {
consumed_all_input: bool,
pub nodes: Vec<AccordionNodes>,
Expand All @@ -51,20 +57,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
49 changes: 29 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,7 @@
list::List, paragraph::Paragraph,
};

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
pub enum AccordionTabNodes {
Pargaraph(Paragraph),
Heading(Heading),
Expand All @@ -25,22 +29,24 @@
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),

Check warning on line 42 in src/nodes/accordion_tab.rs

View check run for this annotation

Codecov / codecov/patch

src/nodes/accordion_tab.rs#L42

Added line #L42 was not covered by tests
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,7 +123,7 @@
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
pub struct AccordionTab {
pub header: Option<String>,
pub nodes: Vec<AccordionTabNodes>,
Expand All @@ -141,9 +147,10 @@
}
}

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 +159,15 @@
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 +307,7 @@
Some("Header"),
vec![Heading::new(true, "Heading", 1).into()]
)
.serialize(),
.to_string(),
"//\n/ Header\n# Heading\n\\\\\n"
);
}
Expand Down Expand Up @@ -393,7 +402,7 @@
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
36 changes: 23 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,7 @@ use crate::{
},
};

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize)]
pub enum BoldNodes {
Text(Text),
I(Italic),
Expand All @@ -35,15 +39,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 +59,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 +97,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 +145,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 +156,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
22 changes: 12 additions & 10 deletions src/nodes/cloudinary_image_gallery.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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,
Expand All @@ -17,16 +21,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 +65,7 @@ mod test {
CloudinaryImageGallery::deserialize(input),
Some(expected.clone()),
);
assert_eq!(expected.serialize(), input);
assert_eq!(expected.to_string(), input);
}

#[test]
Expand Down
Loading
Loading