Tag: Add serde_yaml

This commit is contained in:
Florian RICHER 2023-12-12 00:23:28 +01:00
parent 7dd090be7b
commit dc80396b05
2 changed files with 46 additions and 1 deletions

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)
}
}
}
}