Skip to content

Commit bd195b6

Browse files
Merge NoopCache and FsCache
1 parent 89a4586 commit bd195b6

File tree

6 files changed

+30
-66
lines changed

6 files changed

+30
-66
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ edition = "2018"
1313
[dependencies]
1414
chrono = { version = "0.4.6", features = [ "serde" ] }
1515
derive_more = "0.14.0"
16-
either = "1.5.0"
1716
thiserror = "1"
1817
log = "0.4.5"
1918
reqwest = { version = "0.10.0", features = ["blocking"] }

library/src/cache.rs

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,14 @@
22
33
use crate::{manifest::Manifest, Error};
44
use chrono::NaiveDate;
5-
use either::Either;
65
use std::{
76
fs,
87
path::{Path, PathBuf},
98
};
109

11-
/// A cache trait.
12-
pub trait Cache {
13-
/// Tries to load a manifest from a cached file.
14-
fn get(&self, day: NaiveDate) -> Option<Manifest>;
15-
16-
/// Stores a manifest to the disk.
17-
fn store(&self, manifest: &Manifest);
18-
}
19-
20-
impl<L, R> Cache for Either<L, R>
21-
where
22-
L: Cache,
23-
R: Cache,
24-
{
25-
fn get(&self, day: NaiveDate) -> Option<Manifest> {
26-
match self {
27-
Either::Left(x) => x.get(day),
28-
Either::Right(x) => x.get(day),
29-
}
30-
}
31-
32-
fn store(&self, manifest: &Manifest) {
33-
match self {
34-
Either::Left(x) => x.store(manifest),
35-
Either::Right(x) => x.store(manifest),
36-
}
37-
}
38-
}
39-
40-
/// A cache that does nothing.
41-
pub struct NoopCache {}
42-
43-
impl Cache for NoopCache {
44-
fn get(&self, _day: NaiveDate) -> Option<Manifest> {
45-
None
46-
}
47-
48-
fn store(&self, _manifest: &Manifest) {}
49-
}
50-
5110
/// A cache that stores manifests on a file system.
5211
pub struct FsCache {
53-
storage_path: PathBuf,
12+
storage_path: Option<PathBuf>,
5413
}
5514

5615
impl FsCache {
@@ -63,18 +22,29 @@ impl FsCache {
6322
fs::create_dir_all(path).map_err(|e| (e, format!("creating path {:?}", path)))?;
6423
}
6524
Ok(FsCache {
66-
storage_path: path.into(),
25+
storage_path: Some(path.into()),
6726
})
6827
}
6928

29+
/// Initializes a no-op cache.
30+
pub fn noop() -> Self {
31+
FsCache { storage_path: None }
32+
}
33+
7034
fn make_file_name(&self, day: NaiveDate) -> PathBuf {
7135
self.storage_path
36+
.as_ref()
37+
.unwrap()
7238
.join(day.format("%Y-%m-%d.toml").to_string())
7339
}
7440
}
7541

76-
impl Cache for FsCache {
77-
fn get(&self, day: NaiveDate) -> Option<Manifest> {
42+
impl FsCache {
43+
pub(crate) fn get(&self, day: NaiveDate) -> Option<Manifest> {
44+
if self.storage_path.is_none() {
45+
return None;
46+
}
47+
7848
let file_name = self.make_file_name(day);
7949
if !file_name.exists() {
8050
log::debug!("File {:?} doesn't exist", file_name);
@@ -85,7 +55,11 @@ impl Cache for FsCache {
8555
.ok()
8656
}
8757

88-
fn store(&self, manifest: &Manifest) {
58+
pub(crate) fn store(&self, manifest: &Manifest) {
59+
if self.storage_path.is_none() {
60+
return;
61+
}
62+
8963
let file_name = self.make_file_name(manifest.date);
9064
match manifest.save_to_file(&file_name) {
9165
Ok(_) => log::debug!("Manifest stored at {:?}", file_name),

library/src/downloader.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::skip_errors::SkipMissingExt;
22
use crate::{
3-
cache::{Cache, NoopCache},
3+
cache::FsCache,
44
manifest::Manifest,
55
source::{DefaultSource, SourceInfo},
66
Error,
@@ -9,10 +9,10 @@ use chrono::{Duration, NaiveDate};
99
use std::{io, iter};
1010

1111
/// Manifests downloader and parser.
12-
pub struct Downloader<S, C = NoopCache> {
12+
pub struct Downloader<S> {
1313
client: reqwest::blocking::Client,
1414
source: S,
15-
cache: C,
15+
cache: FsCache,
1616
skip_missing_days: usize,
1717
}
1818

@@ -29,19 +29,18 @@ impl<S> Downloader<S> {
2929
Downloader {
3030
client: reqwest::blocking::Client::new(),
3131
source,
32-
cache: NoopCache {},
32+
cache: FsCache::noop(),
3333
skip_missing_days: 0,
3434
}
3535
}
3636
}
3737

38-
impl<S, C> Downloader<S, C>
38+
impl<S> Downloader<S>
3939
where
4040
S: SourceInfo,
41-
C: Cache,
4241
{
4342
/// Sets a cache for the downloader. By default a [`NoopCache`] is used.
44-
pub fn set_cache<NewCache: Cache>(self, c: NewCache) -> Downloader<S, NewCache> {
43+
pub fn set_cache(self, c: FsCache) -> Downloader<S> {
4544
Downloader {
4645
client: self.client,
4746
source: self.source,
@@ -55,7 +54,7 @@ where
5554
/// Please not that this setting only affects the [`get_last_manifests`] method.
5655
///
5756
/// Off (zero) by default.
58-
pub fn skip_missing_days(self, skip: usize) -> Downloader<S, C> {
57+
pub fn skip_missing_days(self, skip: usize) -> Downloader<S> {
5958
Downloader {
6059
client: self.client,
6160
source: self.source,

web/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ edition = "2018"
66

77
[dependencies]
88
chrono = "0.4.6"
9-
either = "1.5.0"
109
anyhow = "1"
1110
handlebars = "1.0.3"
1211
itertools = "0.8.0"

web/src/main.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ mod tiers_table;
33

44
use anyhow::Context;
55
use chrono::{NaiveDate, Utc};
6-
use either::Either;
76
use handlebars::{handlebars_helper, Handlebars};
87
use itertools::{Itertools, Position};
98
use opts::Config;
10-
use rustup_available_packages::{
11-
cache::{FsCache, NoopCache},
12-
table::Table,
13-
AvailabilityData, Downloader,
14-
};
9+
use rustup_available_packages::{cache::FsCache, table::Table, AvailabilityData, Downloader};
1510
use serde::Serialize;
1611
use std::{
1712
fmt::Display,
@@ -187,9 +182,9 @@ fn main() -> anyhow::Result<()> {
187182

188183
let mut data: AvailabilityData = Default::default();
189184
let cache = if let Some(cache_path) = config.cache_path.as_ref() {
190-
Either::Left(FsCache::new(cache_path).with_context(|| "Can't initialize cache")?)
185+
FsCache::new(cache_path).with_context(|| "Can't initialize cache")?
191186
} else {
192-
Either::Right(NoopCache {})
187+
FsCache::noop()
193188
};
194189
let downloader = Downloader::with_default_source(&config.channel)
195190
.set_cache(cache)

0 commit comments

Comments
 (0)