aboutsummaryrefslogtreecommitdiffstats
path: root/src/blog.rs
blob: 0e757d5097ab9c77178b667a115692853f1d5c48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::path::Path;

use markdown::{mdast::Node, CompileOptions, Constructs, Options, ParseOptions};
use serde::Deserialize;
use tokio::{fs, io::AsyncReadExt};

use chrono::{DateTime, Utc};

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

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

#[derive(Clone)]
pub struct Blogpost {
    file_name: String,
    title: String,
    published_at: DateTime<Utc>,
    updated_at: Option<DateTime<Utc>>,
    tags: Vec<String>,
    content: String,
}

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

impl Article for Blogpost {
    type Metadata = BlogpostMetadata;

    type Article = Blogpost;

    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(Blogpost {
            file_name,
            title: metadata.title,
            published_at: metadata.published_at.parse::<DateTime<Utc>>()?,
            updated_at,
            tags: metadata.tags,
            content,
        })
    }
}

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

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

    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<String> {
        &self.tags
    }

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

    fn post_type(&self) -> PostType {
        PostType::Article
    }

    fn content(&self) -> &str {
        &self.content
    }
}

impl Blogpost {
    pub fn file_name(&self) -> &str {
        &self.file_name
    }
}