use serde::Serialize;
use serde_json::{Map, Value};
use crate::markdown::Heading;
use crate::{Author, Blog, Category, Post, Tag, support};
#[derive(Debug, Clone, Serialize)]
pub struct BlogView {
pub name: String,
pub locales: Vec<String>,
pub url: String,
pub feed_url: Option<String>,
}
impl BlogView {
pub fn of(blog: &Blog, locale: &str) -> Self {
Self {
name: blog.name().to_owned(),
locales: blog.locales(),
url: blog.url("index", Some(locale), &[], true),
feed_url: blog
.settings()
.feed
.enabled
.then(|| blog.url("feed", Some(locale), &[], true)),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CategoryView {
pub key: String,
pub name: String,
pub slug: String,
pub description: Option<String>,
pub color: Option<String>,
pub url: String,
pub meta: Map<String, Value>,
}
impl From<&Category> for CategoryView {
fn from(category: &Category) -> Self {
Self {
key: category.key.clone(),
name: category.name.clone(),
slug: category.slug.clone(),
description: category.description.clone(),
color: category.color.clone(),
url: category.url(),
meta: category
.meta
.keys()
.filter_map(|key| Some((key.clone(), category.meta(key)?)))
.collect(),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct TagView {
pub slug: String,
pub name: String,
pub count: usize,
pub url: String,
}
impl TagView {
pub fn of(blog: &Blog, tag: &Tag, locale: &str) -> Self {
Self {
slug: tag.slug.clone(),
name: tag.name.clone(),
count: tag.count,
url: blog.url("tag", Some(locale), &[("tag", &tag.slug)], true),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct PostView {
pub id: String,
pub locale: String,
pub slug: String,
pub url: String,
pub title: String,
pub description: Option<String>,
pub date: String,
pub date_formatted: String,
pub updated: Option<String>,
pub category_key: Option<String>,
pub category: Option<CategoryView>,
pub tags: Vec<String>,
pub author_key: Option<String>,
pub author: Option<Author>,
pub image: Option<String>,
pub image_url: Option<String>,
pub image_alt: Option<String>,
pub featured: bool,
pub draft: bool,
pub reading_time: u32,
pub meta: Map<String, Value>,
pub html: Option<String>,
pub headings: Option<Vec<Heading>>,
pub alternates: Option<Vec<(String, String)>>,
}
impl PostView {
pub fn summary(post: &Post) -> Self {
Self {
id: post.id.clone(),
locale: post.locale.clone(),
slug: post.slug.clone(),
url: post.url(),
title: post.title.clone(),
description: post.description.clone(),
date: support::atom(post.date),
date_formatted: support::long_date(post.date, &post.locale),
updated: post.updated.map(support::atom),
category_key: post.category.clone(),
category: post.category().as_ref().map(CategoryView::from),
tags: post.tags.clone(),
author_key: post.author.clone(),
author: post.author(),
image: post.image.clone(),
image_url: post.image_url(),
image_alt: post.image_alt.clone(),
featured: post.featured,
draft: post.draft,
reading_time: post.reading_time,
meta: post.meta.clone(),
html: None,
headings: None,
alternates: None,
}
}
pub fn full(post: &Post) -> Self {
Self {
html: Some(post.html()),
headings: Some(post.headings()),
alternates: Some(post.alternates()),
..Self::summary(post)
}
}
}