From 4f9cf4d15e76f9cf2f40351e679d8f03883aa407 Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Sun, 15 Oct 2017 16:49:55 +0200 Subject: [PATCH] Refactor assets routing --- src/wiki_lookup.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/wiki_lookup.rs b/src/wiki_lookup.rs index e262f56..190fdd0 100644 --- a/src/wiki_lookup.rs +++ b/src/wiki_lookup.rs @@ -12,25 +12,25 @@ type BoxResource = Box; type ResourceFn = Box BoxResource + Sync + Send>; lazy_static! { - static ref LOOKUP_MAP: HashMap = { - let mut lookup_map = HashMap::new(); + static ref ASSETS_MAP: HashMap = { + let mut map = HashMap::new(); - lookup_map.insert( + map.insert( format!("style-{}.css", StyleCss::checksum()), Box::new(|| Box::new(StyleCss) as BoxResource) as ResourceFn ); - lookup_map.insert( + map.insert( format!("script-{}.js", ScriptJs::checksum()), Box::new(|| Box::new(ScriptJs) as BoxResource) as ResourceFn ); - lookup_map.insert( + map.insert( format!("amatic-sc-v9-latin-regular.woff"), Box::new(|| Box::new(AmaticFont) as BoxResource) as ResourceFn ); - lookup_map + map }; } @@ -49,7 +49,16 @@ fn split_one(path: &str) -> Result<(::std::borrow::Cow, Option<&str>), ::st } fn asset_lookup(path: &str) -> ::futures::future::FutureResult, Box<::std::error::Error + Send + Sync>> { - match LOOKUP_MAP.get(path) { + let (head, tail) = match split_one(path) { + Ok(x) => x, + Err(x) => return failed(x.into()), + }; + + if tail.is_some() { + return finished(None); + } + + match ASSETS_MAP.get(head.as_ref()) { Some(resource_fn) => finished(Some(resource_fn())), None => finished(None), }