-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy patharticle.rs
More file actions
74 lines (68 loc) · 2.18 KB
/
Copy patharticle.rs
File metadata and controls
74 lines (68 loc) · 2.18 KB
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
use chrono::Utc;
use feed_rs::model::Entry;
use serde::Serialize;
use crate::structs::image::Image;
#[derive(Debug, Serialize)]
pub struct Article {
pub id: String,
pub title: String,
pub content: String,
pub date: i64,
pub read: bool,
pub image: Option<Image>
}
impl From<Entry> for Article {
fn from(entry: Entry) -> Self {
let id = entry.id;
let title = match entry.title {
Some(t) => t.content,
None => String::from("No title found, please report this issue."),
};
let content = match entry.content {
Some(c) => c.body.unwrap()
.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('\n', "<br>\n")
.replace('\t', " "),
None => match entry.summary {
Some(s) => {
// Convert plain text to HTML preserving formatting
s.content
.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('\n', "<br>\n")
.replace('\t', " ")
},
None => String::from("No content found, please report this issue."),
},
};
let date = entry.published
.unwrap_or_else(|| entry.updated.unwrap_or_else(|| Utc::now()))
.timestamp_millis();
let image = if entry.media.is_empty() {
None
} else {
let media = entry.media.iter().filter(|m| !m.thumbnails.is_empty()).next();
match media {
Some(m) => {
let thumbnail = m.thumbnails.iter().next();
match thumbnail {
Some(t) => Some(Image::from(t.image.clone())),
None => None
}
},
None => None
}
};
Article {
id,
title,
content,
date,
read: false,
image,
}
}
}