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
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 `Settungs::max_concurrent_requests`.
const MAX_CONCURRENT_REQUESTS_DEFAULT: usize = 100;

/// 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 = 10;

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

/// 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 concurrent requests.
    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.
    pub max_idle_connections_per_host: usize,
    /// The timeout for idle connections to be kept alive in milliseconds.
    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_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_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_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)?;
            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: Option<CacheMode>) {
        if let Some(cache_mode) = cache_mode {
            debug!("Set cache mode to {}", cache_mode);
            self.cache_mode = cache_mode;
        }
    }
}

#[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(Some(CacheMode::Html));
        assert_eq!(
            settings,
            Settings {
                cache_mode: CacheMode::Html,
                ..Default::default()
            }
        );
    }
}