From 1c2477ed05dde6e54fd445fef82a642f39a159e7 Mon Sep 17 00:00:00 2001 From: Colin Reeder Date: Wed, 30 Sep 2020 19:57:06 -0600 Subject: Hashing and caching for icons (#100) --- icons/build.rs | 52 ++++++++++++++++++++++++++++++++++++++++ icons/res/notifications-some.svg | 7 ------ icons/res/notifications_some.svg | 7 ++++++ icons/src/lib.rs | 47 ++---------------------------------- src/routes/static.rs | 4 ++++ 5 files changed, 65 insertions(+), 52 deletions(-) create mode 100644 icons/build.rs delete mode 100644 icons/res/notifications-some.svg create mode 100644 icons/res/notifications_some.svg diff --git a/icons/build.rs b/icons/build.rs new file mode 100644 index 0000000..a9ae2de --- /dev/null +++ b/icons/build.rs @@ -0,0 +1,52 @@ +use std::hash::{Hash, Hasher}; +use std::io::Write; + +fn main() { + println!("cargo:rerun-if-changed=res"); + let mut file = + std::fs::File::create(format!("{}/icons.rs", std::env::var("OUT_DIR").unwrap())).unwrap(); + let mut mapping = Vec::new(); + + writeln!(file, "use super::Icon;").unwrap(); + + for res in std::fs::read_dir("res").unwrap() { + let path = res.unwrap().path(); + if path.is_dir() { + continue; + } + + println!("{:?}", path); + + let content = std::fs::read(&path).unwrap(); + + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + content.hash(&mut hasher); + let hash = hasher.finish(); + + let key = format!("{}.svg", hash); + + let name = path.file_name().unwrap().to_str().unwrap(); + let name = (&name[0..name.len() - 4]).to_ascii_uppercase(); + + writeln!( + file, + "pub const {}: Icon=Icon{{path:\"{}\",content:include_str!(\"{}\")}};", + name, + key, + path.canonicalize().unwrap().to_str().unwrap() + ) + .unwrap(); + + mapping.push((key, name)); + } + + writeln!( + file, + "pub const ICONS_MAP: phf::Map<&'static str, &'static Icon> = phf::phf_map! {{" + ) + .unwrap(); + for (key, name) in mapping { + writeln!(file, "\"{}\" => &{},", key, name).unwrap(); + } + writeln!(file, "}};").unwrap(); +} diff --git a/icons/res/notifications-some.svg b/icons/res/notifications-some.svg deleted file mode 100644 index 5eef1fa..0000000 --- a/icons/res/notifications-some.svg +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/icons/res/notifications_some.svg b/icons/res/notifications_some.svg new file mode 100644 index 0000000..5eef1fa --- /dev/null +++ b/icons/res/notifications_some.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/icons/src/lib.rs b/icons/src/lib.rs index e9ef054..ae8d747 100644 --- a/icons/src/lib.rs +++ b/icons/src/lib.rs @@ -3,50 +3,7 @@ pub struct Icon { pub content: &'static str, } -macro_rules! icons_consts { - ($i:ident => $p:expr) => { - pub const $i: Icon = Icon { - path: $p, - content: include_str!(concat!("../res/", $p)), - }; - }; - ($i1:ident => $p1:expr, $($i2:ident => $p2:expr),+) => { - icons_consts! { $i1 => $p1 } - icons_consts! { $($i2 => $p2),+ } - } +mod icons { + include!(concat!(env!("OUT_DIR"), "/icons.rs")); } - -macro_rules! icons_map { - ($($i:ident => $p:expr),+) => { - pub const ICONS_MAP: phf::Map<&'static str, &'static Icon> = phf::phf_map! { - $($p => &icons::$i),+ - }; - } -} - -macro_rules! icons { - ($($i:ident => $p:expr),+) => { - pub mod icons { - use super::Icon; - - icons_consts! { - $($i => $p),+ - } - } - - icons_map! { - $($i => $p),+ - } - } -} - -icons! { - LOGOUT => "logout.svg", - NOTIFICATIONS => "notifications.svg", - NOTIFICATIONS_SOME => "notifications-some.svg", - PERSON => "person.svg", - UPVOTE => "upvote.svg", - UPVOTED => "upvoted.svg" -} - pub use icons::*; diff --git a/src/routes/static.rs b/src/routes/static.rs index d319efe..766a094 100644 --- a/src/routes/static.rs +++ b/src/routes/static.rs @@ -27,6 +27,10 @@ async fn handler_static_get( hyper::header::CONTENT_TYPE, hyper::header::HeaderValue::from_static("image/svg+xml"), ); + resp.headers_mut().insert( + hyper::header::CACHE_CONTROL, + hyper::header::HeaderValue::from_static("max-age=31536000, immutable"), + ); Ok(resp) } else { -- cgit v1.2.3