use serde::{Deserialize, Serialize}; use serde_json; use std::io::{Error, ErrorKind}; use crate::{fs, util}; fn f() -> bool { false } #[derive(Debug, Serialize, Deserialize)] pub struct Post { pub title: String, pub slug: String, pub body: String, pub html: String, pub date: String, #[serde(serialize_with = "util::int_from_bool")] #[serde(deserialize_with = "util::bool_from_int")] #[serde(default = "f")] pub draft: bool, } 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(e) => Err(Error::new( ErrorKind::Other, format!("Error deserializing post: {}", e.to_string()), )), }?; Ok(post) } }