You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

post.rs 1020B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use serde::{Deserialize, Serialize};
  2. use serde_json;
  3. use std::io::{Error, ErrorKind};
  4. use crate::{fs, util};
  5. fn f() -> bool {
  6. false
  7. }
  8. #[derive(Debug, Serialize, Deserialize)]
  9. pub struct Post {
  10. pub title: String,
  11. pub slug: String,
  12. pub body: String,
  13. pub html: String,
  14. pub date: String,
  15. #[serde(serialize_with = "util::int_from_bool")]
  16. #[serde(deserialize_with = "util::bool_from_int")]
  17. #[serde(default = "f")]
  18. pub draft: bool,
  19. }
  20. impl Post {
  21. pub async fn save(&mut self) -> Result<(), Error> {
  22. fs::write_post_to_disk(self)
  23. }
  24. pub fn generate_html(&mut self) -> String {
  25. util::generate_html(&self.body)
  26. }
  27. pub fn from_str(blob: &str) -> Result<Post, Error> {
  28. let post: Post = match serde_json::from_str(blob) {
  29. Ok(p) => Ok(p),
  30. Err(e) => Err(Error::new(
  31. ErrorKind::Other,
  32. format!("Error deserializing post: {}", e.to_string()),
  33. )),
  34. }?;
  35. Ok(post)
  36. }
  37. }