From 87ca5b0819a3077a3f37d6eeceff93b50711cdf0 Mon Sep 17 00:00:00 2001 From: Serhiy Barhamon Date: Sat, 21 Oct 2023 22:04:09 +0300 Subject: [PATCH] cloudinary image galery refactoring (#33) * Cloudinary image gallery refactoring --- Cargo.toml | 2 +- src/nodes/accordion.rs | 4 ++-- src/nodes/accordion_tab.rs | 6 +++--- src/nodes/anchor.rs | 2 +- src/nodes/bold.rs | 10 ++-------- src/nodes/cloudinary_image_gallery.rs | 12 ++++++++---- src/nodes/code.rs | 2 +- src/nodes/divider.rs | 2 +- src/nodes/embed.rs | 2 +- src/nodes/heading.rs | 2 +- src/nodes/highlight.rs | 28 +++++++++++++-------------- src/nodes/image.rs | 2 +- src/nodes/image_gallery.rs | 20 +++++++++---------- src/nodes/inline_code.rs | 2 +- src/nodes/italic.rs | 2 +- src/nodes/list.rs | 4 ++-- src/nodes/list_item.rs | 2 +- src/nodes/list_item_content.rs | 4 ++-- src/nodes/metadata.rs | 2 +- src/nodes/paragraph.rs | 4 ++-- src/nodes/strikethrough.rs | 2 +- src/nodes/text.rs | 2 +- src/nodes/yamd.rs | 18 ++++++----------- src/toolkit/context.rs | 8 +------- 24 files changed, 65 insertions(+), 79 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4f087a0..6bfe324 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "yamd" -version = "0.8.0" +version = "0.9.0" edition = "2021" license = "MIT OR Apache-2.0" description = "Yet Another Markdown Document (flavor)" diff --git a/src/nodes/accordion.rs b/src/nodes/accordion.rs index 09d760e..72dba38 100644 --- a/src/nodes/accordion.rs +++ b/src/nodes/accordion.rs @@ -11,7 +11,7 @@ use crate::toolkit::{ use super::accordion_tab::AccordionTab; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] #[serde(tag = "type")] pub enum AccordionNodes { AccordionTab(AccordionTab), @@ -39,7 +39,7 @@ impl From for AccordionNodes { } } -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Accordion { #[serde(skip_serializing)] consumed_all_input: bool, diff --git a/src/nodes/accordion_tab.rs b/src/nodes/accordion_tab.rs index 9701920..60721bf 100644 --- a/src/nodes/accordion_tab.rs +++ b/src/nodes/accordion_tab.rs @@ -15,7 +15,7 @@ use super::{ list::List, paragraph::Paragraph, }; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] #[serde(tag = "type")] pub enum AccordionTabNodes { Pargaraph(Paragraph), @@ -124,7 +124,7 @@ impl From for AccordionTabNodes { } } -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct AccordionTab { pub header: Option, pub nodes: Vec, @@ -366,11 +366,11 @@ t**b** .into(), Image::new(false, 'a', 'u').into(), ImageGallery::new_with_nodes( + false, vec![ Image::new(true, "a", "u").into(), Image::new(true, "a2", "u2").into(), ], - false, ) .into(), Divider::new(false).into(), diff --git a/src/nodes/anchor.rs b/src/nodes/anchor.rs index 94b1c84..29fc3f2 100644 --- a/src/nodes/anchor.rs +++ b/src/nodes/anchor.rs @@ -9,7 +9,7 @@ use crate::{ }; /// Representation of an anchor -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Anchor { pub text: String, pub url: String, diff --git a/src/nodes/bold.rs b/src/nodes/bold.rs index f3f33f1..446e09c 100644 --- a/src/nodes/bold.rs +++ b/src/nodes/bold.rs @@ -14,7 +14,7 @@ use crate::{ }, }; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] #[serde(tag = "type")] pub enum BoldNodes { Text(Text), @@ -60,7 +60,7 @@ impl Node for BoldNodes { } } -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone, Default)] pub struct Bold { pub nodes: Vec, } @@ -92,12 +92,6 @@ impl Branch for Bold { } } -impl Default for Bold { - fn default() -> Self { - Self::new() - } -} - impl Display for Bold { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( diff --git a/src/nodes/cloudinary_image_gallery.rs b/src/nodes/cloudinary_image_gallery.rs index bb000ee..8c750e8 100644 --- a/src/nodes/cloudinary_image_gallery.rs +++ b/src/nodes/cloudinary_image_gallery.rs @@ -6,7 +6,7 @@ use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matc #[derive(Debug, PartialEq, Clone, Serialize)] pub struct CloudinaryImageGallery { - username: String, + pub cloud_name: String, pub tag: String, #[serde(skip_serializing)] pub consumed_all_input: bool, @@ -15,7 +15,7 @@ pub struct CloudinaryImageGallery { impl CloudinaryImageGallery { pub fn new>(username: S, tag: S, consumed_all_input: bool) -> Self { Self { - username: username.into(), + cloud_name: username.into(), tag: tag.into(), consumed_all_input, } @@ -25,13 +25,17 @@ impl CloudinaryImageGallery { 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) + write!( + f, + "!!!!\n! {}\n! {}\n!!!!{}", + self.cloud_name, 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 } + self.cloud_name.len() + self.tag.len() + 15 + if self.consumed_all_input { 0 } else { 2 } } } diff --git a/src/nodes/code.rs b/src/nodes/code.rs index a207b1c..ef9125a 100644 --- a/src/nodes/code.rs +++ b/src/nodes/code.rs @@ -4,7 +4,7 @@ use serde::Serialize; use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matcher, node::Node}; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Code { pub lang: String, pub code: String, diff --git a/src/nodes/divider.rs b/src/nodes/divider.rs index a458948..074d8fe 100644 --- a/src/nodes/divider.rs +++ b/src/nodes/divider.rs @@ -4,7 +4,7 @@ use serde::Serialize; use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matcher, node::Node}; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Divider { #[serde(skip_serializing)] consumed_all_input: bool, diff --git a/src/nodes/embed.rs b/src/nodes/embed.rs index 96449eb..0171787 100644 --- a/src/nodes/embed.rs +++ b/src/nodes/embed.rs @@ -4,7 +4,7 @@ use serde::Serialize; use crate::toolkit::{deserializer::Deserializer, matcher::Matcher, node::Node}; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Embed { pub url: String, pub kind: String, diff --git a/src/nodes/heading.rs b/src/nodes/heading.rs index d00ee62..dd39194 100644 --- a/src/nodes/heading.rs +++ b/src/nodes/heading.rs @@ -4,7 +4,7 @@ use serde::Serialize; use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matcher, node::Node}; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Heading { pub level: u8, pub text: String, diff --git a/src/nodes/highlight.rs b/src/nodes/highlight.rs index d432f97..38a546e 100644 --- a/src/nodes/highlight.rs +++ b/src/nodes/highlight.rs @@ -11,7 +11,7 @@ use crate::toolkit::{ use super::paragraph::Paragraph; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] #[serde(tag = "type")] pub enum HighlightNodes { Paragraph(Paragraph), @@ -39,7 +39,7 @@ impl From for HighlightNodes { } } -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Highlight { pub header: Option, pub icon: Option, @@ -50,17 +50,17 @@ pub struct Highlight { impl Highlight { pub fn new, I: Into>( + consumed_all_input: bool, header: Option, icon: Option, - consumed_all_input: bool, ) -> Self { - Self::new_with_nodes(header, icon, consumed_all_input, vec![]) + Self::new_with_nodes(consumed_all_input, header, icon, vec![]) } pub fn new_with_nodes, I: Into>( + consumed_all_input: bool, header: Option, icon: Option, - consumed_all_input: bool, nodes: Vec, ) -> Self { Self { @@ -146,7 +146,7 @@ impl Deserializer for Highlight { return Self::parse_branch( matcher.get_rest(), - Self::new(header, icon, consumed_all_input), + Self::new(consumed_all_input, header, icon), ); } @@ -166,9 +166,9 @@ mod tests { fn len() { assert_eq!( Highlight::new_with_nodes( + true, Some("h"), Some("i"), - true, vec![ Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(), Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into() @@ -179,9 +179,9 @@ mod tests { ); assert_eq!( Highlight::new_with_nodes( + false, Some("h"), Some("i"), - false, vec![ Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(), Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into() @@ -192,9 +192,9 @@ mod tests { ); assert_eq!( Highlight::new_with_nodes::( + false, None, None, - false, vec![ Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(), Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into() @@ -208,9 +208,9 @@ mod tests { fn serialize() { assert_eq!( Highlight::new_with_nodes( + true, Some("h"), Some("i"), - true, vec![ Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(), Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into() @@ -221,9 +221,9 @@ mod tests { ); assert_eq!( Highlight::new_with_nodes( + false, Some("h"), Some("i"), - false, vec![ Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(), Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into() @@ -234,9 +234,9 @@ mod tests { ); assert_eq!( Highlight::new_with_nodes::( + false, None, None, - false, vec![ Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(), Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into() @@ -252,9 +252,9 @@ mod tests { assert_eq!( Highlight::deserialize(">>>\n>> h\n> i\nt\n\nt\n>>>"), Some(Highlight::new_with_nodes( + true, Some("h"), Some("i"), - true, vec![ Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(), Paragraph::new_with_nodes(true, vec![Text::new("t").into()]).into() @@ -265,9 +265,9 @@ mod tests { assert_eq!( Highlight::deserialize(">>>\n>> h\n> i\nt\n\nt2\n>>>\n\n"), Some(Highlight::new_with_nodes( + false, Some("h"), Some("i"), - false, vec![ Paragraph::new_with_nodes(false, vec![Text::new("t").into()]).into(), Paragraph::new_with_nodes(true, vec![Text::new("t2").into()]).into() diff --git a/src/nodes/image.rs b/src/nodes/image.rs index 2b0bd7d..0bcdd00 100644 --- a/src/nodes/image.rs +++ b/src/nodes/image.rs @@ -4,7 +4,7 @@ use serde::Serialize; use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matcher, node::Node}; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Image { pub alt: String, pub src: String, diff --git a/src/nodes/image_gallery.rs b/src/nodes/image_gallery.rs index adfad36..4e6c24a 100644 --- a/src/nodes/image_gallery.rs +++ b/src/nodes/image_gallery.rs @@ -11,7 +11,7 @@ use crate::toolkit::{ use super::image::Image; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] #[serde(tag = "type")] pub enum ImageGalleryNodes { Image(Image), @@ -41,7 +41,7 @@ impl From for ImageGalleryNodes { /// Image Gallery node is a node that contains multiple Image nodes /// it starts with `!!!\n` and ends with `\n!!!` -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct ImageGallery { pub nodes: Vec, #[serde(skip_serializing)] @@ -50,10 +50,10 @@ pub struct ImageGallery { impl ImageGallery { pub fn new(consumed_all_input: bool) -> Self { - Self::new_with_nodes(vec![], consumed_all_input) + Self::new_with_nodes(consumed_all_input, vec![]) } - pub fn new_with_nodes(nodes: Vec, consumed_all_input: bool) -> Self { + pub fn new_with_nodes(consumed_all_input: bool, nodes: Vec) -> Self { Self { nodes, consumed_all_input, @@ -130,22 +130,22 @@ mod tests { fn serialize() { assert_eq!( ImageGallery::new_with_nodes( + true, vec![ Image::new(true, "a", "u").into(), Image::new(true, "a2", "u2").into() ], - true ) .to_string(), "!!!\n![a](u)\n![a2](u2)\n!!!" ); assert_eq!( ImageGallery::new_with_nodes( + false, vec![ Image::new(true, "a", "u").into(), Image::new(true, "a2", "u2").into() ], - false ) .to_string(), "!!!\n![a](u)\n![a2](u2)\n!!!\n\n" @@ -156,22 +156,22 @@ mod tests { fn len() { assert_eq!( ImageGallery::new_with_nodes( + true, vec![ Image::new(true, "a", "u").into(), Image::new(true, "a2", "u2").into() ], - true ) .len(), 25 ); assert_eq!( ImageGallery::new_with_nodes( + false, vec![ Image::new(true, "a", "u").into(), Image::new(true, "a2", "u2").into() ], - false ) .len(), 27 @@ -183,21 +183,21 @@ mod tests { assert_eq!( ImageGallery::deserialize("!!!\n![a](u)\n![a2](u2)\n!!!"), Some(ImageGallery::new_with_nodes( + true, vec![ Image::new(true, "a", "u").into(), Image::new(true, "a2", "u2").into() ], - true )) ); assert_eq!( ImageGallery::deserialize("!!!\n![a](u)\n![a2](u2)\n!!!\n\n"), Some(ImageGallery::new_with_nodes( + false, vec![ Image::new(true, "a", "u").into(), Image::new(true, "a2", "u2").into() ], - false )) ); } diff --git a/src/nodes/inline_code.rs b/src/nodes/inline_code.rs index 105ec61..2c8a0a8 100644 --- a/src/nodes/inline_code.rs +++ b/src/nodes/inline_code.rs @@ -4,7 +4,7 @@ use serde::Serialize; use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matcher, node::Node}; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct InlineCode { pub text: String, } diff --git a/src/nodes/italic.rs b/src/nodes/italic.rs index 24c53eb..8ec0232 100644 --- a/src/nodes/italic.rs +++ b/src/nodes/italic.rs @@ -8,7 +8,7 @@ use crate::{ }; /// Representation of an Italic text -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Italic { pub text: String, } diff --git a/src/nodes/list.rs b/src/nodes/list.rs index 19415a8..794e726 100644 --- a/src/nodes/list.rs +++ b/src/nodes/list.rs @@ -17,7 +17,7 @@ pub enum ListTypes { Ordered, } -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] #[serde(tag = "type")] pub enum ListNodes { ListItem(ListItem), @@ -45,7 +45,7 @@ impl From for ListNodes { } } -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct List { pub list_type: ListTypes, pub level: usize, diff --git a/src/nodes/list_item.rs b/src/nodes/list_item.rs index dc37cd9..78ec360 100644 --- a/src/nodes/list_item.rs +++ b/src/nodes/list_item.rs @@ -9,7 +9,7 @@ use super::{ list_item_content::ListItemContent, }; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct ListItem { pub list_type: ListTypes, pub level: usize, diff --git a/src/nodes/list_item_content.rs b/src/nodes/list_item_content.rs index 10b1106..11d98a6 100644 --- a/src/nodes/list_item_content.rs +++ b/src/nodes/list_item_content.rs @@ -14,7 +14,7 @@ use super::{ strikethrough::Strikethrough, text::Text, }; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] #[serde(tag = "type")] pub enum ListItemContentNodes { A(Anchor), @@ -87,7 +87,7 @@ impl Node for ListItemContentNodes { } } -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct ListItemContent { #[serde(skip_serializing)] consumed_all_input: bool, diff --git a/src/nodes/metadata.rs b/src/nodes/metadata.rs index 6b92785..f68b89a 100644 --- a/src/nodes/metadata.rs +++ b/src/nodes/metadata.rs @@ -4,7 +4,7 @@ use crate::toolkit::{context::Context, deserializer::Deserializer, matcher::Matc use chrono::{DateTime, FixedOffset}; use serde::Serialize; -#[derive(Debug, PartialEq, Serialize, Default)] +#[derive(Debug, PartialEq, Serialize, Default, Clone)] pub struct Metadata { pub header: Option, pub timestamp: Option>, diff --git a/src/nodes/paragraph.rs b/src/nodes/paragraph.rs index 49601cf..679349a 100644 --- a/src/nodes/paragraph.rs +++ b/src/nodes/paragraph.rs @@ -13,7 +13,7 @@ use crate::toolkit::{ matcher::Matcher, }; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] #[serde(tag = "type")] pub enum ParagraphNodes { A(Anchor), @@ -86,7 +86,7 @@ impl Node for ParagraphNodes { } } -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Paragraph { #[serde(skip_serializing)] consumed_all_input: bool, diff --git a/src/nodes/strikethrough.rs b/src/nodes/strikethrough.rs index d675a4b..826fcbe 100644 --- a/src/nodes/strikethrough.rs +++ b/src/nodes/strikethrough.rs @@ -8,7 +8,7 @@ use crate::{ }; /// Representation of strike through -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Strikethrough { pub text: String, } diff --git a/src/nodes/text.rs b/src/nodes/text.rs index 12794c2..76ce99f 100644 --- a/src/nodes/text.rs +++ b/src/nodes/text.rs @@ -9,7 +9,7 @@ use crate::toolkit::{ }; /// Representation of a regular text -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] pub struct Text { pub text: String, } diff --git a/src/nodes/yamd.rs b/src/nodes/yamd.rs index 121ed3d..d73d382 100644 --- a/src/nodes/yamd.rs +++ b/src/nodes/yamd.rs @@ -15,7 +15,7 @@ use super::{ image_gallery::ImageGallery, list::List, metadata::Metadata, }; -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone)] #[serde(tag = "type")] pub enum YamdNodes { P(Paragraph), @@ -134,7 +134,7 @@ impl Node for YamdNodes { } /// Yamd is a parent node for every node. -#[derive(Debug, PartialEq, Serialize)] +#[derive(Debug, PartialEq, Serialize, Clone, Default)] pub struct Yamd { pub metadata: Metadata, pub nodes: Vec, @@ -190,12 +190,6 @@ impl Deserializer for Yamd { } } -impl Default for Yamd { - fn default() -> Self { - Self::new(None) - } -} - impl Display for Yamd { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( @@ -359,17 +353,17 @@ end"#; .into(), Image::new(false, 'a', 'u').into(), ImageGallery::new_with_nodes( + false, vec![ Image::new(true, "a", "u").into(), Image::new(true, "a2", "u2").into() ], - false ) .into(), Highlight::new_with_nodes( + false, Some("H"), Some("I"), - false, vec![ Paragraph::new_with_nodes(false, vec![Strikethrough::new("s").into()]) .into(), @@ -450,17 +444,17 @@ end"#; .into(), Image::new(false, 'a', 'u').into(), ImageGallery::new_with_nodes( + false, vec![ Image::new(true, "a", "u").into(), Image::new(true, "a2", "u2").into() ], - false ) .into(), Highlight::new_with_nodes( + false, Some("H"), Some("I"), - false, vec![ Paragraph::new_with_nodes(false, vec![Strikethrough::new("s").into()]) .into(), diff --git a/src/toolkit/context.rs b/src/toolkit/context.rs index 3b70b5a..0725fa0 100644 --- a/src/toolkit/context.rs +++ b/src/toolkit/context.rs @@ -20,7 +20,7 @@ impl From for ContextValues { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub struct Context { inner: HashMap, } @@ -51,12 +51,6 @@ impl Context { } } -impl Default for Context { - fn default() -> Self { - Self::new() - } -} - #[cfg(test)] mod tests { use super::Context;