From 0b5bff6356b57b631743f0e00565b3ef3548b297 Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Sun, 24 Jun 2018 23:00:35 +0200 Subject: [PATCH] Refactor handling of generated unique names for resources --- libs/codegen/src/static_resource.rs | 14 +++++++++++--- src/resources/article_resource.rs | 4 ++-- src/resources/new_article_resource.rs | 4 ++-- src/site.rs | 7 ++++--- src/wiki_lookup.rs | 27 ++++++++++++--------------- templates/article.html | 2 +- templates/layout.html | 3 ++- templates/search_input.html | 2 +- 8 files changed, 35 insertions(+), 28 deletions(-) diff --git a/libs/codegen/src/static_resource.rs b/libs/codegen/src/static_resource.rs index a7d85e9..73bb480 100644 --- a/libs/codegen/src/static_resource.rs +++ b/libs/codegen/src/static_resource.rs @@ -49,6 +49,14 @@ pub fn static_resource(input: TokenStream) -> TokenStream { let checksum = calculate_checksum(&abs_filename); + let path: &Path = filename.as_ref(); + let resource_name = + format!("{}-{}.{}", + path.file_stem().unwrap().to_str().unwrap(), + checksum, + path.extension().unwrap().to_str().unwrap() + ); + let mime = find_attr(&ast.attrs, "mime") .expect("The `mime` attribute must be specified"); @@ -90,12 +98,12 @@ pub fn static_resource(input: TokenStream) -> TokenStream { } impl #impl_generics #name #ty_generics #where_clause { - pub fn checksum() -> &'static str { - #checksum + pub fn resource_name() -> &'static str { + #resource_name } pub fn etag() -> ::hyper::header::EntityTag { - ::hyper::header::EntityTag::new(false, Self::checksum().to_owned()) + ::hyper::header::EntityTag::new(false, #checksum.to_owned()) } } }; diff --git a/src/resources/article_resource.rs b/src/resources/article_resource.rs index 5d5b4b4..fd39f60 100644 --- a/src/resources/article_resource.rs +++ b/src/resources/article_resource.rs @@ -29,8 +29,8 @@ struct Template<'a> { } impl<'a> Template<'a> { - fn script_js_checksum(&self) -> &'static str { - ScriptJs::checksum() + fn script_js(&self) -> &'static str { + ScriptJs::resource_name() } } diff --git a/src/resources/new_article_resource.rs b/src/resources/new_article_resource.rs index cceceab..9f3964e 100644 --- a/src/resources/new_article_resource.rs +++ b/src/resources/new_article_resource.rs @@ -69,8 +69,8 @@ impl Resource for NewArticleResource { rendered: &'a str, } impl<'a> Template<'a> { - fn script_js_checksum(&self) -> &'static str { - ScriptJs::checksum() + fn script_js(&self) -> &'static str { + ScriptJs::resource_name() } } diff --git a/src/site.rs b/src/site.rs index 582d6fa..b2fbea6 100644 --- a/src/site.rs +++ b/src/site.rs @@ -9,7 +9,7 @@ use hyper::mime; use hyper::server::*; use hyper; -use assets::{StyleCss, SearchJs}; +use assets::{ThemesCss, StyleCss, SearchJs}; use build_config; use web::Lookup; use wiki_lookup::WikiLookup; @@ -41,8 +41,9 @@ impl<'a, T: 'a + fmt::Display> Layout<'a, T> { THEMES[choice] } - pub fn style_css_checksum(&self) -> &str { StyleCss::checksum() } - pub fn search_js_checksum(&self) -> &str { SearchJs::checksum() } + pub fn themes_css(&self) -> &str { ThemesCss::resource_name() } + pub fn style_css(&self) -> &str { StyleCss::resource_name() } + pub fn search_js(&self) -> &str { SearchJs::resource_name() } pub fn project_name(&self) -> &str { build_config::PROJECT_NAME } pub fn version(&self) -> &str { build_config::VERSION.as_str() } diff --git a/src/wiki_lookup.rs b/src/wiki_lookup.rs index ae004c2..5683089 100644 --- a/src/wiki_lookup.rs +++ b/src/wiki_lookup.rs @@ -16,38 +16,35 @@ type BoxResource = Box; type ResourceFn = Box BoxResource + Sync + Send>; lazy_static! { - static ref ASSETS_MAP: HashMap = hashmap!{ + static ref ASSETS_MAP: HashMap<&'static str, ResourceFn> = hashmap!{ // The CSS should be built to a single CSS file at compile time - "themes.css".into() => + ThemesCss::resource_name() => Box::new(|| Box::new(ThemesCss) as BoxResource) as ResourceFn, - format!("style-{}.css", StyleCss::checksum()) => + StyleCss::resource_name() => Box::new(|| Box::new(StyleCss) as BoxResource) as ResourceFn, - format!("script-{}.js", ScriptJs::checksum()) => + ScriptJs::resource_name() => Box::new(|| Box::new(ScriptJs) as BoxResource) as ResourceFn, - format!("search-{}.js", SearchJs::checksum()) => + SearchJs::resource_name() => Box::new(|| Box::new(SearchJs) as BoxResource) as ResourceFn, - - format!("amatic-sc-v9-latin-regular.woff") => - Box::new(|| Box::new(AmaticFont) as BoxResource) as ResourceFn, }; - static ref LICENSES_MAP: HashMap = hashmap!{ - "bsd-3-clause".to_owned() => Box::new(|| Box::new( + static ref LICENSES_MAP: HashMap<&'static str, ResourceFn> = hashmap!{ + "bsd-3-clause" => Box::new(|| Box::new( HtmlResource::new(Some("../"), "The 3-Clause BSD License", include_str!("licenses/bsd-3-clause.html")) ) as BoxResource) as ResourceFn, - "gpl3".to_owned() => Box::new(|| Box::new( + "gpl3" => Box::new(|| Box::new( HtmlResource::new(Some("../"), "GNU General Public License", include_str!("licenses/gpl3.html")) ) as BoxResource) as ResourceFn, - "mit".to_owned() => Box::new(|| Box::new( + "mit" => Box::new(|| Box::new( HtmlResource::new(Some("../"), "The MIT License", include_str!("licenses/mit.html")) ) as BoxResource) as ResourceFn, - "mpl2".to_owned() => Box::new(|| Box::new( + "mpl2" => Box::new(|| Box::new( HtmlResource::new(Some("../"), "Mozilla Public License Version 2.0", include_str!("licenses/mpl2.html")) ) as BoxResource) as ResourceFn, - "sil-ofl-1.1".to_owned() => Box::new(|| Box::new( + "sil-ofl-1.1" => Box::new(|| Box::new( HtmlResource::new(Some("../"), "SIL Open Font License", include_str!("licenses/sil-ofl-1.1.html")) ) as BoxResource) as ResourceFn, }; @@ -70,7 +67,7 @@ fn split_one(path: &str) -> Result<(Cow, Option<&str>), Utf8Error> { Ok((head, tail)) } -fn map_lookup(map: &HashMap, path: &str) -> +fn map_lookup(map: &HashMap<&str, ResourceFn>, path: &str) -> FutureResult, Box<::std::error::Error + Send + Sync>> { let (head, tail) = match split_one(path) { diff --git a/templates/article.html b/templates/article.html index 52e95a1..05bf03e 100644 --- a/templates/article.html +++ b/templates/article.html @@ -1,4 +1,4 @@ - +
diff --git a/templates/layout.html b/templates/layout.html index 38a9164..2123814 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -5,7 +5,8 @@ {{#base}}{{/base}} - + + diff --git a/templates/search_input.html b/templates/search_input.html index 3e20710..2c3cd49 100644 --- a/templates/search_input.html +++ b/templates/search_input.html @@ -7,5 +7,5 @@
- +