codigocomentado/src/contexts.rs

95 lines
1.6 KiB
Rust

use crate::controllers::posts;
use crate::misc::gen_title;
use crate::models::Post;
use serde::Serialize;
#[derive(Serialize)]
pub struct PageOfPosts {
title: String,
posts: Vec<Post>,
total_pages: i64,
actual_page: u64,
}
impl PageOfPosts {
pub fn new(posts: Vec<Post>, actual_page: u64, n_posts: i64) -> Self {
let total_pages = (n_posts as f64 / posts::MAX_POSTS_PER_PAGE as f64).ceil() as i64;
PageOfPosts {
title: gen_title(),
posts,
total_pages,
actual_page,
}
}
}
#[derive(Serialize)]
pub struct XmlFeed {
posts: Vec<Post>,
}
impl XmlFeed {
pub fn new(posts: Vec<Post>) -> Self {
XmlFeed { posts }
}
}
#[derive(Serialize)]
pub struct SinglePost {
title: String,
post: Post,
}
impl SinglePost {
pub fn new(post: Post) -> Self {
SinglePost {
title: gen_title(),
post,
}
}
}
#[derive(Serialize)]
pub struct NotFound {
title: String,
url: String,
}
impl NotFound {
pub fn new(url: String) -> Self {
NotFound {
title: gen_title(),
url,
}
}
}
#[derive(Serialize)]
pub struct AllPosts {
title: String,
posts: Vec<Post>,
n_posts: i64,
}
impl AllPosts {
pub fn new(posts: Vec<Post>, n_posts: i64) -> Self {
AllPosts {
title: gen_title(),
posts,
n_posts,
}
}
}
#[derive(Serialize)]
pub struct Empty {
title: String,
}
impl Empty {
pub fn new() -> Self {
Empty { title: gen_title() }
}
}