Skip to content

Commit

Permalink
Moved out AnyResponder (TempRedirResult)
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnyLlama committed Apr 14, 2024
1 parent 0e6d87b commit 9687bfc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/anyresponder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use rocket::response::{Redirect, Responder};
use rocket_dyn_templates::Template;

#[derive(Debug, Responder)]
pub enum AnyResponder {
Template(Box<Template>),
Redirect(Box<Redirect>),
}
13 changes: 4 additions & 9 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
anyresponder::AnyResponder,
database::{models::article::Text, DatabaseHandler},
error::Error,
};
Expand All @@ -24,23 +25,17 @@ async fn about_us(db: &State<DatabaseHandler>) -> Result<Template, Error> {
Ok(Template::render("about", context! { tags }))
}

#[derive(Debug, Responder)]
enum TempRedirResult {
Template(Box<Template>),
Redirect(Box<Redirect>),
}

// FIXME: Refactor this mess...
#[get("/t/<id>/<title_slug>")]
async fn text_by_id(
id: i32,
title_slug: &str,
db: &State<DatabaseHandler>,
) -> Result<TempRedirResult, Error> {
) -> Result<AnyResponder, Error> {
let text = Text::get_by_id(db, id, None).await?;

if title_slug != text.title_slug {
let redirect = TempRedirResult::Redirect(Box::new(Redirect::found(uri!(text_by_id(
let redirect = AnyResponder::Redirect(Box::new(Redirect::found(uri!(text_by_id(
id,
text.title_slug
)))));
Expand All @@ -49,7 +44,7 @@ async fn text_by_id(

let tags = Text::get_all_tags(db, None).await?;

let template = TempRedirResult::Template(Box::new(Template::render(
let template = AnyResponder::Template(Box::new(Template::render(
"text-by-id",
context! { text, tags },
)));
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#[macro_use]
extern crate rocket;

pub mod anyresponder;
pub mod app;
pub mod database;
pub mod defaults;
Expand Down

0 comments on commit 9687bfc

Please sign in to comment.