1use askama::Template;
4use axum::{
5 response::{Html, IntoResponse, Redirect},
6 routing::{MethodRouter, get},
7};
8use serde::Serialize;
9
10use crate::error::Error;
11
12pub use axum::extract::Json;
13
14pub type Request = axum::extract::Request;
16pub type Response = axum::response::Response;
18
19pub fn html(body: impl Into<String>) -> Response {
21 Html(body.into()).into_response()
22}
23
24pub fn json(data: impl Serialize) -> Response {
26 Json(data).into_response()
27}
28
29pub fn render<T: Template>(template: T) -> Result<Response, Error> {
31 Ok(Html(template.render()?).into_response())
32}
33
34pub fn redirect(to: &str) -> Response {
36 Redirect::to(to).into_response()
37}
38
39pub trait View: Clone + Send + Sync + 'static {
41 fn call(self, req: Request) -> Result<Response, Error>;
43}
44
45impl<F> View for F
46where
47 F: Fn(Request) -> Result<Response, Error> + Clone + Send + Sync + 'static,
48{
49 fn call(self, req: Request) -> Result<Response, Error> {
50 self(req)
51 }
52}
53
54pub fn get_view<V: View>(view: V) -> MethodRouter {
56 get(move |req: Request| async move { view.call(req).into_response() })
57}