This commit is contained in:
Sam Denty 2021-02-03 21:13:43 +00:00
parent 3e60f40374
commit 64181e1733
No known key found for this signature in database
GPG key ID: F3E9308D4A43BC0E
7 changed files with 83 additions and 46 deletions

12
Cargo.lock generated
View file

@ -1527,7 +1527,7 @@ checksum = "fa8f3741c7372e75519bd9346068370c9cdaabcc1f9599cbcf2a2719352286b7"
[[package]] [[package]]
name = "site_icons" name = "site_icons"
version = "0.1.4" version = "0.1.5"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"clap", "clap",
@ -1549,6 +1549,7 @@ dependencies = [
"tokio 1.1.1", "tokio 1.1.1",
"tokio-futures-byteorder", "tokio-futures-byteorder",
"url", "url",
"vec1",
] ]
[[package]] [[package]]
@ -1990,6 +1991,15 @@ version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b00bca6106a5e23f3eee943593759b7fcddb00554332e856d990c893966879fb" checksum = "b00bca6106a5e23f3eee943593759b7fcddb00554332e856d990c893966879fb"
[[package]]
name = "vec1"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2fffd117bafd8d50c625cce64efde70f416ce6d38f78104035a0913cf15fe97"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "vec_map" name = "vec_map"
version = "0.8.2" version = "0.8.2"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "site_icons" name = "site_icons"
version = "0.1.4" version = "0.1.5"
authors = ["Sam Denty <sam@samdenty.com>"] authors = ["Sam Denty <sam@samdenty.com>"]
edition = "2018" edition = "2018"
license = "GPL-3.0" license = "GPL-3.0"
@ -19,6 +19,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies] [dependencies]
clap = "3.0.0-beta.2" clap = "3.0.0-beta.2"
vec1 = { version = "1.6.0", features = ["serde"] }
itertools = "0.10.0" itertools = "0.10.0"
serde_with = "1.6.2" serde_with = "1.6.2"
html5ever = "0.25.1" html5ever = "0.25.1"

View file

@ -21,7 +21,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
if opts.debug { if opts.debug {
let mut builder = Builder::new(); let mut builder = Builder::new();
builder.filter_module("info", LevelFilter::Info); builder.filter_level(LevelFilter::Info);
builder.init(); builder.init();
} }

View file

@ -19,7 +19,7 @@ enum IconType {
ICO, ICO,
} }
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(tag = "type")] #[serde(tag = "type")]
#[serde(rename_all = "lowercase")] #[serde(rename_all = "lowercase")]
pub enum IconInfo { pub enum IconInfo {
@ -76,15 +76,19 @@ impl IconInfo {
let kind = match (mime.type_(), mime.subtype()) { let kind = match (mime.type_(), mime.subtype()) {
(mime::IMAGE, mime::PNG) => { (mime::IMAGE, mime::PNG) => {
if let Some(size) = sizes.map(|s| s.into_largest()) { if let Some(sizes) = sizes {
return Ok(IconInfo::PNG { size }); return Ok(IconInfo::PNG {
size: *sizes.largest(),
});
} }
IconType::PNG IconType::PNG
} }
(mime::IMAGE, mime::JPEG) => { (mime::IMAGE, mime::JPEG) => {
if let Some(size) = sizes.map(|s| s.into_largest()) { if let Some(sizes) = sizes {
return Ok(IconInfo::JPEG { size }); return Ok(IconInfo::JPEG {
size: *sizes.largest(),
});
} }
IconType::JPEG IconType::JPEG
} }
@ -125,6 +129,14 @@ impl IconInfo {
IconInfo::SVG => None, IconInfo::SVG => None,
} }
} }
pub fn sizes(&self) -> Option<IconSizes> {
match self {
IconInfo::ICO { sizes } => Some((*sizes).clone()),
IconInfo::PNG { size } | IconInfo::JPEG { size } => Some((*size).into()),
IconInfo::SVG => None,
}
}
} }
impl Display for IconInfo { impl Display for IconInfo {

View file

@ -1,7 +1,8 @@
use super::{png::get_png_sizes, IconSizes}; use super::{png::get_png_sizes, IconSize, IconSizes};
use byteorder::{LittleEndian, ReadBytesExt}; use byteorder::{LittleEndian, ReadBytesExt};
use futures::prelude::*; use futures::prelude::*;
use std::{ use std::{
convert::TryInto,
error::Error, error::Error,
io::{Cursor, Seek, SeekFrom}, io::{Cursor, Seek, SeekFrom},
}; };
@ -32,7 +33,7 @@ pub async fn get_ico_sizes<R: AsyncRead + Unpin>(
offset += data.len(); offset += data.len();
let mut data = Cursor::new(data); let mut data = Cursor::new(data);
let mut sizes = IconSizes::new(); let mut sizes = Vec::new();
for i in 0..icon_count { for i in 0..icon_count {
data.seek(SeekFrom::Start((INDEX_SIZE * i) as _))?; data.seek(SeekFrom::Start((INDEX_SIZE * i) as _))?;
@ -52,11 +53,9 @@ pub async fn get_ico_sizes<R: AsyncRead + Unpin>(
sizes.push(size); sizes.push(size);
} }
} else { } else {
sizes.add_size(width as _, height as _) sizes.push(IconSize::new(width as _, height as _))
} }
} }
sizes.sort(); Ok(sizes.try_into()?)
Ok(sizes)
} }

View file

@ -4,13 +4,16 @@ use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
convert::{TryFrom, TryInto},
error::Error, error::Error,
fmt::{self, Display}, fmt::{self, Display},
ops::{Deref, DerefMut}, ops::Deref,
}; };
use vec1::{vec1, Vec1};
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)] #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct IconSizes(Vec<IconSize>); /// Icon sizes, ordered from largest to smallest
pub struct IconSizes(Vec1<IconSize>);
impl Display for IconSizes { impl Display for IconSizes {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -19,52 +22,44 @@ impl Display for IconSizes {
} }
impl IconSizes { impl IconSizes {
pub fn new() -> Self {
IconSizes(Vec::new())
}
pub fn from_str(sizes_str: &str) -> Result<IconSizes, Box<dyn Error>> { pub fn from_str(sizes_str: &str) -> Result<IconSizes, Box<dyn Error>> {
let size_strs = sizes_str.split(" "); let size_strs = sizes_str.split(" ");
let mut sizes = IconSizes::new(); let mut sizes = Vec::new();
for size in size_strs { for size in size_strs {
if let Ok(size) = serde_json::from_value(Value::String(size.to_string())) { if let Ok(size) = serde_json::from_value(Value::String(size.to_string())) {
sizes.push(size); sizes.push(size);
} }
} }
if sizes.is_empty() { Ok(sizes.try_into()?)
return Err("must contain a size".into());
}
sizes.sort();
Ok(sizes)
} }
pub fn add_size(&mut self, width: u32, height: u32) { pub fn add_size(&mut self, size: IconSize) {
self.push(IconSize::new(width, height)) match self.0.binary_search(&size) {
Ok(_) => {}
Err(pos) => self.0.insert(pos, size),
}
} }
pub fn largest(&self) -> &IconSize { pub fn largest(&self) -> &IconSize {
&self.0[0] self.0.first()
}
pub fn into_largest(self) -> IconSize {
self.0.into_iter().next().unwrap()
} }
} }
impl Deref for IconSizes { impl Deref for IconSizes {
type Target = Vec<IconSize>; type Target = Vec1<IconSize>;
fn deref(&self) -> &Vec<IconSize> { fn deref(&self) -> &Vec1<IconSize> {
&self.0 &self.0
} }
} }
impl DerefMut for IconSizes { impl IntoIterator for IconSizes {
fn deref_mut(&mut self) -> &mut Vec<IconSize> { type Item = IconSize;
&mut self.0 type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
} }
} }
@ -79,3 +74,21 @@ impl PartialOrd for IconSizes {
Some(self.cmp(other)) Some(self.cmp(other))
} }
} }
impl TryFrom<Vec<IconSize>> for IconSizes {
type Error = String;
fn try_from(mut vec: Vec<IconSize>) -> Result<Self, Self::Error> {
vec.sort();
Ok(IconSizes(
vec.try_into().map_err(|_| "must contain a size")?,
))
}
}
impl From<IconSize> for IconSizes {
fn from(size: IconSize) -> Self {
IconSizes(vec1![size])
}
}

View file

@ -10,14 +10,14 @@ pub use png::*;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::{ use std::{
cmp::Ordering, cmp::{self, Ordering},
error::Error, error::Error,
fmt::{self, Display}, fmt::{self, Display},
io::{Read, Seek, SeekFrom}, io::{Read, Seek, SeekFrom},
}; };
#[serde_as] #[serde_as]
#[derive(Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IconSize { pub struct IconSize {
pub width: u32, pub width: u32,
pub height: u32, pub height: u32,
@ -33,13 +33,15 @@ impl IconSize {
pub fn new(width: u32, height: u32) -> Self { pub fn new(width: u32, height: u32) -> Self {
Self { width, height } Self { width, height }
} }
pub fn max_size(&self) -> u32 {
cmp::max(self.width, self.height)
}
} }
impl Ord for IconSize { impl Ord for IconSize {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
let self_res = self.width * self.height; other.max_size().cmp(&self.max_size())
let other_res = other.width * other.height;
other_res.cmp(&self_res)
} }
} }