Ajout tags #7

Merged
florian.richer merged 13 commits from refactor-tags into main 2024-01-07 00:49:58 +01:00
2 changed files with 46 additions and 1 deletions
Showing only changes of commit dc80396b05 - Show all commits

View file

@ -1,3 +1,7 @@
mod post;
pub use post::Post;
pub use post::Post;
mod tag;
pub use tag::{Tags, Tag};

41
src/app/models/tag.rs Normal file
View file

@ -0,0 +1,41 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Tag {
pub name : String,
pub url : String,
pub groups : Vec<String>,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Tags {
pub tags : HashMap<String, Tag>,
}
cfg_if::cfg_if! {
if #[cfg(feature = "ssr")] {
impl Tags {
fn from_content(content: String) -> Option<Self> {
let tags = serde_yaml::from_str(&content).ok()?;
Some(Self {
tags
})
}
fn from_path(path: &std::path::Path) -> Option<Self> {
let content = std::fs::read_to_string(path);
let content = match content {
Ok(content) => content,
Err(e) => {
eprintln!("{:?}", e);
return None;
}
};
Self::from_content(content)
}
}
}
}