-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.rs
166 lines (156 loc) · 5.07 KB
/
storage.rs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::{
collections::{BTreeMap, BTreeSet},
path::Path,
};
use chrono::{DateTime, FixedOffset};
use crate::database::{FeedId, LookupKey, SourceLookup};
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, PartialEq)]
pub enum FeedHeader {
Rss(crate::feeds::rss::ChannelHeader),
FeedRs(crate::feeds::feed_rs::FeedHeader),
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, PartialEq)]
pub enum FeedItem {
Rss(crate::feeds::rss::Item),
FeedRs(crate::feeds::feed_rs::Entry),
}
impl FeedItem {
pub fn display_title(&self) -> Option<&str> {
match self {
FeedItem::Rss(item) => item.title.as_deref().map(str::trim),
FeedItem::FeedRs(entry) => entry.title.as_ref().map(|v| v.content.trim()),
}
}
pub fn publish_date(&self) -> Option<DateTime<FixedOffset>> {
match self {
FeedItem::Rss(item) => item
.pub_date
.as_ref()
.and_then(|v| DateTime::parse_from_rfc2822(v).ok()),
FeedItem::FeedRs(entry) => entry.published.as_ref().map(|v| {
// TODO: This is ugly
DateTime::parse_from_rfc2822(&v.to_rfc2822()).unwrap()
}),
}
}
pub fn publish_date_or_old(&self) -> DateTime<FixedOffset> {
self.publish_date()
.unwrap_or_else(|| chrono::DateTime::parse_from_rfc3339("1990-01-01T00:00:00").unwrap())
}
pub fn sort<T, F: FnMut(&T) -> &Self>(items: &mut [T], mut f: F) {
items.sort_by_cached_key(|k| f(k).publish_date_or_old());
}
pub fn display_title_without_prefix(&self, prefix: &str) -> Option<&str> {
self.display_title().map(|t| {
t.trim()
.strip_prefix(prefix)
.unwrap_or(t)
.trim()
.strip_prefix("-")
.unwrap_or(t)
.trim()
})
}
pub fn content_link(&self) -> Option<&str> {
match self {
FeedItem::Rss(item) => item.link.as_deref(),
FeedItem::FeedRs(entry) => entry.links.first().map(|link| &link.href[..]),
}
}
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct Feed {
pub name: String,
pub feed_url: Option<String>,
pub opml: Option<opml::Outline>,
#[serde(default)]
pub feed_headers: Vec<FeedHeader>,
#[serde(default)]
pub feeds: Vec<FeedItem>,
pub parent: Option<FeedId>,
#[serde(default)]
pub tags: BTreeSet<String>,
#[serde(skip)]
_private: (),
}
impl Feed {
pub fn new(name: String) -> Self {
Self {
name,
feed_url: None,
opml: None,
feed_headers: Vec::new(),
feeds: Vec::new(),
parent: None,
tags: BTreeSet::new(),
_private: (),
}
}
pub fn display_name(&self) -> &str {
self.name.trim()
}
pub fn key(&self) -> LookupKey<'_> {
LookupKey {
name: &self.name,
feed_url: self.feed_url.as_deref(),
}
}
}
#[derive(Default)]
pub struct Storage {
sources: BTreeMap<FeedId, Feed>,
}
impl Storage {
fn open(path: &Path) -> std::io::Result<Self> {
let feed_path = path.join("feeds");
let mut sources = BTreeMap::new();
for e in std::fs::read_dir(&feed_path)? {
let e = e?;
let feed_file = e.path();
let id = feed_file
.file_stem()
.expect("file does not have a name")
.to_str()
.expect("file does not have unicode name")
.to_owned();
let file = std::fs::read_to_string(&feed_file)?;
let feed: Feed =
serde_json::from_str(&file).expect("file could be read, but not parsed");
sources.insert(id, feed);
}
Ok(Self { sources })
}
pub fn open_or_default(storage_path: &Path) -> Self {
Self::open(&storage_path).unwrap_or_default()
}
pub fn save(&self, path: &Path) {
let feed_path = path.join("feeds");
std::fs::create_dir_all(&feed_path).unwrap();
for (feed_id, source) in self.iter() {
let file_path = feed_path.join(feed_id).with_extension("json");
super::safe_save_json(source, &file_path);
}
}
pub fn write_to_cache(&self, lookup: &mut SourceLookup) {
for (feed_id, source) in &self.sources {
lookup.touch(feed_id, source.key());
}
}
pub fn iter(&self) -> impl Iterator<Item = (&FeedId, &Feed)> + '_ {
self.sources.iter()
}
/*
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&FeedId, &mut Feed)> + '_ {
self.sources.iter_mut()
}
*/
pub fn get_or_insert(&mut self, feed_id: FeedId, feed: &Feed) -> &mut Feed {
self.sources.entry(feed_id).or_insert_with(|| feed.clone())
}
pub fn get(&self, feed_id: &FeedId) -> Option<&Feed> {
self.sources.get(feed_id)
}
pub fn get_mut(&mut self, feed_id: &FeedId) -> Option<&mut Feed> {
self.sources.get_mut(feed_id)
}
}