use rocket::response::{Redirect, Responder};
use rocket_dyn_templates::{context, Template};

#[catch(404)]
pub async fn not_found() -> Template {
	Template::render("error/not-found", context! {})
}

#[derive(Responder)]
pub enum PageResponse {
	Page(Template),
	Redirect(Redirect),
}

impl From<Template> for PageResponse {
	fn from(value: Template) -> Self {
		PageResponse::Page(value)
	}
}

impl From<Redirect> for PageResponse {
	fn from(value: Redirect) -> Self {
		PageResponse::Redirect(value)
	}
}

#[derive(Responder)]
#[response(status = 500)]
pub struct Fail(Template);

impl<T: std::fmt::Debug> From<T> for Fail {
	fn from(value: T) -> Self {
		error!("Internal error: {:?}", value);
		Fail(Template::render("error/internal", context! {}))
	}
}

pub type RawResult<T> = Result<T, Fail>;
pub type PageResult = RawResult<PageResponse>;
