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

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