diff --git a/Cargo.lock b/Cargo.lock index c0f342b..afc848c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1513,7 +1513,7 @@ checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" [[package]] name = "site_icons" -version = "0.2.0" +version = "0.2.1" dependencies = [ "byteorder", "clap", diff --git a/Cargo.toml b/Cargo.toml index ca8c327..bb8238d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "site_icons" -version = "0.2.0" +version = "0.2.1" authors = ["Sam Denty "] edition = "2018" license = "GPL-3.0" diff --git a/src/icon.rs b/src/icon.rs index e782dfb..c2cb23e 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -2,6 +2,7 @@ use super::IconInfo; use serde::Serialize; use std::{ cmp::Ordering, + collections::HashMap, fmt::{self, Display}, str::FromStr, }; @@ -40,6 +41,7 @@ impl FromStr for IconKind { #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] pub struct Icon { pub url: Url, + pub headers: HashMap, #[serde(with = "serde_with::rust::display_fromstr")] pub kind: IconKind, #[serde(flatten)] diff --git a/src/icons.rs b/src/icons.rs index 4b6c45b..f39c7ec 100644 --- a/src/icons.rs +++ b/src/icons.rs @@ -9,6 +9,7 @@ use html5ever::{ use reqwest::{header::*, IntoUrl}; use scraper::{ElementRef, Html}; use serde::Deserialize; +use std::convert::TryInto; use std::task::Poll; use std::{collections::HashMap, error::Error, pin::Pin, task::Context}; use url::Url; @@ -19,6 +20,7 @@ pub struct Icons { Url, ( IconKind, + HashMap, Pin>>>>, ), >, @@ -27,11 +29,17 @@ pub struct Icons { fn add_icon_entry( entries: &mut Vec, url: Url, + headers: HashMap, kind: IconKind, info: Result>, ) { match info { - Ok(info) => entries.push(Icon { url, kind, info }), + Ok(info) => entries.push(Icon { + url, + headers, + kind, + info, + }), Err(_) => warn_err!(info, "failed to parse icon"), } } @@ -46,7 +54,7 @@ impl Icons { /// Add an icon URL and start fetching it pub fn add_icon(&mut self, url: Url, kind: IconKind, sizes: Option) { - self.add_icon_with_headers(url, HeaderMap::new(), kind, sizes) + self.add_icon_with_headers(url, HashMap::new(), kind, sizes) } /// Add an icon URL and start fetching it, @@ -54,7 +62,7 @@ impl Icons { pub fn add_icon_with_headers( &mut self, url: Url, - headers: HeaderMap, + headers: HashMap, kind: IconKind, sizes: Option, ) { @@ -63,7 +71,7 @@ impl Icons { if let Some(existing_kind) = self .pending_entries .get_mut(&url) - .map(|(kind, _)| kind) + .map(|(kind, _, _)| kind) .or_else(|| { entries.find_map(|icon| { if icon.url.eq(&url) { @@ -81,15 +89,19 @@ impl Icons { return; } - let mut info = Box::pin(IconInfo::load(url.clone(), headers, sizes)); + let mut info = Box::pin(IconInfo::load( + url.clone(), + (&headers).try_into().unwrap(), + sizes, + )); // Start fetching the icon let noop_waker = noop_waker(); let cx = &mut Context::from_waker(&noop_waker); match info.poll_unpin(cx) { - Poll::Ready(info) => add_icon_entry(&mut self.entries, url, kind, info), + Poll::Ready(info) => add_icon_entry(&mut self.entries, url, headers, kind, info), Poll::Pending => { - self.pending_entries.insert(url, (kind, info)); + self.pending_entries.insert(url, (kind, headers, info)); } }; } @@ -278,14 +290,14 @@ impl Icons { let (urls, infos): (Vec<_>, Vec<_>) = self .pending_entries .into_iter() - .map(|(url, (kind, info))| ((url, kind), info)) + .map(|(url, (kind, headers, info))| ((url, headers, kind), info)) .unzip(); let mut urls = urls.into_iter(); for info in join_all(infos).await { - let (url, kind) = urls.next().unwrap(); - add_icon_entry(&mut self.entries, url, kind, info); + let (url, headers, kind) = urls.next().unwrap(); + add_icon_entry(&mut self.entries, url, headers, kind, info); } self.entries.sort();