91 lines
2.3 KiB
Rust
91 lines
2.3 KiB
Rust
use actix_web::{body::BoxBody, http, HttpResponse, HttpResponseBuilder, ResponseError};
|
|
use askama_actix::Template;
|
|
use chrono::prelude::*;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::fmt;
|
|
|
|
use crate::cache;
|
|
|
|
struct ActixError(askama::Error);
|
|
|
|
impl fmt::Debug for ActixError {
|
|
#[inline]
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
<askama::Error as fmt::Debug>::fmt(&self.0, f)
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for ActixError {
|
|
#[inline]
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
<askama::Error as fmt::Display>::fmt(&self.0, f)
|
|
}
|
|
}
|
|
|
|
impl ResponseError for ActixError {}
|
|
|
|
pub trait TemplateToResponseWithStatusCode {
|
|
fn to_response_with_status_code(&self, code: http::StatusCode) -> HttpResponse<BoxBody>;
|
|
}
|
|
|
|
impl<T: askama_actix::Template> TemplateToResponseWithStatusCode for T {
|
|
fn to_response_with_status_code(&self, code: http::StatusCode) -> HttpResponse<BoxBody> {
|
|
match self.render() {
|
|
Ok(buffer) => HttpResponseBuilder::new(code)
|
|
.content_type(http::header::HeaderValue::from_static(T::MIME_TYPE))
|
|
.body(buffer),
|
|
Err(err) => HttpResponse::from_error(ActixError(err)),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "status_code.html")]
|
|
pub struct StatusCode {
|
|
pub status_code: http::StatusCode,
|
|
pub message: Option<String>,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "web/index.html")]
|
|
pub struct Index;
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "web/about.html")]
|
|
pub struct About;
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct PostIndexPost {
|
|
pub name: String,
|
|
pub slug: String,
|
|
pub description: String,
|
|
pub published_at: NaiveDate,
|
|
pub edited_at: Option<NaiveDate>,
|
|
pub tags: Vec<String>,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "web/posts/index.html")]
|
|
pub struct Posts {
|
|
pub posts: Vec<PostIndexPost>,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "web/posts/{slug}.html")]
|
|
pub struct PostBySlug {
|
|
pub post: cache::PostWithoutDescription,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "web/tags/index.html")]
|
|
pub struct Tags {
|
|
pub tags: Vec<cache::PostsByTag>,
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "web/tags/{name}.html")]
|
|
pub struct TagByName {
|
|
pub name: String,
|
|
pub posts: Vec<PostIndexPost>,
|
|
}
|