1
0
Fork 0
website/src/tag.rs

35 lines
888 B
Rust

use std::collections::HashMap;
use serde_derive::Serialize;
use crate::post::Post;
#[derive(Serialize, Debug)]
pub struct Tag {
pub slug: String,
pub absolute_path: String,
pub posts: Vec<String>,
}
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.into_iter() {
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
}