use serde::{Deserialize, Serialize}; use serde_json; use std::io::{Error, ErrorKind}; use crate::{fs, util}; #[derive(Debug, Serialize, Deserialize)] pub struct Post { pub title: String, pub slug: String, pub body: String, pub html: String, pub date: String, } impl Post { pub async fn save(&mut self) -> Result<(), Error> { fs::write_post_to_disk(self) } pub fn generate_html(&mut self) -> String { util::generate_html(&self.body) } pub fn from_str(blob: &str) -> Result { let post: Post = match serde_json::from_str(blob) { Ok(p) => Ok(p), Err(_) => Err(Error::new( ErrorKind::Other, format!("Error deserializing post"), )), }?; Ok(post) } }