From 11e183504d51b6d6f68658731cfc3cc224327710 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Mon, 25 Nov 2019 14:59:42 -0600 Subject: [PATCH] now the context og the templates is unificated in a single file --- src/main.rs | 38 +++++--------------------------------- src/misc.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 33 deletions(-) create mode 100644 src/misc.rs diff --git a/src/main.rs b/src/main.rs index 404a092..b968e59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![feature(proc_macro_hygiene, decl_macro)] +pub mod misc; pub mod models; pub mod schema; @@ -9,7 +10,6 @@ extern crate rocket; extern crate diesel; extern crate comrak; extern crate dotenv; -extern crate rand; extern crate rocket_contrib; extern crate tera; @@ -19,25 +19,13 @@ use comrak::{markdown_to_html, ComrakOptions}; use diesel::pg::PgConnection; use diesel::result::Error; use dotenv::dotenv; -use rand::Rng; +use misc::get_context; use rocket::Request; use rocket_contrib::serve::StaticFiles; use rocket_contrib::templates::Template; use std::env; use std::vec::Vec; -use tera::Context; -const TITLES: [&str; 9] = [ - "/* {} */", - "# {}", - "// {}", - "", - "{# {} #}", - "-- {}", - "--[[ {} --]]", - "; {}", - "% {}", -]; const MAX_POSTS_PER_PAGE: i64 = 20; fn establish_connection() -> PgConnection { @@ -71,17 +59,12 @@ fn get_posts(page: i64) -> (Vec, i64) { fn index(page: Option) -> Template { let page = page.unwrap_or_else(|| 1); - let mut context = Context::new(); - - let mut rng = rand::thread_rng(); - let title_fmt = TITLES[rng.gen_range(0, TITLES.len())]; - let title = str::replace(title_fmt, "{}", "CódigoComentado"); + let mut context = get_context(); let (posts, n_posts) = get_posts(page as i64); let total_pages = (n_posts as f64 / MAX_POSTS_PER_PAGE as f64).ceil() as i64; - context.insert("title", &title); context.insert("posts", &posts); context.insert("total_pages", &total_pages); context.insert("actual_page", &page); @@ -99,19 +82,13 @@ fn get_post(id_number: i32) -> Result { #[get("/post/")] fn show_post(title: String) -> Template { - let mut context = Context::new(); + let mut context = get_context(); let title_splited: Vec<&str> = title.split('-').collect(); let id = title_splited.last().unwrap().parse().unwrap(); - let mut rng = rand::thread_rng(); - let title_fmt = TITLES[rng.gen_range(0, TITLES.len())]; - let title = str::replace(title_fmt, "{}", "CódigoComentado"); - let post = get_post(id); - context.insert("title", &title); - match post { Ok(mut p) => { let content = markdown_to_html(&p.content, &ComrakOptions::default()); @@ -130,15 +107,10 @@ fn show_post(title: String) -> Template { #[catch(404)] fn not_found_404(req: &Request) -> Template { - let mut context = Context::new(); - - let mut rng = rand::thread_rng(); - let title_fmt = TITLES[rng.gen_range(0, TITLES.len())]; - let title = str::replace(title_fmt, "{}", "CódigoComentado"); + let mut context = get_context(); let uri = format!("{}", req.uri()); - context.insert("title", &title); context.insert("url", &uri); Template::render("404", context) } diff --git a/src/misc.rs b/src/misc.rs new file mode 100644 index 0000000..adec8ee --- /dev/null +++ b/src/misc.rs @@ -0,0 +1,28 @@ +extern crate rand; +extern crate tera; + +use rand::Rng; +use tera::Context; + +const TITLES: [&str; 9] = [ + "/* {} */", + "# {}", + "// {}", + "<!-- {} -->", + "{# {} #}", + "-- {}", + "--[[ {} --]]", + "; {}", + "% {}", +]; + +pub fn get_context() -> Context { + let mut context = Context::new(); + + let mut rng = rand::thread_rng(); + let title_fmt = TITLES[rng.gen_range(0, TITLES.len())]; + let title = str::replace(title_fmt, "{}", "CódigoComentado"); + + context.insert("title", &title); + context +}