Skip to content

Commit cf26e2b

Browse files
committed
make cache optional
1 parent 9e26046 commit cf26e2b

File tree

8 files changed

+60
-53
lines changed

8 files changed

+60
-53
lines changed

src/core.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const PLAINTEXT_MEDIA_TYPES: &[&str] = &[
103103
pub fn create_monolithic_document(
104104
source: String,
105105
options: &Options,
106-
mut cache: &mut Cache, // TODO: make it Option-al
106+
mut cache: &mut Option<Cache>,
107107
) -> Result<Vec<u8>, MonolithError> {
108108
// Check if source was provided
109109
if source.len() == 0 {
@@ -488,7 +488,7 @@ pub fn parse_content_type(content_type: &str) -> (String, String, bool) {
488488
}
489489

490490
pub fn retrieve_asset(
491-
cache: &mut Cache,
491+
cache: &mut Option<Cache>,
492492
client: &Client,
493493
parent_url: &Url,
494494
url: &Url,
@@ -570,17 +570,17 @@ pub fn retrieve_asset(
570570
} else {
571571
let cache_key: String = clean_url(url.clone()).as_str().to_string();
572572

573-
if cache.contains_key(&cache_key) {
573+
if cache.is_some() && cache.as_ref().unwrap().contains_key(&cache_key) {
574574
// URL is in cache, we get and return it
575575
if !options.silent {
576576
eprintln!("{} (from cache)", &url);
577577
}
578578

579579
Ok((
580-
cache.get(&cache_key).unwrap().0.to_vec(),
580+
cache.as_ref().unwrap().get(&cache_key).unwrap().0.to_vec(),
581581
url.clone(),
582-
cache.get(&cache_key).unwrap().1,
583-
cache.get(&cache_key).unwrap().2,
582+
cache.as_ref().unwrap().get(&cache_key).unwrap().1,
583+
cache.as_ref().unwrap().get(&cache_key).unwrap().2,
584584
))
585585
} else {
586586
if let Some(domains) = &options.domains {
@@ -676,7 +676,14 @@ pub fn retrieve_asset(
676676
}
677677

678678
// Add retrieved resource to cache
679-
cache.set(&new_cache_key, &data, media_type.clone(), charset.clone());
679+
if cache.is_some() {
680+
cache.as_mut().unwrap().set(
681+
&new_cache_key,
682+
&data,
683+
media_type.clone(),
684+
charset.clone(),
685+
);
686+
}
680687

681688
// Return
682689
Ok((data, response_url, media_type, charset))

src/css.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const CSS_PROPS_WITH_IMAGE_URLS: &[&str] = &[
3030
];
3131

3232
pub fn embed_css(
33-
cache: &mut Cache,
33+
cache: &mut Option<Cache>,
3434
client: &Client,
3535
document_url: &Url,
3636
css: &str,
@@ -73,7 +73,7 @@ pub fn is_image_url_prop(prop_name: &str) -> bool {
7373
}
7474

7575
pub fn process_css<'a>(
76-
cache: &mut Cache,
76+
cache: &mut Option<Cache>,
7777
client: &Client,
7878
document_url: &Url,
7979
parser: &mut Parser,

src/html.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn create_metadata_tag(url: &Url) -> String {
151151
}
152152

153153
pub fn embed_srcset(
154-
cache: &mut Cache,
154+
cache: &mut Option<Cache>,
155155
client: &Client,
156156
document_url: &Url,
157157
srcset: &str,
@@ -639,7 +639,7 @@ pub fn serialize_document(mut dom: RcDom, document_encoding: String, options: &O
639639
}
640640

641641
pub fn retrieve_and_embed_asset(
642-
cache: &mut Cache,
642+
cache: &mut Option<Cache>,
643643
client: &Client,
644644
document_url: &Url,
645645
node: &Handle,
@@ -740,7 +740,7 @@ pub fn retrieve_and_embed_asset(
740740
}
741741

742742
pub fn walk_and_embed_assets(
743-
cache: &mut Cache,
743+
cache: &mut Option<Cache>,
744744
client: &Client,
745745
document_url: &Url,
746746
node: &Handle,

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ fn main() {
175175
Ok(tempfile) => Some(tempfile),
176176
Err(_) => None,
177177
};
178-
let mut cache = Cache::new(
178+
let cache = Cache::new(
179179
CACHE_ASSET_FILE_SIZE_THRESHOLD,
180180
if temp_cache_file.is_some() {
181181
Some(
@@ -210,7 +210,7 @@ fn main() {
210210
}
211211
}
212212

213-
match create_monolithic_document(source, &options, &mut cache) {
213+
match create_monolithic_document(source, &options, &mut Some(cache)) {
214214
Ok(result) => {
215215
// Define output
216216
let mut output = Output::new(&destination).expect("Could not prepare output");

tests/core/retrieve_asset.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod passing {
1717

1818
#[test]
1919
fn read_data_url() {
20-
let cache = &mut Cache::new(0, None);
20+
let cache = &mut Some(Cache::new(0, None));
2121
let client = Client::new();
2222

2323
let mut options = Options::default();
@@ -47,7 +47,7 @@ mod passing {
4747

4848
#[test]
4949
fn read_local_file_with_file_url_parent() {
50-
let cache = &mut Cache::new(0, None);
50+
let cache = &mut Some(Cache::new(0, None));
5151
let client = Client::new();
5252

5353
let mut options = Options::default();
@@ -107,7 +107,7 @@ mod failing {
107107

108108
#[test]
109109
fn read_local_file_with_data_url_parent() {
110-
let cache = &mut Cache::new(0, None);
110+
let cache = &mut Some(Cache::new(0, None));
111111
let client = Client::new();
112112

113113
let mut options = Options::default();
@@ -132,7 +132,7 @@ mod failing {
132132

133133
#[test]
134134
fn read_local_file_with_https_parent() {
135-
let cache = &mut Cache::new(0, None);
135+
let cache = &mut Some(Cache::new(0, None));
136136
let client = Client::new();
137137

138138
let mut options = Options::default();

tests/css/embed_css.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod passing {
1717

1818
#[test]
1919
fn empty_input() {
20-
let cache = &mut Cache::new(0, None);
20+
let cache = &mut Some(Cache::new(0, None));
2121
let client = Client::new();
2222
let document_url: Url = Url::parse("data:,").unwrap();
2323
let options = Options::default();
@@ -30,7 +30,7 @@ mod passing {
3030

3131
#[test]
3232
fn trim_if_empty() {
33-
let cache = &mut Cache::new(0, None);
33+
let cache = &mut Some(Cache::new(0, None));
3434
let client = Client::new();
3535
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
3636
let options = Options::default();
@@ -43,7 +43,7 @@ mod passing {
4343

4444
#[test]
4545
fn style_exclude_unquoted_images() {
46-
let cache = &mut Cache::new(0, None);
46+
let cache = &mut Some(Cache::new(0, None));
4747
let client = Client::new();
4848
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
4949
let mut options = Options::default();
@@ -75,7 +75,7 @@ mod passing {
7575

7676
#[test]
7777
fn style_exclude_single_quoted_images() {
78-
let cache = &mut Cache::new(0, None);
78+
let cache = &mut Some(Cache::new(0, None));
7979
let client = Client::new();
8080
let document_url: Url = Url::parse("data:,").unwrap();
8181
let mut options = Options::default();
@@ -107,7 +107,7 @@ mod passing {
107107

108108
#[test]
109109
fn style_block() {
110-
let cache = &mut Cache::new(0, None);
110+
let cache = &mut Some(Cache::new(0, None));
111111
let client = Client::new();
112112
let document_url: Url = Url::parse("file:///").unwrap();
113113
let mut options = Options::default();
@@ -129,7 +129,7 @@ mod passing {
129129

130130
#[test]
131131
fn attribute_selectors() {
132-
let cache = &mut Cache::new(0, None);
132+
let cache = &mut Some(Cache::new(0, None));
133133
let client = Client::new();
134134
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
135135
let mut options = Options::default();
@@ -173,7 +173,7 @@ mod passing {
173173

174174
#[test]
175175
fn import_string() {
176-
let cache = &mut Cache::new(0, None);
176+
let cache = &mut Some(Cache::new(0, None));
177177
let client = Client::new();
178178
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
179179
let mut options = Options::default();
@@ -201,7 +201,7 @@ mod passing {
201201

202202
#[test]
203203
fn hash_urls() {
204-
let cache = &mut Cache::new(0, None);
204+
let cache = &mut Some(Cache::new(0, None));
205205
let client = Client::new();
206206
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
207207
let mut options = Options::default();
@@ -225,7 +225,7 @@ mod passing {
225225

226226
#[test]
227227
fn transform_percentages_and_degrees() {
228-
let cache = &mut Cache::new(0, None);
228+
let cache = &mut Some(Cache::new(0, None));
229229
let client = Client::new();
230230
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
231231
let mut options = Options::default();
@@ -247,7 +247,7 @@ mod passing {
247247

248248
#[test]
249249
fn unusual_indents() {
250-
let cache = &mut Cache::new(0, None);
250+
let cache = &mut Some(Cache::new(0, None));
251251
let client = Client::new();
252252
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
253253
let mut options = Options::default();
@@ -271,7 +271,7 @@ mod passing {
271271

272272
#[test]
273273
fn exclude_fonts() {
274-
let cache = &mut Cache::new(0, None);
274+
let cache = &mut Some(Cache::new(0, None));
275275
let client = Client::new();
276276
let document_url: Url = Url::parse("https://doesntmatter.local/").unwrap();
277277
let mut options = Options::default();
@@ -319,7 +319,7 @@ mod passing {
319319

320320
#[test]
321321
fn content() {
322-
let cache = &mut Cache::new(0, None);
322+
let cache = &mut Some(Cache::new(0, None));
323323
let client = Client::new();
324324
let document_url: Url = Url::parse("data:,").unwrap();
325325
let mut options = Options::default();
@@ -344,7 +344,7 @@ mod passing {
344344

345345
#[test]
346346
fn ie_css_hack() {
347-
let cache = &mut Cache::new(0, None);
347+
let cache = &mut Some(Cache::new(0, None));
348348
let client = Client::new();
349349
let document_url: Url = Url::parse("data:,").unwrap();
350350
let mut options = Options::default();

tests/html/embed_srcset.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod passing {
1717

1818
#[test]
1919
fn small_medium_large() {
20-
let cache = &mut Cache::new(0, None);
20+
let cache = &mut Some(Cache::new(0, None));
2121
let client = Client::new();
2222
let srcset_value = "small.png 1x, medium.png 1.5x, large.png 2x";
2323
let mut options = Options::default();
@@ -42,7 +42,7 @@ mod passing {
4242

4343
#[test]
4444
fn small_medium_only_medium_has_scale() {
45-
let cache = &mut Cache::new(0, None);
45+
let cache = &mut Some(Cache::new(0, None));
4646
let client = Client::new();
4747
let srcset_value = "small.png, medium.png 1.5x";
4848
let mut options = Options::default();
@@ -64,7 +64,7 @@ mod passing {
6464

6565
#[test]
6666
fn commas_within_file_names() {
67-
let cache = &mut Cache::new(0, None);
67+
let cache = &mut Some(Cache::new(0, None));
6868
let client = Client::new();
6969
let srcset_value = "small,s.png 1x, large,l.png 2x";
7070
let mut options = Options::default();
@@ -86,7 +86,7 @@ mod passing {
8686

8787
#[test]
8888
fn tabs_and_newlines_after_commas() {
89-
let cache = &mut Cache::new(0, None);
89+
let cache = &mut Some(Cache::new(0, None));
9090
let client = Client::new();
9191
let srcset_value = "small,s.png 1x,\nmedium,m.png 2x,\nlarge,l.png 3x";
9292
let mut options = Options::default();
@@ -111,7 +111,7 @@ mod passing {
111111

112112
#[test]
113113
fn no_whitespace_after_commas() {
114-
let cache = &mut Cache::new(0, None);
114+
let cache = &mut Some(Cache::new(0, None));
115115
let client = Client::new();
116116
let srcset_value = "small,s.png 1x,medium,m.png 2x,large,l.png 3x";
117117
let mut options = Options::default();
@@ -136,7 +136,7 @@ mod passing {
136136

137137
#[test]
138138
fn last_without_descriptor() {
139-
let cache = &mut Cache::new(0, None);
139+
let cache = &mut Some(Cache::new(0, None));
140140
let client = Client::new();
141141
let srcset_value = "small,s.png 1x, medium,m.png 2x, large,l.png";
142142
let mut options = Options::default();
@@ -179,7 +179,7 @@ mod failing {
179179

180180
#[test]
181181
fn trailing_comma() {
182-
let cache = &mut Cache::new(0, None);
182+
let cache = &mut Some(Cache::new(0, None));
183183
let client = Client::new();
184184
let srcset_value = "small.png 1x, large.png 2x,";
185185
let mut options = Options::default();

0 commit comments

Comments
 (0)