bogrep 0.10.1

Full-text search for bookmarks from multiple browsers
Documentation
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use super::{Action, JsonBookmark, SourceFolder, Status};
use crate::{cache::CacheMode, SourceBookmarks, SourceType, UnderlyingType};
use chrono::{DateTime, Utc};
use log::debug;
use std::collections::{
    hash_map::{Entry, IntoIter, IntoValues, Iter, IterMut, Keys, Values, ValuesMut},
    HashMap, HashSet,
};
use url::Url;
use uuid::Uuid;

/// A standardized bookmark for internal bookkeeping that is created from the
/// [`SourceBookmarks`].
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct TargetBookmark {
    /// Unique id of the bookmark used for caching.
    pub id: String,
    /// The url of the bookmark.
    pub url: Url,
    /// The url of the underlying for supported `UnderlyingType`s.
    pub underlying_url: Option<Url>,
    /// The type of the underlying.
    pub underlying_type: UnderlyingType,
    /// The timestamp in milliseconds when the bookmark was imported from the source.
    pub last_imported: i64,
    /// The timestamp in milliseconds when the bookmark was added to the cache.
    pub last_cached: Option<i64>,
    /// The source or sources this bookmark was imported from.
    pub sources: HashSet<SourceType>,
    /// The folder locations from which this bookmark was imported.
    pub source_folders: HashSet<SourceFolder>,
    /// The file format for the cached bookmark.
    pub cache_modes: HashSet<CacheMode>,
    /// The status of an imported bookmark.
    pub status: Status,
    /// The action performed when processing [`TargetBookmark`] in
    /// `BookmarkProcessor`.
    pub action: Action,
}

impl TargetBookmark {
    pub fn builder(url: Url, last_imported: DateTime<Utc>) -> TargetBookmarkBuilder {
        TargetBookmarkBuilder::new(url, last_imported)
    }

    pub fn builder_with_id(
        id: String,
        url: Url,
        last_imported: DateTime<Utc>,
    ) -> TargetBookmarkBuilder {
        TargetBookmarkBuilder::new_with_id(id, url, last_imported)
    }

    pub fn new(url: Url, last_imported: DateTime<Utc>) -> Self {
        let underlying_type = UnderlyingType::from(&url);

        Self {
            id: Uuid::new_v4().to_string(),
            url,
            underlying_url: None,
            underlying_type,
            last_imported: last_imported.timestamp_millis(),
            last_cached: None,
            sources: HashSet::new(),
            source_folders: HashSet::new(),
            cache_modes: HashSet::new(),
            status: Status::None,
            action: Action::None,
        }
    }

    pub fn id(&self) -> &str {
        self.id.as_ref()
    }

    pub fn url(&self) -> &Url {
        &self.url
    }

    pub fn underlying_url(&self) -> Option<&Url> {
        self.underlying_url.as_ref()
    }

    pub fn underlying_type(&self) -> &UnderlyingType {
        &self.underlying_type
    }

    pub fn last_imported(&self) -> i64 {
        self.last_imported
    }

    pub fn last_cached(&self) -> Option<i64> {
        self.last_cached
    }

    pub fn status(&self) -> &Status {
        &self.status
    }

    pub fn action(&self) -> &Action {
        &self.action
    }

    pub fn sources(&self) -> &HashSet<SourceType> {
        &self.sources
    }

    pub fn cache_modes(&self) -> &HashSet<CacheMode> {
        &self.cache_modes
    }

    pub fn set_url(&mut self, url: Url) {
        self.url = url;
    }

    pub fn set_underlying_url(&mut self, underlying_url: Url) {
        self.underlying_url = Some(underlying_url);
    }

    pub fn set_last_imported(&mut self, last_imported: DateTime<Utc>) {
        self.last_imported = last_imported.timestamp_millis();
    }

    pub fn set_last_cached(&mut self, last_cached: DateTime<Utc>) {
        self.last_cached = Some(last_cached.timestamp_millis());
    }

    pub fn unset_last_cached(&mut self) {
        self.last_cached = None;
    }

    pub fn set_status(&mut self, status: Status) {
        self.status = status;
    }

    pub fn set_action(&mut self, action: Action) {
        self.action = action;
    }

    pub fn add_source(&mut self, source: SourceType) {
        self.sources.insert(source);
    }

    pub fn add_cache_mode(&mut self, cache_mode: CacheMode) {
        self.cache_modes.insert(cache_mode);
    }

    pub fn remove_cache_mode(&mut self, cache_mode: &CacheMode) {
        self.cache_modes.remove(cache_mode);
    }

    pub fn clear_cache_mode(&mut self) {
        self.cache_modes.clear();
    }
}

pub struct TargetBookmarkBuilder {
    id: String,
    url: Url,
    underlying_url: Option<Url>,
    last_imported: DateTime<Utc>,
    last_cached: Option<DateTime<Utc>>,
    sources: HashSet<SourceType>,
    source_folders: HashSet<SourceFolder>,
    cache_modes: HashSet<CacheMode>,
    status: Status,
    action: Action,
}

impl TargetBookmarkBuilder {
    pub fn new(url: Url, last_imported: DateTime<Utc>) -> TargetBookmarkBuilder {
        TargetBookmarkBuilder {
            id: Uuid::new_v4().to_string(),
            url,
            underlying_url: None,
            last_imported,
            last_cached: None,
            sources: HashSet::new(),
            source_folders: HashSet::new(),
            cache_modes: HashSet::new(),
            status: Status::None,
            action: Action::None,
        }
    }

    pub fn new_with_id(
        id: String,
        url: Url,
        last_imported: DateTime<Utc>,
    ) -> TargetBookmarkBuilder {
        TargetBookmarkBuilder {
            id,
            url,
            underlying_url: None,
            last_imported,
            last_cached: None,
            sources: HashSet::new(),
            source_folders: HashSet::new(),
            cache_modes: HashSet::new(),
            status: Status::None,
            action: Action::None,
        }
    }

    pub fn with_status(mut self, status: Status) -> TargetBookmarkBuilder {
        self.status = status;
        self
    }

    pub fn with_action(mut self, action: Action) -> TargetBookmarkBuilder {
        self.action = action;
        self
    }

    pub fn with_sources(mut self, sources: HashSet<SourceType>) -> TargetBookmarkBuilder {
        self.sources = sources;
        self
    }

    pub fn with_folders(mut self, folders: HashSet<SourceFolder>) -> TargetBookmarkBuilder {
        self.source_folders = folders;
        self
    }

    pub fn add_source(mut self, source: SourceType) -> TargetBookmarkBuilder {
        self.sources.insert(source);
        self
    }

    pub fn add_cache_mode(mut self, cache_mode: CacheMode) -> TargetBookmarkBuilder {
        self.cache_modes.insert(cache_mode);
        self
    }

    pub fn build(self) -> TargetBookmark {
        let underlying_type = UnderlyingType::from(&self.url);

        TargetBookmark {
            id: self.id,
            url: self.url,
            underlying_url: self.underlying_url,
            underlying_type,
            last_imported: self.last_imported.timestamp_millis(),
            last_cached: self
                .last_cached
                .map(|timestamp| timestamp.timestamp_millis()),
            sources: self.sources,
            source_folders: self.source_folders,
            cache_modes: self.cache_modes,
            status: self.status,
            action: self.action,
        }
    }
}

impl TryFrom<JsonBookmark> for TargetBookmark {
    type Error = anyhow::Error;

    fn try_from(value: JsonBookmark) -> Result<Self, anyhow::Error> {
        let url = Url::parse(&value.url)?;
        let underlying_type = UnderlyingType::from(&url);

        Ok(Self {
            id: value.id,
            url,
            underlying_url: None,
            underlying_type,
            last_imported: value.last_imported,
            last_cached: value.last_cached,
            sources: value.sources,
            source_folders: HashSet::new(),
            cache_modes: value.cache_modes,
            status: Status::None,
            action: Action::None,
        })
    }
}

/// A wrapper for a collection of [`TargetBookmark`]s that is stored in the
/// `bookmarks.json` file.
#[derive(Debug, PartialEq, Eq, Default)]
pub struct TargetBookmarks(HashMap<Url, TargetBookmark>);

impl TargetBookmarks {
    pub fn new(bookmarks: HashMap<Url, TargetBookmark>) -> Self {
        Self(bookmarks)
    }

    pub fn inner(self) -> HashMap<Url, TargetBookmark> {
        self.0
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn get(&self, url: &Url) -> Option<&TargetBookmark> {
        self.0.get(url)
    }

    pub fn get_mut(&mut self, url: &Url) -> Option<&mut TargetBookmark> {
        self.0.get_mut(url)
    }

    pub fn keys(&self) -> Keys<Url, TargetBookmark> {
        self.0.keys()
    }

    pub fn values(&self) -> Values<Url, TargetBookmark> {
        self.0.values()
    }

    pub fn values_mut(&mut self) -> ValuesMut<Url, TargetBookmark> {
        self.0.values_mut()
    }

    pub fn into_values(self) -> IntoValues<Url, TargetBookmark> {
        self.0.into_values()
    }

    pub fn contains_key(&self, url: &Url) -> bool {
        self.0.contains_key(url)
    }

    pub fn iter(&self) -> Iter<Url, TargetBookmark> {
        self.0.iter()
    }

    pub fn iter_mut(&mut self) -> IterMut<Url, TargetBookmark> {
        self.0.iter_mut()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// If the cache was removed, reset the cache values in the target
    /// bookmarks.
    pub fn reset_cache_status(&mut self) {
        debug!("Reset cache status");
        for bookmark in self.values_mut() {
            bookmark.last_cached = None;
            bookmark.cache_modes.clear();
        }
    }

    pub fn set_action(&mut self, action: &Action) {
        debug!("Set action to {action:#?}");

        for bookmark in self.values_mut() {
            bookmark.action = action.clone()
        }
    }

    pub fn insert(&mut self, bookmark: TargetBookmark) -> Option<TargetBookmark> {
        self.0.insert(bookmark.url.clone(), bookmark)
    }

    pub fn upsert(&mut self, bookmark: TargetBookmark) {
        let url = &bookmark.url;
        let entry = self.0.entry(url.clone());

        match entry {
            Entry::Occupied(entry) => {
                let url = entry.key().clone();
                let target_bookmark = entry.into_mut();
                debug!("Overwrite duplicate target bookmark: {}", url);

                // We are keeping the existing id and url, but overwriting all other fields.
                target_bookmark.last_imported = bookmark.last_imported;
                target_bookmark.last_cached = bookmark.last_cached;

                for source in bookmark.sources {
                    target_bookmark.sources.insert(source);
                }

                target_bookmark.cache_modes = bookmark.cache_modes;
                target_bookmark.action = bookmark.action;
            }
            Entry::Vacant(entry) => {
                let inserted_bookmark = entry.insert(bookmark);
                inserted_bookmark.status = Status::Added;
            }
        }
    }

    pub fn remove(&mut self, url: &Url) -> Option<TargetBookmark> {
        self.0.remove(url)
    }
}

impl IntoIterator for TargetBookmarks {
    type Item = (Url, TargetBookmark);
    type IntoIter = IntoIter<Url, TargetBookmark>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl TryFrom<SourceBookmarks> for TargetBookmarks {
    type Error = anyhow::Error;

    fn try_from(source_bookmarks: SourceBookmarks) -> Result<Self, Self::Error> {
        let now = Utc::now();
        let mut target_bookmarks = TargetBookmarks::default();

        for source_bookmark in source_bookmarks.into_iter() {
            let url = Url::parse(&source_bookmark.0)?;
            let sources = source_bookmark.1.sources_owned();
            let target_bookmark = TargetBookmarkBuilder::new(url.to_owned(), now)
                .with_sources(sources)
                .build();
            target_bookmarks.insert(target_bookmark);
        }

        Ok(target_bookmarks)
    }
}