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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
mod bookmark_manager;
mod bookmark_service;
mod source_bookmarks;
mod target_bookmarks;

use crate::CacheMode;
pub use bookmark_manager::BookmarkManager;
pub use bookmark_service::{BookmarkService, ServiceConfig};
use serde::{Deserialize, Serialize};
pub use source_bookmarks::{SourceBookmark, SourceBookmarkBuilder, SourceBookmarks};
use std::{
    cmp::Ordering,
    collections::HashSet,
    fmt,
    path::{Path, PathBuf},
    slice::Iter,
};
pub use target_bookmarks::{TargetBookmark, TargetBookmarkBuilder, TargetBookmarks};
use url::Url;
use uuid::Uuid;

pub const HACKER_NEWS_DOMAINS: &[&str] = &["news.ycombinator.com", "www.news.ycombinator.com"];
pub const REDDIT_DOMAINS: &[&str] = &["reddit.com", "www.reddit.com"];

/// The supported domains to fetch the underlying.
pub const SUPPORTED_UNDERLYING_DOMAINS: &[&str] = &[
    "news.ycombinator.com",
    "www.news.ycombinator.com",
    "reddit.com",
    "www.reddit.com",
];

/// The type used to identify a source.
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum SourceType {
    Firefox,
    ChromiumDerivative,
    Chromium,
    Chrome,
    Edge,
    Safari,
    Simple,
    Underlying(String),
    Internal,
    External,
    #[default]
    Unknown,
}

impl fmt::Display for SourceType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let reader_name = match &self {
            SourceType::Firefox => "Firefox",
            SourceType::ChromiumDerivative => "Chromium (derivative)",
            SourceType::Chromium => "Chromium",
            SourceType::Chrome => "Chrome",
            SourceType::Edge => "Edge",
            SourceType::Safari => "Safari",
            SourceType::Simple => "Simple",
            SourceType::Underlying(_) => "Underlying",
            SourceType::Internal => "Internal",
            SourceType::External => "External",
            SourceType::Unknown => "Unknown",
        };
        write!(f, "{}", reader_name)
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Status {
    /// Bookmark added to state.
    Added,
    /// Bookmark removed from state.
    Removed,
    None,
}

/// The action to be performed on the bookmark.
///
/// `Actions`s includes external resources, like cache and fetching bookmarks
/// from the web.
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum Action {
    /// Fetch and cache the bookmark, even if it is cached already. The cached
    /// content will be replaced with the most recent version of the website.
    FetchAndReplace,
    /// Fetch and cache bookmark if it is not cached yet.
    FetchAndAdd,
    /// Fetch a bookmark and diff the fetched content with the cached content.
    FetchAndDiff,
    /// Remove a bookmark from the cache.
    Remove,
    /// Remove a bookmark from the cache for all `CacheMode`s.
    RemoveAll,
    /// No actions to be performed.
    None,
    /// Skip fetching, caching, and writing to file.
    DryRun,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum UnderlyingType {
    HackerNews,
    Reddit,
    None,
}

impl From<&Url> for UnderlyingType {
    fn from(url: &Url) -> Self {
        if url
            .domain()
            .is_some_and(|domain| HACKER_NEWS_DOMAINS.contains(&domain))
        {
            UnderlyingType::HackerNews
        } else if url
            .domain()
            .is_some_and(|domain| REDDIT_DOMAINS.contains(&domain))
        {
            UnderlyingType::Reddit
        } else {
            UnderlyingType::None
        }
    }
}

#[derive(Debug, Default, PartialEq, Clone)]
pub enum RunMode {
    /// Import bookmarks, but don't fetch them.
    Import,
    /// Add provided bookmark urls.
    AddUrls(Vec<Url>),
    /// Remove provided bookmark urls.
    RemoveUrls(Vec<Url>),
    /// Remove bookmarks from cache.
    Remove,
    /// Remove bookmarks from cache for all `CacheMode`s.
    RemoveAll,
    /// Import and fetch provided bookmark urls.
    FetchUrls(Vec<Url>),
    /// Import, fetch and replace provided bookmark urls.
    FetchAllUrls(Vec<Url>),
    /// Fetch bookmarks if not fetched yet.
    Fetch,
    /// Fetch and replace bookmarks.
    FetchAll,
    /// Fetch diff for provided bookmark urls.
    FetchDiff(Vec<Url>),
    /// Sync bookmarks.
    Sync,
    /// Run in dry mode.
    DryRun,
    #[default]
    None,
}

/// The source of bookmarks.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct RawSource {
    /// The path to the source file or directory.
    #[serde(rename = "source")]
    pub path: PathBuf,
    /// The folders to be imported.
    ///
    /// If no folders are selected, all bookmarks in the source file will be
    /// imported.
    pub folders: Vec<String>,
}

impl RawSource {
    pub fn new(path: impl Into<PathBuf>, folders: Vec<String>) -> Self {
        Self {
            path: path.into(),
            folders,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Source {
    /// The name of the source.
    pub source_type: SourceType,
    /// The path of the source file used for logging.
    pub path: PathBuf,
    /// The folders to be imported.
    ///
    /// If no folders are selected, all bookmarks in the source file will be
    /// imported.
    pub folders: Vec<String>,
}

impl Source {
    pub fn new(source_type: SourceType, path: &Path, folders: Vec<String>) -> Self {
        Self {
            source_type,
            path: path.to_owned(),
            folders,
        }
    }
}

/// The source folder of a bookmark.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SourceFolder {
    source: SourceType,
    name: String,
}

impl SourceFolder {
    pub fn new(source: SourceType, name: String) -> Self {
        Self { source, name }
    }

    pub fn source(&self) -> &SourceType {
        &self.source
    }

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

#[derive(Debug, Serialize, PartialEq, Deserialize)]
pub struct JsonBookmark {
    pub id: String,
    pub url: String,
    pub last_imported: i64,
    pub last_cached: Option<i64>,
    pub sources: HashSet<SourceType>,
    pub cache_modes: HashSet<CacheMode>,
}

impl JsonBookmark {
    pub fn new(
        url: String,
        last_imported: i64,
        last_cached: Option<i64>,
        sources: HashSet<SourceType>,
        cache_modes: HashSet<CacheMode>,
    ) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            url,
            last_imported,
            last_cached,
            sources,
            cache_modes,
        }
    }
}

impl From<TargetBookmark> for JsonBookmark {
    fn from(value: TargetBookmark) -> Self {
        Self {
            id: value.id().to_owned(),
            url: value.url().to_string(),
            last_imported: value.last_imported(),
            last_cached: value.last_cached(),
            sources: value.sources().to_owned(),
            cache_modes: value.cache_modes().to_owned(),
        }
    }
}

impl From<&TargetBookmark> for JsonBookmark {
    fn from(value: &TargetBookmark) -> Self {
        Self {
            id: value.id().to_owned(),
            url: value.url().to_string(),
            last_imported: value.last_imported(),
            last_cached: value.last_cached(),
            sources: value.sources().clone(),
            cache_modes: value.cache_modes().clone(),
        }
    }
}

#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct JsonBookmarks {
    pub bookmarks: Vec<JsonBookmark>,
}

impl JsonBookmarks {
    pub fn new(bookmarks: Vec<TargetBookmark>) -> Self {
        let mut bookmarks = bookmarks
            .into_iter()
            .map(JsonBookmark::from)
            .collect::<Vec<_>>();
        bookmarks.sort_by(Self::compare);

        Self { bookmarks }
    }

    pub fn iter(&self) -> Iter<JsonBookmark> {
        self.bookmarks.iter()
    }

    pub fn get(&self, index: usize) -> Option<&JsonBookmark> {
        self.bookmarks.get(index)
    }

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

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

    // Sort by `last_cached` and then by `url`.
    fn compare(a: &JsonBookmark, b: &JsonBookmark) -> Ordering {
        match (a.last_cached, b.last_cached) {
            (Some(a_cached), Some(b_cached)) => {
                a_cached.cmp(&b_cached).then_with(|| a.url.cmp(&b.url))
            }
            (Some(_), None) => Ordering::Less,
            (None, Some(_)) => Ordering::Greater,
            (None, None) => a.url.cmp(&b.url),
        }
    }
}

impl From<&TargetBookmarks> for JsonBookmarks {
    fn from(target_bookmarks: &TargetBookmarks) -> Self {
        let mut bookmarks = target_bookmarks
            .values()
            .filter(|bookmark| {
                if bookmark.action() == &Action::DryRun {
                    bookmark.status() == &Status::None
                } else {
                    true
                }
            })
            .map(JsonBookmark::from)
            .collect::<Vec<_>>();
        bookmarks.sort_by(Self::compare);
        JsonBookmarks { bookmarks }
    }
}

impl IntoIterator for JsonBookmarks {
    type Item = JsonBookmark;
    type IntoIter = std::vec::IntoIter<Self::Item>;

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

pub struct JsonBookmarksIterator<'a> {
    bookmarks_iter: Iter<'a, JsonBookmark>,
}

impl<'a> IntoIterator for &'a JsonBookmarks {
    type Item = &'a JsonBookmark;
    type IntoIter = JsonBookmarksIterator<'a>;

    fn into_iter(self) -> Self::IntoIter {
        JsonBookmarksIterator {
            bookmarks_iter: self.bookmarks.iter(),
        }
    }
}

impl<'a> Iterator for JsonBookmarksIterator<'a> {
    type Item = &'a JsonBookmark;

    fn next(&mut self) -> Option<Self::Item> {
        self.bookmarks_iter.next()
    }
}

#[derive(Debug, Default)]
pub struct ServiceReport {
    total: usize,
    processed: i32,
    cached: i32,
    failed_response: i32,
    binary_response: i32,
    empty_response: i32,
    dry_run: bool,
}

impl ServiceReport {
    pub fn new(
        total: usize,
        processed: i32,
        cached: i32,
        failed_response: i32,
        binary_response: i32,
        empty_response: i32,
        dry_run: bool,
    ) -> Self {
        Self {
            total,
            processed,
            cached,
            failed_response,
            binary_response,
            empty_response,
            dry_run,
        }
    }

    pub fn init(dry_run: bool) -> Self {
        Self::new(0, 0, 0, 0, 0, 0, dry_run)
    }

    pub fn print(&self) {
        print!("Processing bookmarks ({}/{})\r", self.processed, self.total);
    }

    pub fn print_summary(&self) {
        if self.total == 0 {
            println!("Processing bookmarks (0/0)");
        } else {
            println!();
        }

        if self.dry_run {
            println!(
                "Processed {} bookmarks, {} cached, {} ignored, {} failed (dry run)",
                self.total, 0, 0, 0
            );
        } else {
            println!(
                "Processed {} bookmarks, {} cached, {} ignored, {} failed",
                self.total,
                self.cached,
                self.binary_response + self.empty_response,
                self.failed_response,
            );
        }
    }

    pub fn reset(&mut self) {
        self.total = 0;
        self.processed = 0;
        self.cached = 0;
        self.failed_response = 0;
        self.binary_response = 0;
        self.empty_response = 0;
    }

    pub fn set_total(&mut self, total: usize) {
        self.total = total;
    }

    pub fn increment_processed(&mut self) {
        self.processed += 1;
    }

    pub fn increment_cached(&mut self) {
        self.cached += 1;
    }

    pub fn increment_failed_response(&mut self) {
        self.failed_response += 1;
    }

    pub fn increment_binary_response(&mut self) {
        self.binary_response += 1;
    }

    pub fn increment_empty_response(&mut self) {
        self.empty_response += 1;
    }
}