Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const PLAINTEXT_MEDIA_TYPES: &[&str] = &[
pub fn create_monolithic_document(
source: String,
options: &Options,
mut cache: &mut Cache, // TODO: make it Option-al
mut cache: &mut Option<Cache>,
) -> Result<Vec<u8>, MonolithError> {
// Check if source was provided
if source.len() == 0 {
Expand Down Expand Up @@ -488,7 +488,7 @@ pub fn parse_content_type(content_type: &str) -> (String, String, bool) {
}

pub fn retrieve_asset(
cache: &mut Cache,
cache: &mut Option<Cache>,
client: &Client,
parent_url: &Url,
url: &Url,
Expand Down Expand Up @@ -570,17 +570,17 @@ pub fn retrieve_asset(
} else {
let cache_key: String = clean_url(url.clone()).as_str().to_string();

if cache.contains_key(&cache_key) {
if cache.is_some() && cache.as_ref().unwrap().contains_key(&cache_key) {
// URL is in cache, we get and return it
if !options.silent {
eprintln!("{} (from cache)", &url);
}

Ok((
cache.get(&cache_key).unwrap().0.to_vec(),
cache.as_ref().unwrap().get(&cache_key).unwrap().0.to_vec(),
url.clone(),
cache.get(&cache_key).unwrap().1,
cache.get(&cache_key).unwrap().2,
cache.as_ref().unwrap().get(&cache_key).unwrap().1,
cache.as_ref().unwrap().get(&cache_key).unwrap().2,
))
} else {
if let Some(domains) = &options.domains {
Expand Down Expand Up @@ -676,7 +676,14 @@ pub fn retrieve_asset(
}

// Add retrieved resource to cache
cache.set(&new_cache_key, &data, media_type.clone(), charset.clone());
if cache.is_some() {
cache.as_mut().unwrap().set(
&new_cache_key,
&data,
media_type.clone(),
charset.clone(),
);
}

// Return
Ok((data, response_url, media_type, charset))
Expand Down
4 changes: 2 additions & 2 deletions src/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const CSS_PROPS_WITH_IMAGE_URLS: &[&str] = &[
];

pub fn embed_css(
cache: &mut Cache,
cache: &mut Option<Cache>,
client: &Client,
document_url: &Url,
css: &str,
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn is_image_url_prop(prop_name: &str) -> bool {
}

pub fn process_css<'a>(
cache: &mut Cache,
cache: &mut Option<Cache>,
client: &Client,
document_url: &Url,
parser: &mut Parser,
Expand Down
6 changes: 3 additions & 3 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub fn create_metadata_tag(url: &Url) -> String {
}

pub fn embed_srcset(
cache: &mut Cache,
cache: &mut Option<Cache>,
client: &Client,
document_url: &Url,
srcset: &str,
Expand Down Expand Up @@ -639,7 +639,7 @@ pub fn serialize_document(mut dom: RcDom, document_encoding: String, options: &O
}

pub fn retrieve_and_embed_asset(
cache: &mut Cache,
cache: &mut Option<Cache>,
client: &Client,
document_url: &Url,
node: &Handle,
Expand Down Expand Up @@ -740,7 +740,7 @@ pub fn retrieve_and_embed_asset(
}

pub fn walk_and_embed_assets(
cache: &mut Cache,
cache: &mut Option<Cache>,
client: &Client,
document_url: &Url,
node: &Handle,
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ fn main() {
Ok(tempfile) => Some(tempfile),
Err(_) => None,
};
let mut cache = Cache::new(
let cache = Cache::new(
CACHE_ASSET_FILE_SIZE_THRESHOLD,
if temp_cache_file.is_some() {
Some(
Expand Down Expand Up @@ -210,7 +210,7 @@ fn main() {
}
}

match create_monolithic_document(source, &options, &mut cache) {
match create_monolithic_document(source, &options, &mut Some(cache)) {
Ok(result) => {
// Define output
let mut output = Output::new(&destination).expect("Could not prepare output");
Expand Down
8 changes: 4 additions & 4 deletions tests/core/retrieve_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod passing {

#[test]
fn read_data_url() {
let cache = &mut Cache::new(0, None);
let cache = &mut Some(Cache::new(0, None));
let client = Client::new();

let mut options = Options::default();
Expand Down Expand Up @@ -47,7 +47,7 @@ mod passing {

#[test]
fn read_local_file_with_file_url_parent() {
let cache = &mut Cache::new(0, None);
let cache = &mut Some(Cache::new(0, None));
let client = Client::new();

let mut options = Options::default();
Expand Down Expand Up @@ -107,7 +107,7 @@ mod failing {

#[test]
fn read_local_file_with_data_url_parent() {
let cache = &mut Cache::new(0, None);
let cache = &mut Some(Cache::new(0, None));
let client = Client::new();

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

#[test]
fn read_local_file_with_https_parent() {
let cache = &mut Cache::new(0, None);
let cache = &mut Some(Cache::new(0, None));
let client = Client::new();

let mut options = Options::default();
Expand Down
26 changes: 13 additions & 13 deletions tests/css/embed_css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod passing {

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

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

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

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

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

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

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

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

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

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

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

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

#[test]
fn ie_css_hack() {
let cache = &mut Cache::new(0, None);
let cache = &mut Some(Cache::new(0, None));
let client = Client::new();
let document_url: Url = Url::parse("data:,").unwrap();
let mut options = Options::default();
Expand Down
14 changes: 7 additions & 7 deletions tests/html/embed_srcset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod passing {

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

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

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

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

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

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

#[test]
fn trailing_comma() {
let cache = &mut Cache::new(0, None);
let cache = &mut Some(Cache::new(0, None));
let client = Client::new();
let srcset_value = "small.png 1x, large.png 2x,";
let mut options = Options::default();
Expand Down
Loading