From b98031de94a670dcdcc1d2a2ede1151c6eebf01d Mon Sep 17 00:00:00 2001 From: Adrian Hedqvist Date: Wed, 31 Aug 2022 23:25:17 +0200 Subject: [PATCH] cargo fmt --- src/handlers/mod.rs | 52 ++++++++++++++++++++++++++++----------------- src/main.rs | 38 ++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 1e893a9..19eb128 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1,13 +1,16 @@ -use std::sync::Arc; use serde_derive::Serialize; +use std::sync::Arc; use tracing::log::*; -use axum::{response::{Html, IntoResponse, Response}, extract::Path, Extension}; -use pulldown_cmark::{Options, Parser, html}; +use axum::{ + extract::Path, + response::{Html, IntoResponse, Response}, + Extension, +}; +use pulldown_cmark::{html, Options, Parser}; use crate::State; - pub enum Error { NotFound, } @@ -16,19 +19,29 @@ pub type Result>> = std::result::Result; #[derive(Serialize)] struct PageContext { - content: String + content: String, } pub async fn index(Extension(state): Extension>) -> Result { let ctx = tera::Context::new(); - let res = state.tera.render("index.html", &ctx).map_err(|e| { error!("Failed rendering index: {}", e); Error::NotFound})?; + let res = state.tera.render("index.html", &ctx).map_err(|e| { + error!("Failed rendering index: {}", e); + Error::NotFound + })?; Ok(Html(res.into())) } -pub async fn post_view(Path(name): Path, Extension(state): Extension>) -> Result { +pub async fn post_view( + Path(name): Path, + Extension(state): Extension>, +) -> Result { info!("Requested post: {}", name); let state = state.clone(); - let post = state.posts.iter().find(|p| p.name == name).ok_or(Error::NotFound)?; + let post = state + .posts + .iter() + .find(|p| p.name == name) + .ok_or(Error::NotFound)?; let options = Options::all(); let parser = Parser::new_ext(&post.content, options); @@ -36,12 +49,13 @@ pub async fn post_view(Path(name): Path, Extension(state): Extension, Extension(state): Extension>) -> Result { let mut ctx = tera::Context::new(); ctx.insert("posts", &state.posts); - let res = state.tera.render("postsindex.html", &ctx).map_err(|e| { error!("Failed rendering posts index: {}", e); Error::NotFound})?; + let res = state.tera.render("postsindex.html", &ctx).map_err(|e| { + error!("Failed rendering posts index: {}", e); + Error::NotFound + })?; Ok(Html(res.into())) } @@ -58,12 +75,7 @@ impl IntoResponse for Error { let result: Vec = "not found".into(); let body = axum::body::boxed(axum::body::Full::from(result)); match self { - Error::NotFound => { - Response::builder() - .status(404) - .body(body) - .unwrap() - }, + Error::NotFound => Response::builder().status(404).body(body).unwrap(), } } } diff --git a/src/main.rs b/src/main.rs index 3b2a92a..53ea8ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,12 @@ use std::{path::PathBuf, sync::Arc}; -use axum::{ - Router, - routing::get, Extension -}; +use axum::{routing::get, Extension, Router}; +use color_eyre::eyre::Result; use glob::glob; use serde_derive::Serialize; use tera::Tera; use tower_http::trace::TraceLayer; use tracing::log::*; -use color_eyre::eyre::Result; mod handlers; @@ -31,14 +28,22 @@ async fn main() -> Result<()> { info!("Starting server..."); let tera = Tera::new("templates/**/*")?; - let posts = glob("posts/**/*.md")?.map(|p| { - let path = p.unwrap(); - let name = path.file_name().unwrap().to_string_lossy().strip_suffix(".md").unwrap().to_owned(); - Post { - name, - content: std::fs::read_to_string(&path).unwrap(), - } - }).collect(); + let posts = glob("posts/**/*.md")? + .map(|p| { + let path = p.unwrap(); + let name = path + .file_name() + .unwrap() + .to_string_lossy() + .strip_suffix(".md") + .unwrap() + .to_owned(); + Post { + name, + content: std::fs::read_to_string(&path).unwrap(), + } + }) + .collect(); let state = Arc::new(State { tera, posts }); @@ -46,12 +51,11 @@ async fn main() -> Result<()> { .layer(TraceLayer::new_for_http()) .layer(Extension(state.clone())); - let mut app = Router::new() + let app = Router::new() .route("/", get(handlers::index)) .route("/posts", get(handlers::post_index)) - .route("/posts/:name", get(handlers::post_view)); - app = app.layer(middleware); - + .route("/posts/:name", get(handlers::post_view)) + .layer(middleware); info!("Now listening at http://localhost:8180");