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 814B

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