From dd121715722258ba7d81da48c9f4c783a5c9c662 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Thu, 28 Mar 2024 17:34:14 -0300 Subject: [PATCH] feat: NPF type definitions --- src/convert.rs | 2 + src/convert/npf.rs | 344 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 346 insertions(+) create mode 100644 src/convert/npf.rs diff --git a/src/convert.rs b/src/convert.rs index d7bf5a8..0c9cc6a 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -1,3 +1,5 @@ +mod npf; + #[derive(clap::ValueEnum, Clone, Debug)] pub enum Formats { TumblrNPF, diff --git a/src/convert/npf.rs b/src/convert/npf.rs new file mode 100644 index 0000000..a4ac654 --- /dev/null +++ b/src/convert/npf.rs @@ -0,0 +1,344 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, Serialize)] +pub struct NPF<'a> { + #[serde(borrow)] + pub content: Vec>, +} + +impl<'a> NPF<'a> { + pub fn new() -> NPF<'a> { + NPF { content: vec![] } + } +} + +#[derive(Debug, Deserialize, Serialize)] +pub enum ContentType<'a> { + #[serde(borrow)] + Text(content_types::Text<'a>), + #[serde(borrow)] + Link(content_types::Link<'a>), + #[serde(borrow)] + Audio(content_types::Audio<'a>), + #[serde(borrow)] + Image(content_types::Image<'a>), + #[serde(borrow)] + Video(content_types::Video<'a>), +} + +pub mod content_types { + use super::objects; + use serde::{Deserialize, Serialize}; + use std::collections::HashMap; + + pub mod text { + use serde::{Deserialize, Serialize}; + #[derive(Debug, Deserialize, Serialize)] + pub enum Subtypes { + Heading1, + Heading2, + Quirky, + Quote, + Indented, + Chat, + OrderedListItem, + UnorderedListItem, + } + + #[derive(Debug, Clone, Deserialize, Serialize)] + pub struct Formatting { + pub start: usize, + pub end: usize, + pub r#type: FormattingType, + pub url: Option, + pub blog: Option, + pub color: Option, + } + + #[derive(Debug, Clone, Deserialize, Serialize)] + pub enum FormattingType { + Bold, + Italic, + Small, + Strikethrough, + Link, + Mention, + } + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct Text<'a> { + r#type: &'a str, + pub text: String, + pub subtype: Option, + pub ident_level: Option, + pub formating: Option>, + } + impl<'a> Text<'a> { + pub fn new() -> Text<'a> { + Text { + r#type: "text", + text: String::new(), + subtype: None, + ident_level: None, + formating: None, + } + } + pub fn from(str: String) -> Text<'a> { + Text { + r#type: "text", + text: str, + subtype: None, + ident_level: None, + formating: None, + } + } + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct Image<'a> { + r#type: &'a str, + pub media: Vec, + pub alt_text: Option, + pub caption: Option, + pub feedback_token: Option, + pub colors: Option>, + pub attribution: Option, + } + impl<'a> Image<'a> { + pub fn new() -> Image<'a> { + Image { + r#type: "image", + media: vec![], + alt_text: None, + caption: None, + feedback_token: None, + colors: None, + attribution: None, + } + } + pub fn from(media: Vec) -> Image<'a> { + Image { + media, + r#type: "image", + alt_text: None, + caption: None, + feedback_token: None, + colors: None, + attribution: None, + } + } + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct Link<'a> { + r#type: &'a str, + pub url: String, + pub title: Option, + pub description: Option, + pub author: Option, + pub site_name: Option, + pub display_url: Option, + pub poster: Option>, + } + impl<'a> Link<'a> { + pub fn new() -> Link<'a> { + Link { + r#type: "link", + url: String::new(), + title: None, + description: None, + author: None, + site_name: None, + display_url: None, + poster: None, + } + } + pub fn from(url: String) -> Link<'a> { + Link { + r#type: "link", + url, + title: None, + description: None, + author: None, + site_name: None, + display_url: None, + poster: None, + } + } + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct Audio<'a> { + r#type: &'a str, + pub url: Option, + pub media: Option, + pub provider: Option, + pub title: Option, + pub artist: Option, + pub album: Option, + pub poster: Option>, + pub embed_html: Option, + pub embed_url: Option, + pub metadata: Option, + pub attribution: Option, + } + impl<'a> Audio<'a> { + pub fn new() -> Audio<'a> { + Audio { + r#type: "audio", + url: Some(String::new()), + media: None, + provider: None, + title: None, + artist: None, + album: None, + poster: None, + embed_html: None, + embed_url: None, + metadata: None, + attribution: None, + } + } + pub fn from(url: String) -> Audio<'a> { + Audio { + r#type: "audio", + url: Some(url), + media: None, + provider: None, + title: None, + artist: None, + album: None, + poster: None, + embed_html: None, + embed_url: None, + metadata: None, + attribution: None, + } + } + pub fn from_media(media: objects::Media) -> Audio<'a> { + Audio { + r#type: "audio", + url: None, + media: Some(media), + provider: None, + title: None, + artist: None, + album: None, + poster: None, + embed_html: None, + embed_url: None, + metadata: None, + attribution: None, + } + } + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct Video<'a> { + r#type: &'a str, + pub media: Option, + pub url: Option, + pub provider: Option, + pub embed_html: Option, + pub embed_iframe: Option, + pub embed_url: Option, + pub poster: Option>, + pub filmstrip: Option>, + pub metadata: Option, + pub attribution: Option, + pub can_autoplay_on_cellular: Option, + } + impl<'a> Video<'a> { + pub fn new() -> Video<'a> { + Video { + r#type: "video", + url: Some(String::new()), + media: None, + provider: None, + embed_html: None, + embed_iframe: None, + embed_url: None, + poster: None, + filmstrip: None, + metadata: None, + attribution: None, + can_autoplay_on_cellular: None, + } + } + pub fn from(url: String) -> Video<'a> { + Video { + r#type: "video", + url: Some(url), + media: None, + provider: None, + embed_html: None, + embed_iframe: None, + embed_url: None, + poster: None, + filmstrip: None, + metadata: None, + attribution: None, + can_autoplay_on_cellular: None, + } + } + pub fn from_media(media: objects::Media) -> Video<'a> { + Video { + r#type: "video", + url: None, + media: Some(media), + provider: None, + embed_html: None, + embed_iframe: None, + embed_url: None, + poster: None, + filmstrip: None, + metadata: None, + attribution: None, + can_autoplay_on_cellular: None, + } + } + } + + // TODO: Paywall type +} +pub mod objects { + use serde::{Deserialize, Serialize}; + + #[derive(Debug, Clone, Deserialize, Serialize)] + pub struct Blog { + pub uuid: String, + pub name: String, + pub url: String, + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct Media { + pub r#type: String, + pub url: String, + pub width: i32, + pub height: i32, + pub poster: Option, + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct MediaPoster { + pub r#type: String, + pub url: String, + pub width: Option, + pub height: Option, + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct Attribution { + pub r#type: String, + pub url: String, + pub post: Post, + pub blug: Blog, + } + + #[derive(Debug, Deserialize, Serialize)] + pub struct Post { + pub id: u64, + } +}