diff --git a/src/models.rs b/src/models.rs index 0b088bc..70e918a 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,5 +1,13 @@ use chrono; +fn slug_link(slug: &str) -> &str { + if slug.is_empty() { + "." + } else { + slug + } +} + #[derive(Debug, Queryable)] pub struct ArticleRevision { pub sequence_number: i32, @@ -18,13 +26,7 @@ pub struct ArticleRevision { } impl ArticleRevision { - pub fn link(&self) -> &str { - if self.slug.is_empty() { - "." - } else { - &self.slug - } - } + pub fn link(&self) -> &str { slug_link(&self.slug) } } #[derive(Debug, Queryable)] @@ -43,9 +45,17 @@ pub struct ArticleRevisionStub { pub author: Option, } +impl ArticleRevisionStub { + pub fn link(&self) -> &str { slug_link(&self.slug) } +} + #[derive(Debug, Queryable, Serialize)] pub struct SearchResult { pub title: String, pub snippet: String, pub slug: String, } + +impl SearchResult { + pub fn link(&self) -> &str { slug_link(&self.slug) } +} diff --git a/src/resources/article_resource.rs b/src/resources/article_resource.rs index ab3c744..192e383 100644 --- a/src/resources/article_resource.rs +++ b/src/resources/article_resource.rs @@ -106,7 +106,7 @@ impl Resource for ArticleResource { data.author.as_ref().map(|x| &**x) )), edit: self.edit, - cancel_url: Some(&data.slug), + cancel_url: Some(data.link()), title: &data.title, raw: &data.body, rendered: render_markdown(&data.body), diff --git a/src/resources/search_resource.rs b/src/resources/search_resource.rs index 4c5e808..c64a962 100644 --- a/src/resources/search_resource.rs +++ b/src/resources/search_resource.rs @@ -6,7 +6,7 @@ use serde_json; use serde_urlencoded; use mimes::*; -use models; +use models::SearchResult; use site::Layout; use state::State; use web::{Resource, ResponseFuture}; @@ -148,33 +148,16 @@ impl Resource for SearchResource { #[derive(Serialize)] struct JsonResponse<'a> { query: &'a str, - hits: &'a [models::SearchResult], + hits: &'a [SearchResult], prev: Option, next: Option, } - struct Hit<'a> { - index: usize, - slug: &'a str, - title: &'a str, - snippet: &'a str, - } - - impl<'a> Hit<'a> { - fn link(&self) -> &'a str { - if self.slug == "" { - "." - } else { - self.slug - } - } - } - #[derive(BartDisplay)] #[template="templates/search.html"] struct Template<'a> { query: &'a str, - hits: &'a [Hit<'a>], + hits: &'a [(usize, &'a SearchResult)], prev: Option, next: Option, } @@ -223,12 +206,6 @@ impl Resource for SearchResource { query: self.query.as_ref().map(|x| &**x).unwrap_or(""), hits: &data.iter() .enumerate() - .map(|(i, result)| Hit { - index: i, - slug: &result.slug, - title: &result.title, - snippet: &result.snippet, - }) .collect::>(), prev, next, diff --git a/src/resources/sitemap_resource.rs b/src/resources/sitemap_resource.rs index 340178b..48cdffc 100644 --- a/src/resources/sitemap_resource.rs +++ b/src/resources/sitemap_resource.rs @@ -4,6 +4,7 @@ use hyper::header::ContentType; use hyper::server::*; use mimes::*; +use models::ArticleRevisionStub; use site::Layout; use state::State; use web::{Resource, ResponseFuture}; @@ -35,12 +36,7 @@ impl Resource for SitemapResource { #[derive(BartDisplay)] #[template="templates/sitemap.html"] struct Template<'a> { - articles: &'a [ArticleReference], - } - - struct ArticleReference { - link: String, - title: String, + articles: &'a [ArticleRevisionStub], } let data = self.state.get_latest_article_revision_stubs(); @@ -48,20 +44,13 @@ impl Resource for SitemapResource { Box::new(data.join(head) .and_then(move |(articles, head)| { - use std::iter::Iterator; - - let articles = &articles.into_iter().map(|x| { - ArticleReference { - link: if x.slug.is_empty() { ".".to_owned() } else { x.slug }, - title: x.title, - } - }).collect::>(); - Ok(head .with_body(Layout { base: None, // Hmm, should perhaps accept `base` as argument title: "Sitemap", - body: &Template { articles }, + body: &Template { + articles: &articles, + }, }.to_string())) })) } diff --git a/templates/search.html b/templates/search.html index c6b64ef..f882ed4 100644 --- a/templates/search.html +++ b/templates/search.html @@ -13,7 +13,7 @@ {{/hits}} diff --git a/templates/sitemap.html b/templates/sitemap.html index 2349b07..c127416 100644 --- a/templates/sitemap.html +++ b/templates/sitemap.html @@ -6,7 +6,7 @@