aboutsummaryrefslogblamecommitdiffstats
path: root/src/poetry.rs
blob: 6429bd38e5634d46e08aa28bf7968272f7782ae3 (plain) (tree)
1
2
3
4
5
6
7
                            
                       
 



                                           











                                                    


































                                                                                                   
















                                                    

                                 













                                                   
use chrono::{DateTime, Utc};
use serde::Deserialize;

use crate::Result;
use crate::{article::Article, posts::Post};

static DIRECTORY: &str = "./poetry";

pub struct Poem {
    file_name: String,
    title: Option<String>,
    created_at: DateTime<Utc>,
    published_at: DateTime<Utc>,
    updated_at: Option<DateTime<Utc>>,
    content: String,
    // TODO: localisation (get lang from file names)
    lang: String,
}

#[derive(Deserialize)]
pub struct PoemMetadata {
    title: Option<String>,
    created_at: String,
    published_at: String,
    updated_at: Option<String>,
}

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<Self::Article> {
        let updated_at = if let Some(updated_at) = metadata.updated_at {
            Some(updated_at.parse::<DateTime<Utc>>()?)
        } else {
            None
        };
        Ok(Poem {
            file_name,
            title: metadata.title,
            created_at: metadata.created_at.parse::<DateTime<Utc>>()?,
            published_at: metadata.published_at.parse::<DateTime<Utc>>()?,
            updated_at,
            content,
            lang: "en".to_string(),
        })
    }
}

impl Post for Poem {
    fn id(&self) -> &str {
        &self.file_name
    }

    fn subject(&self) -> Option<&str> {
        self.title.as_deref()
    }

    fn published_at(&self) -> &DateTime<Utc> {
        &self.published_at
    }

    fn updated_at(&self) -> Option<&DateTime<Utc>> {
        self.updated_at.as_ref()
    }

    fn tags(&self) -> Vec<&str> {
        vec!["poetry"]
    }

    fn lang(&self) -> &str {
        "en"
    }

    fn post_type(&self) -> crate::posts::PostType {
        todo!()
    }

    fn content(&self) -> &str {
        todo!()
    }
}