This commit is contained in:
Sam Denty 2021-02-02 15:57:40 +00:00
parent 61ae905521
commit 4e3cfe4031
No known key found for this signature in database
GPG key ID: F3E9308D4A43BC0E
9 changed files with 60 additions and 91 deletions

50
Cargo.lock generated
View file

@ -95,12 +95,6 @@ version = "1.0.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -309,7 +303,7 @@ version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "801bbab217d7f79c0062f4f7205b5d4427c6d1a7bd7aafdd1475f7c59d62b283"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
]
[[package]]
@ -494,7 +488,7 @@ version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
@ -505,7 +499,7 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
"libc",
"wasi 0.10.2+wasi-snapshot-preview1",
]
@ -677,7 +671,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
]
[[package]]
@ -737,7 +731,7 @@ version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
]
[[package]]
@ -775,12 +769,6 @@ version = "2.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
[[package]]
name = "memory_units"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
[[package]]
name = "mime"
version = "0.3.16"
@ -888,7 +876,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "038d43985d1ddca7a9900630d8cd031b56e4794eecc2e9ea39dd17aa04399a70"
dependencies = [
"bitflags",
"cfg-if 1.0.0",
"cfg-if",
"foreign-types",
"lazy_static",
"libc",
@ -937,7 +925,7 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ccb628cad4f84851442432c60ad8e1f607e29752d0bf072cbd0baf28aa34272"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
"instant",
"libc",
"redox_syscall 0.1.57",
@ -1552,7 +1540,6 @@ dependencies = [
"mime_4",
"once_cell",
"percent-encoding",
"pin-utils",
"regex",
"reqwest-wasm",
"scraper",
@ -1562,7 +1549,6 @@ dependencies = [
"tokio 1.1.0",
"tokio-futures-byteorder",
"url",
"wee_alloc",
]
[[package]]
@ -1583,7 +1569,7 @@ version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
"libc",
"winapi",
]
@ -1706,7 +1692,7 @@ version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
"libc",
"rand 0.8.3",
"redox_syscall 0.2.4",
@ -1913,7 +1899,7 @@ version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f47026cdc4080c07e49b37087de021820269d996f581aac150ef9e5583eefe3"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
"pin-project-lite 0.2.4",
"tracing-core",
]
@ -2044,7 +2030,7 @@ version = "0.2.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55c0f7123de74f0dab9b7d00fd614e7b19349cd1e2f5252bbe9b1754b59433be"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
"serde",
"serde_json",
"wasm-bindgen-macro",
@ -2071,7 +2057,7 @@ version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3de431a2910c86679c34283a33f66f4e4abd7e0aec27b6669060148872aadf94"
dependencies = [
"cfg-if 1.0.0",
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
@ -2129,18 +2115,6 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "wee_alloc"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"
dependencies = [
"cfg-if 0.1.10",
"libc",
"memory_units",
"winapi",
]
[[package]]
name = "winapi"
version = "0.3.9"

View file

@ -21,7 +21,6 @@ crate-type = ["cdylib", "rlib"]
clap = "3.0.0-beta.2"
itertools = "0.10.0"
serde_with = "1.6.1"
pin-utils = "0.1.0"
html5ever = "0.25.1"
percent-encoding = "2.1.0"
url = { version = "2.2.0", features = ["serde"] }
@ -37,7 +36,6 @@ serde = { version = "1.0", features = ["derive", "rc"] }
serde_json = "1.0"
reqwest = { package = "reqwest-wasm", version = "0.11.0", features = ["json", "cookies", "blocking", "stream"] }
futures = "0.3.12"
wee_alloc = { version = "0.4.2", optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.1.0", features = ["full"] }

View file

@ -19,7 +19,7 @@ An efficient website icon scraper for rust or command line usage.
```bash
cargo install site_icons
site-icons https://google.com
site-icons https://github.com
# https://github.githubassets.com/favicons/favicon.svg site_favicon svg
# https://github.githubassets.com/app-icon-512.png app_icon png 512x512
# https://github.githubassets.com/app-icon-192.png app_icon png 192x192

View file

@ -29,42 +29,8 @@ pub enum IconInfo {
SVG,
}
impl Display for IconInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
IconInfo::PNG { size } => write!(f, "png {}", size),
IconInfo::JPEG { size } => write!(f, "jpeg {}", size),
IconInfo::ICO { sizes } => write!(f, "ico {}", sizes),
IconInfo::SVG => write!(f, "svg"),
}
}
}
impl Ord for IconInfo {
fn cmp(&self, other: &Self) -> Ordering {
let this_size = self.size();
let other_size = other.size();
if this_size.is_none() && other_size.is_none() {
Ordering::Equal
} else if let (Some(this_size), Some(other_size)) = (this_size, other_size) {
this_size.cmp(other_size)
} else if this_size.is_none() {
Ordering::Less
} else {
Ordering::Greater
}
}
}
impl PartialOrd for IconInfo {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl IconInfo {
pub async fn get(url: Url, sizes: Option<String>) -> Result<IconInfo, Box<dyn Error>> {
pub async fn load(url: Url, sizes: Option<String>) -> Result<IconInfo, Box<dyn Error>> {
let sizes = sizes.as_ref().and_then(|s| IconSizes::from_str(s).ok());
let (mime, mut body): (_, Box<dyn AsyncRead + Unpin>) = match url.scheme() {
@ -160,3 +126,37 @@ impl IconInfo {
}
}
}
impl Display for IconInfo {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
IconInfo::PNG { size } => write!(f, "png {}", size),
IconInfo::JPEG { size } => write!(f, "jpeg {}", size),
IconInfo::ICO { sizes } => write!(f, "ico {}", sizes),
IconInfo::SVG => write!(f, "svg"),
}
}
}
impl Ord for IconInfo {
fn cmp(&self, other: &Self) -> Ordering {
let this_size = self.size();
let other_size = other.size();
if this_size.is_none() && other_size.is_none() {
Ordering::Equal
} else if let (Some(this_size), Some(other_size)) = (this_size, other_size) {
this_size.cmp(other_size)
} else if this_size.is_none() {
Ordering::Less
} else {
Ordering::Greater
}
}
}
impl PartialOrd for IconInfo {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

View file

@ -1,5 +1,4 @@
use super::IconSize;
use crate::assert_slice_eq;
use byteorder::BigEndian;
use futures::prelude::*;
use std::{error::Error, io::Cursor};

View file

@ -19,8 +19,8 @@ use std::{
#[serde_as]
#[derive(Debug, PartialEq, Eq)]
pub struct IconSize {
width: u32,
height: u32,
pub width: u32,
pub height: u32,
}
impl Display for IconSize {
@ -92,12 +92,3 @@ fn slice_eq<T: Read + Seek + Unpin>(
cur.read_exact(&mut buffer)?;
Ok(buffer == slice)
}
#[macro_export]
macro_rules! assert_slice_eq {
($cur:expr, $offset:expr, $slice:expr, $($arg:tt)+) => {{
if !super::slice_eq($cur, $offset, $slice)? {
return Err(format!($($arg)+).into());
}
}};
}

View file

@ -1,5 +1,4 @@
use super::IconSize;
use crate::assert_slice_eq;
use byteorder::{BigEndian, ReadBytesExt};
use futures::prelude::*;
use std::{error::Error, io::Cursor};

View file

@ -61,7 +61,7 @@ impl Icons {
return;
}
let mut info = Box::pin(IconInfo::get(url.clone(), sizes));
let mut info = Box::pin(IconInfo::load(url.clone(), sizes));
// Start fetching the icon
let noop_waker = noop_waker();
@ -143,7 +143,7 @@ impl Icons {
let mentions_logo = |attr_name| {
elem
.attr(attr_name)
.map(|attr| attr.to_lowercase().contains("logo"))
.map(|attr| regex!("logo(?!s)").is_match(&attr.to_lowercase()))
.unwrap_or(false)
};
if mentions_logo("class") || mentions_logo("id") {

View file

@ -25,3 +25,11 @@ macro_rules! warn_err {
}
}};
}
macro_rules! assert_slice_eq {
($cur:expr, $offset:expr, $slice:expr, $($arg:tt)+) => {{
if !super::slice_eq($cur, $offset, $slice)? {
return Err(format!($($arg)+).into());
}
}};
}