1
0
Fork 0
website/src/tag.rs

34 lines
849 B
Rust
Raw Normal View History

2023-03-29 21:48:27 +02:00
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().filter(|p| p.is_published()) {
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
}