openring 0.5.9

A webring for static site generators written in Rust
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use std::{
    cmp::Ordering,
    fs,
    io::{BufReader, BufWriter},
    path::{Path, PathBuf},
    time::Duration,
};

use dashmap::DashMap;
use directories::ProjectDirs;
use jiff::{Span, Timestamp, ToSpan};
use serde::{Deserialize, Serialize};
use tracing::{info, warn};
use url::Url;

use crate::{args::Args, error::Result};

const MAX_SPAN_SEC: i64 = 631_107_417_600;

/// Options for loading cache
#[derive(Copy, Clone, Debug)]
pub(crate) enum CachePath<'a> {
    Default,
    #[allow(dead_code)]
    Path(&'a Path),
}

/// Describes a feed fetch result that can be serialized to disk
#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct CacheValue {
    pub(crate) timestamp: Timestamp,
    pub(crate) retry_after: Option<Span>,
    pub(crate) last_modified: Option<String>,
    pub(crate) etag: Option<String>,
    pub(crate) body: Option<String>,
}

/// Get the path to cache location.
pub(crate) fn get_cache_path() -> Option<PathBuf> {
    if let Some(proj_dirs) = ProjectDirs::from("dev", "hsiao", "openring") {
        return Some(proj_dirs.cache_dir().join("cache.json"));
    }
    None
}

fn spans_equal(a: &Span, b: &Span) -> bool {
    // The spans we generate contain only time units, so `compare` never
    // needs a relative datetime and cannot error.
    a.compare(b)
        .expect("time‑only spans never require a relative datetime")
        == Ordering::Equal
}

impl PartialEq for CacheValue {
    fn eq(&self, other: &Self) -> bool {
        self.timestamp == other.timestamp
            && self.last_modified == other.last_modified
            && self.etag == other.etag
            && self.body == other.body
            && match (&self.retry_after, &other.retry_after) {
                (Some(a), Some(b)) => spans_equal(a, b),
                (None, None) => true,
                _ => false,
            }
    }
}
impl Eq for CacheValue {}

pub(crate) type Cache = DashMap<Url, CacheValue>;

pub(crate) trait StoreExt {
    /// Store the cache under the given path. Update access timestamps
    fn store<T: AsRef<Path>>(&self, path: T) -> Result<()>;

    /// Load cache from path. Discard entries older than `max_age_secs`
    fn load<T: AsRef<Path>>(path: T, max_age_secs: u64, now: Timestamp) -> Result<Cache>;
}

impl StoreExt for Cache {
    fn store<T: AsRef<Path>>(&self, path: T) -> Result<()> {
        // Ensure the parent directory exists
        let path = path.as_ref();
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        let f = fs::File::create(path)?;
        // Grab a lock to avoid multiple processes writing simultaneously
        f.lock()?;
        let w = BufWriter::new(f);
        serde_json::to_writer_pretty(w, self)?;
        Ok(())
    }

    fn load<T: AsRef<Path>>(path: T, max_age_secs: u64, now: Timestamp) -> Result<Cache> {
        let clamped_secs: i64 = max_age_secs.min(MAX_SPAN_SEC as u64).cast_signed();

        let f = fs::File::open(path)?;

        // Acquire a shared lock so multiple readers can coexist, but no writers
        f.lock_shared()?;

        let r = BufReader::new(f);

        let map: DashMap<Url, CacheValue> = serde_json::from_reader(r)?;

        // Remove entries older than max_age_secs
        let current_ts = now;
        let threshold = clamped_secs.seconds();
        let keys_to_remove: Vec<Url> = map
            .iter()
            .filter_map(|entry| {
                let v = entry.value();
                if (current_ts - v.timestamp).compare(threshold).ok()? == std::cmp::Ordering::Less {
                    None
                } else {
                    Some(entry.key().clone())
                }
            })
            .collect();

        for k in keys_to_remove {
            map.remove(&k);
        }

        Ok(map)
    }
}
/// Load cache (if exists and is still valid).
///
/// This returns an `Option` as starting without a cache is a common scenario
/// and we silently discard errors on purpose.
pub(crate) fn load_cache(args: &Args, cache_path: CachePath) -> Option<Cache> {
    if args.no_cache {
        return None;
    }
    let default_cache_path = get_cache_path();
    let cache_path = match cache_path {
        CachePath::Default if default_cache_path.is_none() => return None,
        CachePath::Default => default_cache_path.unwrap(),
        CachePath::Path(p) => p.to_path_buf(),
    };

    // Discard entire cache if it hasn't been updated since `max_cache_age`.
    // This is an optimization, which avoids iterating over the file and
    // checking the age of each entry.
    match fs::metadata(&cache_path) {
        Err(_e) => {
            // No cache found; silently start with empty cache
            return None;
        }
        Ok(metadata) => {
            let modified = metadata.modified().ok()?;
            let elapsed = modified.elapsed().ok()?;
            if elapsed > args.max_cache_age {
                warn!(
                    "Cache is too old (age: {:#?}, max age: {:#?}). Discarding and recreating.",
                    Duration::from_secs(elapsed.as_secs()),
                    Duration::from_secs(args.max_cache_age.as_secs())
                );
                return None;
            }
            info!(
                "Cache is recent (age: {:#?}, max age: {:#?}). Using.",
                Duration::from_secs(elapsed.as_secs()),
                Duration::from_secs(args.max_cache_age.as_secs())
            );
        }
    }

    let cache = Cache::load(cache_path, args.max_cache_age.as_secs(), Timestamp::now());
    match cache {
        Ok(cache) => Some(cache),
        Err(e) => {
            warn!("Error while loading cache: {e}. Continuing without.");
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{fs::File, thread::sleep, time::Duration as StdDuration};

    use jiff::{Span, Timestamp};
    use proptest::prelude::*;
    use tempfile::NamedTempFile;
    use tempfile::TempDir;
    use url::Url;

    use super::*;

    fn bounded_timestamp(secs: i64) -> Timestamp {
        let secs = secs.clamp(Timestamp::MIN.as_second(), Timestamp::MAX.as_second());
        Timestamp::from_second(secs).expect("failed to clamp timestamp")
    }

    // A random but well‑formed URL (http or https) – enough for the cache key.
    fn url_gen() -> impl Strategy<Value = Url> {
        // Simple host + path generator; you can extend it if you need more variety.
        ("https?://", "[a-z]{1,10}\\.[a-z]{2,5}", "/[a-z]{0,15}")
            .prop_map(|(scheme, host, path)| format!("{scheme}{host}{path}"))
            .prop_filter_map("valid URL", |s| Url::parse(&s).ok())
    }

    // Random `CacheValue`.  All fields are optional except `timestamp`.
    fn cache_value_gen() -> impl Strategy<Value = CacheValue> {
        let ts = any::<i64>().prop_map(bounded_timestamp);

        // `None`  – no retry‑after header
        // `Some` – a non‑negative span built from a random i64
        let retry_after = prop_oneof![
            // None branch
            Just(None),
            // Some branch
            any::<i64>()
                .prop_map(|secs| Span::new().seconds(secs.clamp(0, MAX_SPAN_SEC)))
                .prop_map(Some)
        ];

        let opt_string = ".*".prop_map(|s| if s.is_empty() { None } else { Some(s) });

        (
            ts,
            retry_after,
            opt_string.clone(),
            opt_string.clone(),
            opt_string,
        )
            .prop_map(
                |(timestamp, retry_after, last_modified, etag, body)| CacheValue {
                    timestamp,
                    retry_after,
                    last_modified,
                    etag,
                    body,
                },
            )
    }

    proptest! {
        // spans_equal behaves as expected for equal and different spans
        #[test]
        fn spans_equal_behavior(a_secs in 0i64..1_000_000i64, b_secs in 0i64..1_000_000i64) {
            let a = Span::new().seconds(a_secs);
            let b = Span::new().seconds(b_secs);

            let eq = spans_equal(&a, &b);
            prop_assert_eq!(eq, a_secs == b_secs);
        }

        // Round-trip JSON when optional fields are all None
        #[test]
        fn round_trip_all_none_fields(url in url_gen()) {
            let cache: Cache = DashMap::new();
            let cv = CacheValue {
                timestamp: Timestamp::now(),
                retry_after: None,
                last_modified: None,
                etag: None,
                body: None,
            };
            cache.insert(url.clone(), cv.clone());

            let tmp = NamedTempFile::new().expect("temp file");
            cache.store(tmp.path()).expect("store succeeds");

            let loaded = Cache::load(tmp.path(), u64::MAX, Timestamp::now()).expect("load succeeds");
            let loaded_val = loaded.get(&url).expect("key present after load");
            prop_assert_eq!(&*loaded_val, &cv);
        }

        #[test]
        fn load_clamps_max_age(url in url_gen(), value in cache_value_gen()) {
            // create file with single entry
            let cache: Cache = DashMap::new();
            cache.insert(url.clone(), value.clone());
            let tmp = NamedTempFile::new().expect("temp file");
            cache.store(tmp.path()).expect("store succeeds");

            // Use an enormous max_age (u128-sized) mapped to u64::MAX via API; ensure it doesn't overflow
            let loaded_large = Cache::load(tmp.path(), u64::MAX, Timestamp::now()).expect("load succeeds");
            let loaded_clamped = Cache::load(tmp.path(), MAX_SPAN_SEC as u64, Timestamp::now()).expect("load succeeds");

            // Both should contain the same entry (since clamp should cap max age)
            prop_assert!(loaded_large.contains_key(&url));
            prop_assert!(loaded_clamped.contains_key(&url));
        }

        #[test]
        fn round_trip_preserves_entries(
            // generate a vector of (Url, CacheValue) pairs
            entries in prop::collection::vec((url_gen(), cache_value_gen()), 0..100)
        ) {
            // Build a cache and insert the generated entries
            let cache: Cache = DashMap::new();
            for (url, value) in &entries {
                cache.insert(url.clone(), value.clone());
            }

            // Write to a temporary file
            let tmp = NamedTempFile::new().expect("temp file");
            cache.store(tmp.path()).expect("store succeeds");

            // Load with a very large max_age so nothing is filtered out
            let loaded = Cache::load(tmp.path(), u64::MAX, Timestamp::now()).expect("load succeeds");

            // The two maps must contain the same keys and values
            for (url, value) in entries {
                let loaded_val = loaded.get(&url).expect("key present after load");
                prop_assert_eq!(&*loaded_val, &value);
            }
        }

        #[test]
        fn age_filter_discards_old_entries(
            // generate a fresh timestamp (now) and a max_age in seconds, ensuring they can be subtracted
            now_secs in 10_000i64..Timestamp::MAX.as_second(),
            max_age in 0u32..10_000,
            // generate a mix of recent and old entries
            entries in prop::collection::vec(
                (url_gen(), cache_value_gen()),
                0..200
            )
        ) {
            // Freeze “now” for the test
            let now = bounded_timestamp(now_secs);
            let max_age_span = Span::new().seconds(i64::from(max_age));

            // Build a cache where half the entries are artificially old
            let cache: Cache = DashMap::new();

            for (i, (url, mut value)) in entries.into_iter().enumerate() {
                // Make every even index entry older than max_age
                if i % 2 == 0 {
                    value.timestamp = now - max_age_span - Span::new().seconds(1);
                } else {
                    value.timestamp = now;
                }
                cache.insert(url, value);
            }

            // Write to a temporary file
            let tmp = NamedTempFile::new().expect("temp file");
            cache.store(tmp.path()).expect("store succeeds");

            // Load with the generated max_age
            let loaded = Cache::load(tmp.path(), max_age.into(), now).expect("load succeeds");

            // Verify that only the “new” entries survived
            for entry in &cache {
                let url   = entry.key();    // &Url
                let value = entry.value(); // &CacheValue

                let cutoff = now - max_age_span;          // Timestamp that is `max_age` old
                let should_keep = value.timestamp > cutoff;
                let present = loaded.contains_key(url);
                prop_assert_eq!(present, should_keep);
            }
        }
    }
    #[test]
    fn load_cache_returns_none_when_cache_disabled() {
        let tmp_cache_path = NamedTempFile::new().expect("temp file");
        let mut args = Args {
            no_cache: true,
            ..Default::default()
        };
        args.no_cache = true;
        assert!(super::load_cache(&args, CachePath::Path(tmp_cache_path.path())).is_none());
    }

    #[test]
    fn load_cache_returns_none_when_no_file() {
        let tmpdir = TempDir::new().expect("tempdir");
        let tmp_cache_path = tmpdir.path().join("nonexistent");
        let args = Args {
            no_cache: false,
            ..Default::default()
        };
        // Ensure no cache file exists
        let _ = fs::remove_file(&tmp_cache_path);
        assert!(super::load_cache(&args, CachePath::Path(tmp_cache_path.as_path())).is_none());
    }

    #[test]
    fn load_cache_discards_too_old_file_and_returns_none() {
        let tmp_cache_path = NamedTempFile::new().expect("temp file");
        File::create(&tmp_cache_path).expect("create cache file");

        // Ensure the file's mtime is at least in the past relative to the check
        sleep(StdDuration::from_millis(10));

        // Use a max_cache_age smaller than the sleep to make the file "too old"
        let args = Args {
            no_cache: false,
            max_cache_age: Duration::from_millis(1),
            ..Default::default()
        };

        // Should detect file too old and return None
        assert!(super::load_cache(&args, CachePath::Path(tmp_cache_path.path())).is_none());
    }

    #[test]
    fn load_cache_uses_recent_file_and_loads_entries() {
        let tmp_cache_path = NamedTempFile::new().expect("temp file");

        // Prepare a real cache JSON under the expected filename
        let url = Url::parse("https://example.test/").unwrap();
        let value = CacheValue {
            timestamp: Timestamp::now(),
            retry_after: None,
            last_modified: Some("Mon, 01 Jan 2000 00:00:00 GMT".into()),
            etag: Some("etag".into()),
            body: Some("body".into()),
        };
        let cache = Cache::new();
        cache.insert(url.clone(), value.clone());
        cache.store(&tmp_cache_path).expect("store");

        let args = Args {
            no_cache: false,
            max_cache_age: Duration::from_hours(24),
            ..Default::default()
        };

        // Should return Some(Cache) and contain our entry
        let loaded =
            super::load_cache(&args, CachePath::Path(tmp_cache_path.path())).expect("some cache");
        assert!(loaded.contains_key(&url));
        let loaded_val = loaded.get(&url).expect("get");
        assert_eq!(&*loaded_val, &value);
    }

    #[test]
    fn cache_round_trip_prunes_entries_that_are_old() {
        let tmp_cache_path = NamedTempFile::new().expect("temp file");

        // Prepare a real cache JSON under the expected filename
        let cache = Cache::new();
        let valid_url = Url::parse("https://example.test/").unwrap();
        let valid_value = CacheValue {
            timestamp: Timestamp::now(),
            retry_after: None,
            last_modified: Some("Mon, 01 Jan 2000 00:00:00 GMT".into()),
            etag: Some("etag".into()),
            body: Some("body".into()),
        };
        cache.insert(valid_url.clone(), valid_value.clone());

        // To old, should be filtered
        let expired_url = Url::parse("https://example2.test/").unwrap();
        let expired_value = CacheValue {
            timestamp: Timestamp::now() - Duration::from_hours(48),
            retry_after: None,
            last_modified: Some("Mon, 01 Jan 2000 00:00:00 GMT".into()),
            etag: Some("etag".into()),
            body: Some("body".into()),
        };
        cache.insert(expired_url.clone(), expired_value.clone());
        cache.store(&tmp_cache_path).expect("store");

        let args = Args {
            no_cache: false,
            max_cache_age: Duration::from_hours(24),
            ..Default::default()
        };

        // Should return Some(Cache) and contain only the valid entry
        let loaded =
            super::load_cache(&args, CachePath::Path(tmp_cache_path.path())).expect("some cache");
        assert!(loaded.contains_key(&valid_url));
        assert!(!loaded.contains_key(&expired_url));
        let loaded_val = loaded.get(&valid_url).expect("get");
        assert_eq!(&*loaded_val, &valid_value);

        // After loading with the config, storing again should prune old entries
        loaded.store(&tmp_cache_path).expect("store");
        let loaded =
            super::load_cache(&args, CachePath::Path(tmp_cache_path.path())).expect("some cache");
        assert!(loaded.contains_key(&valid_url));
        assert!(!loaded.contains_key(&expired_url));
    }
}