From d1bfe2eb0ba8836d4eef4ca5dd6e20611dc3a8c0 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Wed, 31 Jan 2024 18:55:36 +0000 Subject: changed Post tags() to return a Vec<&str> --- src/atom.rs | 4 ++-- src/blog.rs | 4 ++-- src/main.rs | 1 + src/poetry.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- src/posts.rs | 4 ++-- 5 files changed, 49 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/atom.rs b/src/atom.rs index a64a6aa..bda4c51 100644 --- a/src/atom.rs +++ b/src/atom.rs @@ -107,9 +107,9 @@ pub async fn atom(ctx: Context, entries: Vec

) -> Feed { .tags() .into_iter() .map(|category| Category { - term: category.clone(), + term: category.to_string(), scheme: None, - label: Some(category.clone()), + label: Some(category.to_string()), }) .collect(); let published = Some(entry.published_at().to_owned().into()); diff --git a/src/blog.rs b/src/blog.rs index 0e757d5..b6ca75c 100644 --- a/src/blog.rs +++ b/src/blog.rs @@ -76,8 +76,8 @@ impl Post for Blogpost { self.updated_at.as_ref() } - fn tags(&self) -> &Vec { - &self.tags + fn tags(&self) -> Vec<&str> { + self.tags.iter().map(|s| &**s).collect() } fn lang(&self) -> &str { diff --git a/src/main.rs b/src/main.rs index 3d52f46..4f8ec55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,7 @@ use tracing_subscriber::FmtSubscriber; use crate::article::Article; use crate::blog::Blogpost; +use crate::posts::Post; type Result = std::result::Result; diff --git a/src/poetry.rs b/src/poetry.rs index f5f194c..6429bd3 100644 --- a/src/poetry.rs +++ b/src/poetry.rs @@ -1,6 +1,10 @@ use chrono::{DateTime, Utc}; +use serde::Deserialize; -use crate::posts::Post; +use crate::Result; +use crate::{article::Article, posts::Post}; + +static DIRECTORY: &str = "./poetry"; pub struct Poem { file_name: String, @@ -13,6 +17,41 @@ pub struct Poem { lang: String, } +#[derive(Deserialize)] +pub struct PoemMetadata { + title: Option, + created_at: String, + published_at: String, + updated_at: Option, +} + +impl Article for Poem { + type Article = Poem; + + type Metadata = PoemMetadata; + + fn directory() -> &'static str { + DIRECTORY + } + + fn new(file_name: String, metadata: Self::Metadata, content: String) -> Result { + let updated_at = if let Some(updated_at) = metadata.updated_at { + Some(updated_at.parse::>()?) + } else { + None + }; + Ok(Poem { + file_name, + title: metadata.title, + created_at: metadata.created_at.parse::>()?, + published_at: metadata.published_at.parse::>()?, + updated_at, + content, + lang: "en".to_string(), + }) + } +} + impl Post for Poem { fn id(&self) -> &str { &self.file_name @@ -30,8 +69,8 @@ impl Post for Poem { self.updated_at.as_ref() } - fn tags(&self) -> &Vec { - todo!() + fn tags(&self) -> Vec<&str> { + vec!["poetry"] } fn lang(&self) -> &str { diff --git a/src/posts.rs b/src/posts.rs index a8e9f90..93c9179 100644 --- a/src/posts.rs +++ b/src/posts.rs @@ -14,7 +14,7 @@ pub trait Post { fn subject(&self) -> Option<&str>; fn published_at(&self) -> &DateTime; fn updated_at(&self) -> Option<&DateTime>; - fn tags(&self) -> &Vec; + fn tags(&self) -> Vec<&str>; fn lang(&self) -> &str; fn post_type(&self) -> PostType; fn content(&self) -> &str; @@ -23,7 +23,7 @@ pub trait Post { "https://en.blos.sm/posts/".to_owned() + self.id() } - fn get_tags<'a>(posts: &'a Vec) -> Vec<&'a String> + fn get_tags<'a>(posts: &'a Vec) -> Vec<&'a str> where Self: Sized, { -- cgit