1
0
Fork 0
website/src/tag.rs

38 lines
928 B
Rust

use std::collections::HashMap;
use serde_derive::Serialize;
use tracing::instrument;
use crate::post::Post;
#[derive(Serialize, Debug)]
pub struct Tag {
pub slug: String,
pub absolute_path: String,
pub posts: Vec<String>,
}
#[instrument(skip(posts))]
pub fn get_tags<'a>(posts: impl IntoIterator<Item = &'a Post>) -> HashMap<String, Tag> {
let mut tags: HashMap<String, Tag> = HashMap::new();
for post in posts {
for key in &post.tags {
if let Some(tag) = tags.get_mut(key) {
tag.posts.push(post.slug.clone());
} else {
tags.insert(
key.clone(),
Tag {
slug: key.clone(),
absolute_path: format!("/tags/{key}/"),
posts: vec![post.slug.clone()],
},
);
}
}
}
tags
}