-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbookmarks.rs
More file actions
156 lines (126 loc) · 3.74 KB
/
bookmarks.rs
File metadata and controls
156 lines (126 loc) · 3.74 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
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
use std::collections::BTreeMap;
use std::path::Path;
use anyhow::{Context, Ok};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use toml;
// todo!(bookmarks): replace bookmarks.bookmarks.insert() with bookmarks.insert_bookmark()
pub static DEFAULT_BOOKMARKS: Lazy<Bookmarks> = Lazy::new(|| {
let mut bookmarks = Bookmarks::default();
bookmarks.bookmarks.insert(
1.to_string(),
BookmarkBuilder::new()
.title("Gemini Project")
.url("gemini://geminiprotocol.net")
.build(),
);
bookmarks.bookmarks.insert(
2.to_string(),
BookmarkBuilder::new()
.title("Spacewalk aggregator")
.url("gemini://rawtext.club:1965/~sloum/spacewalk.gmi")
.build(),
);
bookmarks.bookmarks.insert(
3.to_string(),
BookmarkBuilder::new()
.title("About Geopard + help")
.url("about:help")
.build(),
);
bookmarks
});
#[derive(Clone, Default, Serialize, Deserialize, Debug)]
pub struct Bookmark {
title: String,
description: Option<String>,
url: String,
}
#[derive(Clone, Default, Debug)]
pub struct BookmarkBuilder {
title: String,
description: Option<String>,
url: String,
}
#[derive(Clone, Default, Serialize, Deserialize, Debug)]
pub struct Bookmarks {
#[serde(rename = "bookmark")]
pub bookmarks: BTreeMap<String, Bookmark>,
}
impl BookmarkBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn title(mut self, title: &str) -> Self {
self.title = String::from(title);
self
}
pub fn description(mut self, description: Option<&str>) -> Self {
match description {
Some(desc) => self.description = Some(String::from(desc)),
None => self.description = None,
}
self
}
pub fn url(mut self, url: &str) -> Self {
self.url = String::from(url);
self
}
pub fn build(self) -> Bookmark {
Bookmark {
title: self.title,
description: self.description,
url: self.url,
}
}
}
impl Bookmark {
pub fn title(&self) -> String {
self.title.clone()
}
pub fn set_title(&mut self, title: &str) {
self.title = String::from(title);
}
pub fn description(&self) -> Option<String> {
self.description.as_ref().cloned()
}
pub fn set_description(&mut self, description: &str) {
self.description = Some(String::from(description));
}
pub fn url(&self) -> String {
self.url.clone()
}
pub fn set_url(&mut self, url: &str) {
self.url = String::from(url);
}
}
//todo!(bookmarks): Add from_gmi() method for migrations
impl Bookmarks {
pub async fn from_file(&self, path: &Path) -> anyhow::Result<Self> {
let file_str = async_fs::read_to_string(path)
.await
.context("Reading bookmarks file")?;
let bookmarks = toml::from_str(&file_str)?;
Ok(bookmarks)
}
pub async fn to_file(&self, path: &Path) -> anyhow::Result<()> {
let toml = toml::to_string(self)?;
async_fs::write(path, toml)
.await
.context("Writting data to bookmarks file")?;
Ok(())
}
//todo!(bookmarks): key must be the biggest current key + 1
pub fn insert_bookmark(&mut self, bookmark: Bookmark) {
self.bookmarks.insert(1.to_string(), bookmark);
}
pub fn update_bookmark(&mut self, key: u32, new_bookmark: Bookmark) {
self.bookmarks.insert(key.to_string(), new_bookmark);
}
pub fn remove_bookmark(&mut self, key: u32) {
if self.bookmarks.is_empty() {
return;
}
self.bookmarks.remove(&key.to_string());
}
}