From b6d2088863cfc9873dc7601175570777dc0e0035 Mon Sep 17 00:00:00 2001 From: postscriptum Date: Wed, 2 Jul 2025 20:38:22 +0300 Subject: [PATCH] make sure post entry has expected content type, reorganize post members --- src/main.rs | 4 ++ src/snac/user/public.rs | 45 +-------------------- src/snac/user/public/post.rs | 52 +++++++++++++++++++++++++ src/snac/user/public/post/attachment.rs | 7 ++++ src/snac/user/public/post/ptype.rs | 4 ++ src/snac/user/public/post/tag.rs | 4 ++ 6 files changed, 73 insertions(+), 43 deletions(-) create mode 100644 src/snac/user/public/post.rs create mode 100644 src/snac/user/public/post/attachment.rs create mode 100644 src/snac/user/public/post/ptype.rs create mode 100644 src/snac/user/public/post/tag.rs diff --git a/src/main.rs b/src/main.rs index e5729ad..46151e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,10 @@ fn sync(snac: &Snac, nex: &Nex) -> Result<(usize, usize)> { println!("\tsync profile for `{}`...", user.name); for post in user.public()? { t += 1; + // make sure post entry has expected content type + if !post.is_note() { + todo!() + } // skip non authorized content if let Some(content) = post.source_content { println!("\t\tsync post `{}`...", post.id); diff --git a/src/snac/user/public.rs b/src/snac/user/public.rs index ab90e5d..61f5994 100644 --- a/src/snac/user/public.rs +++ b/src/snac/user/public.rs @@ -1,43 +1,2 @@ -use chrono::{DateTime, Utc}; -use serde::{Deserialize, de::Error}; -use std::str::FromStr; - -#[derive(Deserialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct Attachment { - pub media_type: String, - pub url: String, - pub name: String, -} - -#[derive(Deserialize, Debug)] -pub struct Tag { - pub name: String, -} - -#[derive(Deserialize, Debug)] -#[serde(rename_all = "camelCase")] -pub struct Post { - pub attachment: Option>, - pub id: String, - #[serde(default, deserialize_with = "time")] - pub published: DateTime, - pub source_content: Option, - pub tag: Option>, - #[serde(default, deserialize_with = "time_option")] - pub updated: Option>, - pub url: String, -} - -fn time<'de, D: serde::Deserializer<'de>>(d: D) -> Result, D::Error> { - let s = String::deserialize(d)?; - DateTime::from_str(&s).map_err(Error::custom) -} - -fn time_option<'de, D: serde::Deserializer<'de>>(d: D) -> Result>, D::Error> { - let s: Option = Option::deserialize(d)?; - match s { - Some(ref t) => DateTime::from_str(t).map(Some).map_err(Error::custom), - None => Ok(None), - } -} +mod post; +pub use post::Post; diff --git a/src/snac/user/public/post.rs b/src/snac/user/public/post.rs new file mode 100644 index 0000000..8fefa0e --- /dev/null +++ b/src/snac/user/public/post.rs @@ -0,0 +1,52 @@ +mod attachment; +mod ptype; +mod tag; + +use attachment::Attachment; +use chrono::{DateTime, Utc}; +use ptype::Type; +use serde::{Deserialize, de::Error}; +use std::str::FromStr; +use tag::Tag; + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct Post { + #[serde(rename = "type", deserialize_with = "ptype")] + pub ptype: Type, + pub attachment: Option>, + pub id: String, + #[serde(default, deserialize_with = "time")] + pub published: DateTime, + pub source_content: Option, + pub tag: Option>, + #[serde(default, deserialize_with = "time_option")] + pub updated: Option>, + pub url: String, +} + +impl Post { + pub fn is_note(&self) -> bool { + matches!(self.ptype, Type::Note) + } +} + +fn ptype<'de, D: serde::Deserializer<'de>>(d: D) -> Result { + if String::deserialize(d)?.to_lowercase() == "note" { + return Ok(Type::Note); + } + Err(D::Error::custom("Invalid post type")) +} + +fn time<'de, D: serde::Deserializer<'de>>(d: D) -> Result, D::Error> { + let s = String::deserialize(d)?; + DateTime::from_str(&s).map_err(Error::custom) +} + +fn time_option<'de, D: serde::Deserializer<'de>>(d: D) -> Result>, D::Error> { + let s: Option = Option::deserialize(d)?; + match s { + Some(ref t) => DateTime::from_str(t).map(Some).map_err(Error::custom), + None => Ok(None), + } +} diff --git a/src/snac/user/public/post/attachment.rs b/src/snac/user/public/post/attachment.rs new file mode 100644 index 0000000..076c75a --- /dev/null +++ b/src/snac/user/public/post/attachment.rs @@ -0,0 +1,7 @@ +#[derive(serde::Deserialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct Attachment { + pub media_type: String, + pub url: String, + pub name: String, +} diff --git a/src/snac/user/public/post/ptype.rs b/src/snac/user/public/post/ptype.rs new file mode 100644 index 0000000..6a70775 --- /dev/null +++ b/src/snac/user/public/post/ptype.rs @@ -0,0 +1,4 @@ +#[derive(Debug)] +pub enum Type { + Note, +} diff --git a/src/snac/user/public/post/tag.rs b/src/snac/user/public/post/tag.rs new file mode 100644 index 0000000..e759768 --- /dev/null +++ b/src/snac/user/public/post/tag.rs @@ -0,0 +1,4 @@ +#[derive(serde::Deserialize, Debug)] +pub struct Tag { + pub name: String, +}