diff --git a/src/site.rs b/src/site.rs index 1461ee3..eda6939 100644 --- a/src/site.rs +++ b/src/site.rs @@ -77,27 +77,24 @@ struct WikiLookup { impl Lookup for WikiLookup { type Resource = ArticleResource; type Error = Box<::std::error::Error + Send + Sync>; - type Future = futures::future::FutureResult, Self::Error>; + type Future = futures::BoxFuture, Self::Error>; fn lookup(&self, path: &str, _query: Option<&str>, _fragment: Option<&str>) -> Self::Future { assert!(path.starts_with("/")); if path.starts_with("/_") { // Reserved namespace - return futures::finished(None); + return futures::finished(None).boxed(); } let slug = &path[1..]; if let Ok(article_id) = slug.parse() { - match self.state.get_article_revision_by_id(article_id) { - Ok(Some(article)) => { - futures::finished(Some(ArticleResource::new(self.state.clone(), article))) - }, - Ok(None) => futures::finished(None), - Err(err) => futures::failed(err), - } + let state = self.state.clone(); + self.state.get_article_revision_by_id(article_id) + .and_then(|x| Ok(x.map(move |article| ArticleResource::new(state, article)))) + .boxed() } else { - futures::finished(None) + futures::finished(None).boxed() } } } diff --git a/src/state.rs b/src/state.rs index c00c7ec..47282c6 100644 --- a/src/state.rs +++ b/src/state.rs @@ -3,7 +3,7 @@ use std; use diesel; use diesel::sqlite::SqliteConnection; use diesel::prelude::*; -use futures::{self, Future, IntoFuture}; +use futures::{Future, IntoFuture, BoxFuture}; use r2d2::Pool; use r2d2_diesel::ConnectionManager; @@ -21,18 +21,20 @@ impl State { State { connection_pool } } - pub fn get_article_revision_by_id(&self, article_id: i32) -> Result, Error> { - use schema::article_revisions; + pub fn get_article_revision_by_id(&self, article_id: i32) -> BoxFuture, Error> { + (|| -> Result<_, _> { + use schema::article_revisions; - Ok(article_revisions::table - .filter(article_revisions::article_id.eq(article_id)) - .order(article_revisions::revision.desc()) - .limit(1) - .load::(&*self.connection_pool.get()?)? - .pop()) + Ok(article_revisions::table + .filter(article_revisions::article_id.eq(article_id)) + .order(article_revisions::revision.desc()) + .limit(1) + .load::(&*self.connection_pool.get()?)? + .pop()) + }()).into_future().boxed() } - pub fn update_article(&self, article_id: i32, base_revision: i32, body: String) -> futures::BoxFuture { + pub fn update_article(&self, article_id: i32, base_revision: i32, body: String) -> BoxFuture { self.connection_pool.get().into_future() .map_err(Into::into) .and_then(move |conn| {