From 9db060c4207034042b7537683b4f45f47dbd3274 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Tue, 23 Apr 2024 15:35:56 -0300 Subject: [PATCH] refactor: improve the api for the structs and remove Default public implementation --- src/convert/npf.rs | 189 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 142 insertions(+), 47 deletions(-) diff --git a/src/convert/npf.rs b/src/convert/npf.rs index 0a411b1..48a017b 100644 --- a/src/convert/npf.rs +++ b/src/convert/npf.rs @@ -35,12 +35,6 @@ pub mod blocks { UnordoredListItem, } - trait BlockType: Default { - fn new() -> Self { - Self::default() - } - } - #[serde_with::skip_serializing_none] #[derive(Debug, Deserialize, Serialize)] pub struct BlockText { @@ -50,7 +44,10 @@ pub mod blocks { pub formatting: Option>, pub ident_level: Option, } - impl Default for BlockText { + impl BlockText { + pub fn new(value: &str) -> Self { + Self::from(value) + } fn default() -> Self { Self { r#type: String::from("text"), @@ -77,7 +74,6 @@ pub mod blocks { } } } - impl BlockType for BlockText {} #[serde_with::skip_serializing_none] #[derive(Debug, Deserialize, Serialize)] @@ -91,7 +87,10 @@ pub mod blocks { pub alt_text: Option, pub caption: Option, } - impl Default for BlockImage { + impl BlockImage { + pub fn new(media: Vec) -> Self { + Self::from(media) + } fn default() -> Self { Self { r#type: String::from("image"), @@ -118,7 +117,6 @@ pub mod blocks { Self::from(vec![value]) } } - impl BlockType for BlockImage {} #[serde_with::skip_serializing_none] #[derive(Debug, Deserialize, Serialize)] @@ -132,7 +130,10 @@ pub mod blocks { pub display_url: Option, pub poster: Option, } - impl Default for BlockLink { + impl BlockLink { + pub fn new(url: url::Url) -> Self { + Self::from(url) + } fn default() -> Self { Self { r#type: String::from("link"), @@ -154,7 +155,6 @@ pub mod blocks { } } } - impl BlockType for BlockLink {} #[serde_with::skip_serializing_none] #[derive(Debug, Deserialize, Serialize)] @@ -173,6 +173,9 @@ pub mod blocks { pub attribution: Option, } impl BlockAudio { + pub fn new(url: url::Url) -> Self { + Self::from(url) + } pub fn is_valid(&self) -> bool { if self.url.is_some() || self.media.is_some() { true @@ -180,8 +183,6 @@ pub mod blocks { false } } - } - impl Default for BlockAudio { fn default() -> Self { Self { r#type: String::from("audio"), @@ -215,7 +216,6 @@ pub mod blocks { } } } - impl BlockType for BlockAudio {} #[serde_with::skip_serializing_none] #[derive(Debug, Deserialize, Serialize)] @@ -233,6 +233,9 @@ pub mod blocks { pub can_autoplay_on_cellular: Option, } impl BlockVideo { + pub fn new(url: url::Url) -> Self { + Self::from(url) + } pub fn is_valid(&self) -> bool { if self.url.is_some() || self.media.is_some() { true @@ -240,8 +243,6 @@ pub mod blocks { false } } - } - impl Default for BlockVideo { fn default() -> Self { Self { r#type: String::from("audio"), @@ -274,7 +275,6 @@ pub mod blocks { } } } - impl BlockType for BlockVideo {} } pub mod text_formatting { @@ -296,12 +296,8 @@ pub mod text_formatting { Color(FormatTypeColor), } - // TODO: Make default() be private, removing the Default implementation - // since private implementations does't appear outside the module - trait FormatType: Default + From> + From { - fn new() -> Self { - Self::default() - } + trait FormatType: From> + From { + fn default() -> Self; } #[derive(Debug, Deserialize, Serialize)] @@ -311,7 +307,15 @@ pub mod text_formatting { pub end: u64, pub url: url::Url, } - impl Default for FormatTypeLink { + impl FormatTypeLink { + pub fn new(range: Range, url: url::Url) -> Self { + Self { + url, + ..Self::from(range) + } + } + } + impl FormatType for FormatTypeLink { fn default() -> Self { Self { r#type: String::from("link"), @@ -329,13 +333,21 @@ pub mod text_formatting { pub end: u64, pub blog: objects::BlogInfo, } - impl Default for FormatTypeMention { + impl FormatTypeMention { + pub fn new(range: Range, blog: objects::BlogInfo) -> Self { + Self { + blog, + ..Self::from(range) + } + } + } + impl FormatType for FormatTypeMention { fn default() -> Self { Self { r#type: String::from("mention"), start: 0, end: 0, - blog: objects::BlogInfo::default(), + blog: objects::BlogInfo::new("t:0"), } } } @@ -347,7 +359,15 @@ pub mod text_formatting { pub end: u64, pub hex: String, } - impl Default for FormatTypeColor { + impl FormatTypeColor { + pub fn new(range: Range, hex: color_art::Color) -> Self { + Self { + hex: hex.hex(), + ..Self::from(range) + } + } + } + impl FormatType for FormatTypeColor { fn default() -> Self { Self { r#type: String::from("link"), @@ -389,7 +409,6 @@ pub mod text_formatting { } } })* - $(impl FormatType for $t {})* }; // Defines the struct and implements Default trait if the token is an // identifier and a literal @@ -400,7 +419,7 @@ pub mod text_formatting { pub start: u64, pub end: u64, })* - $(impl Default for $t { + $(impl FormatType for $t { fn default() -> Self { Self { r#type: String::from($s), @@ -439,8 +458,11 @@ pub mod objects { pub fn new(uuid: &str) -> Self { Self::from(uuid) } - } - impl Default for BlogInfo { + pub fn is_valid(&self) -> bool { + !self.uuid.is_empty() + || !self.uuid.chars().count() == 22 + || !self.uuid.starts_with("t:") + } fn default() -> Self { Self { uuid: String::new(), @@ -490,10 +512,15 @@ pub mod objects { } impl From for Post { fn from(value: u64) -> Self { - Self { id: value } + Self { + id: value, + id_string: value.to_string(), + ..Self::default() + } } } + #[serde_with::skip_serializing_none] #[derive(Debug, Deserialize, Serialize)] pub struct Media { pub r#type: Option, @@ -504,6 +531,30 @@ pub mod objects { pub cropped: Option, pub has_original_dimentions: Option, } + impl Media { + pub fn new(url: url::Url) -> Self { + Self::from(url) + } + fn default() -> Self { + Self { + r#type: None, + url: url::Url::parse("https://tumblr.com").unwrap(), + width: None, + height: None, + original_dimensions_missing: None, + cropped: None, + has_original_dimentions: None, + } + } + } + impl From for Media { + fn from(value: url::Url) -> Self { + Self { + url: value, + ..Self::default() + } + } + } #[derive(Debug, Deserialize, Serialize)] pub struct EmbedIframe { @@ -528,12 +579,6 @@ pub mod attributions { Blog(AttributionBlog), } - trait AttributionType: Default { - fn new() -> Self { - Self::default() - } - } - #[derive(Debug, Deserialize, Serialize)] pub struct AttributionPost { r#type: String, @@ -541,7 +586,18 @@ pub mod attributions { pub post: objects::Post, pub blog: objects::BlogInfo, } - impl Default for AttributionPost { + impl AttributionPost { + pub fn new(url: url::Url, post: objects::Post, blog: objects::BlogInfo) -> Self { + Self { + url, + post, + blog, + ..Self::default() + } + } + pub fn is_valid(&self) -> bool { + self.blog.is_valid() && self.post.is_valid() + } fn default() -> Self { Self { r#type: String::from("post"), @@ -551,14 +607,16 @@ pub mod attributions { } } } - impl AttributionType for AttributionPost {} #[derive(Debug, Deserialize, Serialize)] pub struct AttributionLink { r#type: String, pub url: url::Url, } - impl Default for AttributionLink { + impl AttributionLink { + pub fn new(url: url::Url) -> Self { + Self::from(url) + } fn default() -> Self { Self { r#type: String::from("link"), @@ -566,7 +624,14 @@ pub mod attributions { } } } - impl AttributionType for AttributionLink {} + impl From for AttributionLink { + fn from(value: url::Url) -> Self { + Self { + url: value, + ..Self::default() + } + } + } #[derive(Debug, Deserialize, Serialize)] pub struct AttributionBlog { @@ -574,7 +639,13 @@ pub mod attributions { pub url: Option, pub blog: objects::BlogInfo, } - impl Default for AttributionBlog { + impl AttributionBlog { + pub fn new(blog: objects::BlogInfo) -> Self { + Self::from(blog) + } + pub fn is_valid(&self) -> bool { + self.blog.is_valid() + } fn default() -> Self { Self { r#type: String::from("blog"), @@ -583,7 +654,21 @@ pub mod attributions { } } } - impl AttributionType for AttributionBlog {} + impl From for AttributionBlog { + fn from(value: objects::BlogInfo) -> Self { + Self { + blog: value, + ..Self::default() + } + } + } + /* TODO: Add TryFrom which checks if given BlogInfo is valid + impl TryFrom for AttributionBlog { + fn try_from(value: objects::BlogInfo) -> Result { + todo!() + } + } + */ #[derive(Debug, Deserialize, Serialize)] pub struct AttributionApp { @@ -593,7 +678,10 @@ pub mod attributions { pub display_text: Option, pub logo: Option, } - impl Default for AttributionApp { + impl AttributionApp { + pub fn new(url: url::Url) -> Self { + Self::from(url) + } fn default() -> Self { Self { r#type: String::from("blog"), @@ -604,5 +692,12 @@ pub mod attributions { } } } - impl AttributionType for AttributionApp {} + impl From for AttributionApp { + fn from(value: url::Url) -> Self { + Self { + url: value, + ..Self::default() + } + } + } }