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
use crate::{
    bookmarks::{RawSource, SUPPORTED_UNDERLYING_DOMAINS},
    cache::CacheMode,
    json,
};
use anyhow::{anyhow, Context};
use log::debug;
use reqwest::Url;
use serde::{Deserialize, Serialize};
use std::{
    fs::File,
    io::{Read, Write},
    path::Path,
};

/// The default for `Settungs::max_open_files`.
const MAX_OPEN_FILES_DEFAULT: u64 = 500;

/// The default for `Settungs::max_concurrent_requests`.
const MAX_CONCURRENT_REQUESTS_DEFAULT: usize = 500;

/// The default for `Settings::request_timeout`.
const REQUEST_TIMEOUT_DEFAULT: u64 = 60_000;

/// The default for `Settings::request_throttling`.
const REQUEST_THROTTLING_DEFAULT: u64 = 3_000;

/// The  default for `Setting::max_idle_connections_per_host`.
const MAX_IDLE_CONNECTIONS_PER_HOST: usize = 1;

/// The  default for `Setting::idle_connections_timeout`.
const IDLE_CONNECTIONS_TIMEOUT: u64 = 5_000;

/// Optional settings configured via `ConfigArgs`.
#[derive(Debug, PartialEq, Clone, Default)]
pub struct SettingsArgs {
    /// The paths to the configured bookmark files.
    ///
    /// Source could be Firefox or Chrome.
    pub source: Option<RawSource>,
    /// The urls which are ignored and not imported.
    pub ignored_urls: Vec<String>,
    /// Fetch the underlying for the given urls.
    pub underlying_urls: Vec<String>,
    /// The file extension used to cache websites.
    pub cache_mode: Option<CacheMode>,
    /// The maximal number of open files used to write the fetched content to
    /// disk.
    pub max_open_files: Option<u64>,
    /// The maximal number of concurrent requests.
    pub max_concurrent_requests: Option<usize>,
    /// The request timeout in milliseconds.
    pub request_timeout: Option<u64>,
    /// The throttling between requests in milliseconds.
    pub request_throttling: Option<u64>,
    /// The maximum number of idle connections allowed in the connection pool of
    /// the client.
    pub max_idle_connections_per_host: Option<usize>,
    /// The timeout for idle connections to be kept alive in milliseconds.
    pub idle_connections_timeout: Option<u64>,
}

impl SettingsArgs {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        source: Option<RawSource>,
        ignored_urls: Vec<String>,
        underlying_urls: Vec<String>,
        cache_mode: Option<CacheMode>,
        max_open_files: Option<u64>,
        max_concurrent_requests: Option<usize>,
        request_timeout: Option<u64>,
        request_throttling: Option<u64>,
        max_idle_connections_per_host: Option<usize>,
        idle_connections_timeout: Option<u64>,
    ) -> Self {
        Self {
            source,
            ignored_urls,
            underlying_urls,
            cache_mode,
            max_open_files,
            max_concurrent_requests,
            request_timeout,
            request_throttling,
            max_idle_connections_per_host,
            idle_connections_timeout,
        }
    }
}

/// Describes the settings used in Bogrep.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Settings {
    /// The paths to the configured bookmark files.
    ///
    /// Source could be Firefox or Chrome.
    pub sources: Vec<RawSource>,
    /// The urls which are ignored and not imported.
    pub ignored_urls: Vec<String>,
    /// Fetch the underlying for the given urls.
    pub underlying_urls: Vec<String>,
    /// The file extension used to cache websites.
    pub cache_mode: CacheMode,
    /// The maximal number of open files used to write the fetched content to
    /// disk.
    ///
    /// As the number of file descriptors (i.e. open files + network sockets) is
    /// limited by the operating system, the sum of `max_open_files` and
    /// `max_concurrent_requests` will be set as file descriptor limit.
    ///
    /// On macOS, the default for the soft file descriptor limit is 256. On
    /// Linux, the soft file descriptor limit usually is 1024 by default.
    pub max_open_files: u64,
    /// The maximal number of concurrent requests used to fetch the content from
    /// the bookmarked websites.
    ///
    /// As the number of file descriptors (i.e. open files + network sockets) is
    /// limited by the operating system, the sum of `max_open_files` and
    /// `max_concurrent_requests` will be set as file descriptor limit.
    ///
    /// On macOS, the default for the soft file descriptor limit is 256. On
    /// Linux, the soft file descriptor limit usually is 1024 by default.
    pub max_concurrent_requests: usize,
    /// The request timeout in milliseconds.
    pub request_timeout: u64,
    /// The throttling between requests in milliseconds.
    pub request_throttling: u64,
    /// The maximum number of idle connections allowed in the connection pool of
    /// the client.
    ///
    /// As the maximum number of open file descriptors (i.e. open files +
    /// network sockets) are limited by the operating system,
    /// `max_idle_connections_per_host` should be configured to 1.
    ///
    /// Choose sensible values for `max_idle_connections_per_host` and
    /// `idle_connections_timeout` to fix "Too many open files" and DNS errors
    /// (rate limit for DNS server).
    pub max_idle_connections_per_host: usize,
    /// The timeout for idle connections to be kept alive in milliseconds.
    ///
    /// Choose sensible values for `max_idle_connections_per_host` and
    /// `idle_connections_timeout` to fix "Too many open files" and DNS errors
    /// (rate limit for DNS server).
    pub idle_connections_timeout: u64,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            sources: Vec::new(),
            ignored_urls: Vec::new(),
            underlying_urls: Vec::new(),
            cache_mode: CacheMode::default(),
            max_open_files: MAX_OPEN_FILES_DEFAULT,
            max_concurrent_requests: MAX_CONCURRENT_REQUESTS_DEFAULT,
            request_timeout: REQUEST_TIMEOUT_DEFAULT,
            request_throttling: REQUEST_THROTTLING_DEFAULT,
            max_idle_connections_per_host: MAX_IDLE_CONNECTIONS_PER_HOST,
            idle_connections_timeout: IDLE_CONNECTIONS_TIMEOUT,
        }
    }
}

impl Settings {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        sources: Vec<RawSource>,
        ignored_urls: Vec<String>,
        underlying_urls: Vec<String>,
        cache_mode: CacheMode,
        max_open_files: u64,
        max_concurrent_requests: usize,
        request_timeout: u64,
        request_throttling: u64,
        max_idle_connections_per_host: usize,
        idle_connections_timeout: u64,
    ) -> Self {
        Self {
            sources,
            ignored_urls,
            underlying_urls,
            cache_mode,
            max_open_files,
            max_concurrent_requests,
            request_timeout,
            request_throttling,
            max_idle_connections_per_host,
            idle_connections_timeout,
        }
    }

    pub fn init(settings_path: &Path) -> Result<Settings, anyhow::Error> {
        if settings_path.exists() {
            debug!("Reading settings file at {}", settings_path.display());
            let mut buf = Vec::new();
            let mut settings_file = File::open(settings_path)?;
            settings_file
                .read_to_end(&mut buf)
                .context("Can't read `settings.json` file")?;
            let settings = json::deserialize::<Settings>(&buf)?;
            Ok(settings)
        } else {
            debug!("Create settings file at {}", settings_path.display());
            let settings = Settings::default();
            let settings_json = json::serialize(&settings)?;
            let mut settings_file = File::create(settings_path).context(format!(
                "Can't create `settings.json` file: {}",
                settings_path.display()
            ))?;
            settings_file.write_all(&settings_json)?;
            settings_file.flush()?;

            Ok(settings)
        }
    }

    pub fn add_ignored_url(&mut self, url: &str) -> Result<(), anyhow::Error> {
        let url = Url::parse(url).context(format!("Invalid url {url}"))?;
        let normalized_url = url.to_string();

        if self.ignored_urls.iter().any(|url| *url == normalized_url) {
            return Err(anyhow!("Duplicate url: {url}"));
        }

        self.ignored_urls.push(url.to_string());

        Ok(())
    }

    pub fn add_underlying_url(&mut self, url: &str) -> Result<(), anyhow::Error> {
        let url = Url::parse(url).context(format!("Invalid url {url}"))?;
        let normalized_url = url.to_string();
        let domain = url.domain().ok_or(anyhow!(format!("Invalid url {url}")))?;

        if !SUPPORTED_UNDERLYING_DOMAINS.contains(&domain) {
            return Err(anyhow!("Underlying not supported for {url}"));
        }

        if self
            .underlying_urls
            .iter()
            .any(|url| *url == normalized_url)
        {
            return Err(anyhow!("Duplicate url: {url}"));
        }

        self.underlying_urls.push(normalized_url);

        Ok(())
    }

    pub fn set_source(&mut self, source: RawSource) -> Result<(), anyhow::Error> {
        debug!("Set source to {}", source.path.display());

        if let Some(s) = self.sources.iter_mut().find(|s| s.path == source.path) {
            *s = source;
        } else {
            self.sources.push(source);
        }

        Ok(())
    }

    pub fn set_cache_mode(&mut self, cache_mode: CacheMode) {
        debug!("Set cache mode to {}", cache_mode);
        self.cache_mode = cache_mode;
    }

    pub fn set_max_open_files(&mut self, max_open_files: u64) {
        debug!("Set `max_open_files` to {max_open_files}");
        self.max_open_files = max_open_files;
    }

    pub fn set_request_timeout(&mut self, request_timeout: u64) {
        debug!("Set `request_timeout` to {request_timeout}");
        self.request_timeout = request_timeout;
    }

    pub fn set_request_throttling(&mut self, request_throttling: u64) {
        debug!("Set `request_throttling` to {request_throttling}");
        self.request_throttling = request_throttling;
    }

    pub fn set_max_concurrent_requests(&mut self, max_concurrent_requests: usize) {
        debug!("Set `max_concurrent_requests` to {max_concurrent_requests}");
        self.max_concurrent_requests = max_concurrent_requests;
    }

    pub fn set_max_idle_connections_per_host(&mut self, max_idle_connections_per_host: usize) {
        debug!("Set `max_idle_connections_per_host` to {max_idle_connections_per_host}");
        self.max_idle_connections_per_host = max_idle_connections_per_host;
    }

    pub fn set_idle_connections_timeout(&mut self, idle_connections_timeout: u64) {
        debug!("Set `idle_connections_timeout` to {idle_connections_timeout}");
        self.idle_connections_timeout = idle_connections_timeout;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bookmarks::{HACKER_NEWS_DOMAINS, REDDIT_DOMAINS};
    use std::path::PathBuf;

    #[test]
    fn test_add_ignored_urls() {
        let mut settings = Settings::default();
        assert!(settings.ignored_urls.is_empty());
        let urls = vec!["https://youtube.com/", "https://soundcloud.com/"];

        for url in urls {
            let res = settings.add_ignored_url(url);
            assert!(res.is_ok(), "{}", res.unwrap_err());
        }

        assert_eq!(
            settings.ignored_urls,
            vec![
                String::from("https://youtube.com/"),
                String::from("https://soundcloud.com/"),
            ]
        );
    }

    #[test]
    fn test_add_ignored_urls_duplicate() {
        let mut settings = Settings::default();
        let url = "https://youtube.com/";
        let res = settings.add_ignored_url(url);
        assert!(res.is_ok());
        let res = settings.add_ignored_url(url);
        assert!(res.is_err());
    }

    #[test]
    fn test_add_ignored_urls_invalid() {
        let mut settings = Settings::default();
        let url = "youtube.com/";
        let res = settings.add_ignored_url(url);
        assert!(res.is_err());
    }

    #[test]
    fn test_add_underlying_urls_hackernews() {
        let mut settings = Settings::default();
        assert!(settings.underlying_urls.is_empty());
        let url = "https://news.ycombinator.com/item?id=00000000";

        let res = settings.add_underlying_url(url);
        assert!(res.is_ok(), "{}", res.unwrap_err());
        assert_eq!(settings.underlying_urls.len(), 1);
        let actual_url = Url::parse(&settings.underlying_urls[0]).unwrap();
        assert_eq!(actual_url, Url::parse(url).unwrap());
        assert!(HACKER_NEWS_DOMAINS.contains(&actual_url.domain().unwrap()));
    }

    #[test]
    fn test_add_underlying_urls_reddit() {
        let mut settings = Settings::default();
        assert!(settings.underlying_urls.is_empty());
        let url = "https://www.reddit.com/r/programming/comments/article_id";

        let res = settings.add_underlying_url(url);
        assert!(res.is_ok(), "{}", res.unwrap_err());
        assert_eq!(settings.underlying_urls.len(), 1);
        let actual_url = Url::parse(&settings.underlying_urls[0]).unwrap();
        assert_eq!(actual_url, Url::parse(url).unwrap());
        assert!(REDDIT_DOMAINS.contains(&actual_url.domain().unwrap()));
    }

    #[test]
    fn test_add_underlying_urls_duplicate() {
        let mut settings = Settings::default();
        let url = "https://news.ycombinator.com/";
        let res = settings.add_underlying_url(url);
        assert!(res.is_ok());
        let res = settings.add_underlying_url(url);
        assert!(res.is_err());
    }

    #[test]
    fn test_add_underlying_urls_unsupported() {
        let mut settings = Settings::default();
        let url = "https://url.com";
        let res = settings.add_underlying_url(url);
        assert!(res.is_err());
    }

    #[test]
    fn test_set_source() {
        let raw_source = RawSource::new(PathBuf::from("path/to/source"), vec![]);
        let mut settings = Settings::default();
        let res = settings.set_source(raw_source.clone());
        assert!(res.is_ok());
        assert_eq!(
            settings,
            Settings {
                sources: vec![raw_source],
                ..Default::default()
            }
        );
    }

    #[test]
    fn test_set_source_overwrite() {
        let raw_source = RawSource::new(PathBuf::from("path/to/source"), vec![]);
        let raw_source_with_folders = RawSource::new(
            PathBuf::from("path/to/source"),
            vec!["dev".to_string(), "articles".to_string()],
        );
        let mut settings = Settings::default();
        settings.set_source(raw_source.clone()).unwrap();
        assert_eq!(
            settings,
            Settings {
                sources: vec![raw_source],
                ..Default::default()
            }
        );

        settings
            .set_source(raw_source_with_folders.clone())
            .unwrap();
        assert_eq!(
            settings,
            Settings {
                sources: vec![raw_source_with_folders],
                ..Default::default()
            }
        );
    }

    #[test]
    fn test_set_source_and_folders() {
        let raw_source_with_folders = RawSource::new(
            PathBuf::from("path/to/source"),
            vec!["dev".to_string(), "articles".to_string()],
        );
        let mut settings = Settings::default();
        settings
            .set_source(raw_source_with_folders.clone())
            .unwrap();
        assert_eq!(
            settings,
            Settings {
                sources: vec![raw_source_with_folders],
                ..Default::default()
            }
        );
    }

    #[test]
    fn test_set_cache_mode() {
        let mut settings = Settings::default();
        settings.set_cache_mode(CacheMode::Html);
        assert_eq!(
            settings,
            Settings {
                cache_mode: CacheMode::Html,
                ..Default::default()
            }
        );
    }
}