From cfd903dfd6c9c9cb7affb39b5db2721a1400c00c Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 13 Jul 2020 08:19:33 -0400 Subject: [PATCH] split blog handlers into blog.rs --- src/handlers/blog.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++ src/handlers/mod.rs | 61 +-------------------------------------- src/main.rs | 8 +++--- 3 files changed, 73 insertions(+), 64 deletions(-) create mode 100644 src/handlers/blog.rs diff --git a/src/handlers/blog.rs b/src/handlers/blog.rs new file mode 100644 index 0000000..9bcd423 --- /dev/null +++ b/src/handlers/blog.rs @@ -0,0 +1,68 @@ +use super::{PostNotFound, SeriesNotFound}; +use crate::{ + app::State, + post::Post, + templates::{self, Html, RenderRucte}, +}; +use std::sync::Arc; +use warp::{http::Response, Rejection, Reply}; + +pub async fn index(state: Arc) -> Result { + let state = state.clone(); + Response::builder().html(|o| templates::blogindex_html(o, state.blog.clone())) +} + +pub async fn series(state: Arc) -> Result { + let state = state.clone(); + let mut series: Vec = vec![]; + + for post in &state.blog { + if post.front_matter.series.is_some() { + series.push(post.front_matter.series.as_ref().unwrap().clone()); + } + } + + series.sort(); + series.dedup(); + + Response::builder().html(|o| templates::series_html(o, series)) +} + +pub async fn series_view(series: String, state: Arc) -> Result { + let state = state.clone(); + let mut posts: Vec = vec![]; + + for post in &state.blog { + if post.front_matter.series.is_none() { + continue; + } + if post.front_matter.series.as_ref().unwrap() != &series { + continue; + } + posts.push(post.clone()); + } + + if posts.len() == 0 { + Err(SeriesNotFound(series).into()) + } else { + Response::builder().html(|o| templates::series_posts_html(o, series, &posts)) + } +} + +pub async fn post_view(name: String, state: Arc) -> Result { + let mut want: Option = None; + + for post in &state.blog { + if post.link == format!("blog/{}", name) { + want = Some(post.clone()); + } + } + + match want { + None => Err(PostNotFound("blog".into(), name).into()), + Some(post) => { + let body = Html(post.body_html.clone()); + Response::builder().html(|o| templates::blogpost_html(o, post, body)) + } + } +} diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index fead8e2..e9a89b5 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,6 +1,5 @@ use crate::{ app::State, - post::Post, templates::{self, Html, RenderRucte}, }; use std::{convert::Infallible, fmt, sync::Arc}; @@ -35,65 +34,7 @@ pub async fn not_found() -> Result { Response::builder().html(|o| templates::notfound_html(o, "some path".into())) } -pub async fn blog_index(state: Arc) -> Result { - let state = state.clone(); - Response::builder().html(|o| templates::blogindex_html(o, state.blog.clone())) -} - -pub async fn blog_series(state: Arc) -> Result { - let state = state.clone(); - let mut series: Vec = vec![]; - - for post in &state.blog { - if post.front_matter.series.is_some() { - series.push(post.front_matter.series.as_ref().unwrap().clone()); - } - } - - series.sort(); - series.dedup(); - - Response::builder().html(|o| templates::series_html(o, series)) -} - -pub async fn blog_series_view(series: String, state: Arc) -> Result { - let state = state.clone(); - let mut posts: Vec = vec![]; - - for post in &state.blog { - if post.front_matter.series.is_none() { - continue; - } - if post.front_matter.series.as_ref().unwrap() != &series { - continue; - } - posts.push(post.clone()); - } - - if posts.len() == 0 { - Err(SeriesNotFound(series).into()) - } else { - Response::builder().html(|o| templates::series_posts_html(o, series, &posts)) - } -} - -pub async fn blog_post_view(name: String, state: Arc) -> Result { - let mut want: Option = None; - - for post in &state.blog { - if post.link == format!("blog/{}", name) { - want = Some(post.clone()); - } - } - - match want { - None => Err(PostNotFound("blog".into(), name).into()), - Some(post) => { - let body = Html(post.body_html.clone()); - Response::builder().html(|o| templates::blogpost_html(o, post, body)) - } - } -} +pub mod blog; #[derive(Debug, thiserror::Error)] struct PostNotFound(String, String); diff --git a/src/main.rs b/src/main.rs index 90f2b6f..5c1fcc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,21 +36,21 @@ async fn main() -> Result<()> { let index = base .and(warp::path::end()) .and(with_state(state.clone())) - .and_then(handlers::blog_index); + .and_then(handlers::blog::index); let series = base.and( - warp::path!("series").and(with_state(state.clone()).and_then(handlers::blog_series)), + warp::path!("series").and(with_state(state.clone()).and_then(handlers::blog::series)), ); let series_view = base.and( warp::path!("series" / String) .and(with_state(state.clone())) .and(warp::get()) - .and_then(handlers::blog_series_view), + .and_then(handlers::blog::series_view), ); let post_view = base.and( warp::path!(String) .and(with_state(state.clone())) .and(warp::get()) - .and_then(handlers::blog_post_view), + .and_then(handlers::blog::post_view), ); index.or(series.or(series_view)).or(post_view)