parse_blogger_backup_xml/
models.rs

1use crate::errors::EmptyResult;
2use crate::utilities;
3use chrono::DateTime;
4use chrono::Datelike;
5use chrono::FixedOffset;
6use chrono::Timelike;
7#[derive(Clone, Debug)]
8pub struct Comment {
9    pub author_name: String,
10    pub content: String,
11    pub id: String,
12    pub post_id: String,
13    pub published: DateTime<FixedOffset>,
14    pub title: String,
15}
16
17#[derive(Clone, Debug)]
18pub struct Post {
19    pub author_name: String,
20    pub comments: Vec<Comment>,
21    pub content: String,
22    pub draft: bool,
23    pub id: String,
24    pub published: DateTime<FixedOffset>,
25    pub title: String,
26}
27
28impl Post {
29    pub fn save_content(&self) -> EmptyResult {
30        let path = format!(
31            "data/bookroot/post_content_for_{}-{}-{}-{}-{}-{}",
32            self.published.year(),
33            self.published.month(),
34            self.published.day(),
35            self.published.hour(),
36            self.published.minute(),
37            self.published.second(),
38        );
39        let content = self.content.clone();
40        utilities::save(&path, content)?;
41        Ok(())
42    }
43}
44
45#[derive(Clone, Debug)]
46pub enum EntryKind {
47    Comment,
48    Post,
49    Settings,
50    Template,
51}
52#[derive(Clone, Debug, Default)]
53pub struct Entry {
54    pub author_name: Option<String>,
55    pub content: Option<String>,
56    pub draft: bool,
57    pub id: Option<String>,
58    pub kind: Option<EntryKind>,
59    pub post_id: Option<String>,
60    pub published: Option<DateTime<FixedOffset>>,
61    pub title: Option<String>,
62}
63
64impl Entry {
65    pub fn new() -> Entry {
66        Entry {
67            author_name: None,
68            content: None,
69            draft: false,
70            id: None,
71            kind: None,
72            post_id: None,
73            published: None,
74            title: None,
75        }
76    }
77    pub fn to_post(&self) -> Option<Post> {
78        if let Entry {
79            author_name: Some(author_name),
80            content: Some(content),
81            draft,
82            kind: Some(EntryKind::Post),
83            id: Some(id),
84            published: Some(published),
85            title: Some(title),
86            ..
87        } = self
88        {
89            Some(Post {
90                author_name: author_name.to_owned(),
91                comments: vec![],
92                content: content.to_owned(),
93                draft: draft.to_owned(),
94                id: id.to_owned(),
95                published: published.to_owned(),
96                title: title.to_owned(),
97            })
98        } else {
99            None
100        }
101    }
102    pub fn to_comment(&self) -> Option<Comment> {
103        if let Entry {
104            author_name: Some(author_name),
105            content: Some(content),
106            draft: _draft,
107            kind: Some(EntryKind::Comment),
108            id: Some(id),
109            published: Some(published),
110            title: Some(title),
111            post_id: Some(post_id),
112        } = self
113        {
114            Some(Comment {
115                author_name: author_name.to_owned(),
116                content: content.to_owned(),
117                id: id.to_owned(),
118                post_id: post_id.to_owned(),
119                published: published.to_owned(),
120                title: title.to_owned(),
121            })
122        } else {
123            None
124        }
125    }
126    pub fn clear(&mut self) {
127        self.author_name = None;
128        self.content = None;
129        self.draft = false;
130        self.id = None;
131        self.kind = None;
132        self.post_id = None;
133        self.published = None;
134        self.title = None;
135    }
136}