Browse Source

Move Post to its own module

master
Dylan Baker 3 years ago
parent
commit
f853c4fa2a
4 changed files with 43 additions and 40 deletions
  1. 1
    1
      src/fs.rs
  2. 2
    38
      src/main.rs
  3. 39
    0
      src/post.rs
  4. 1
    1
      src/routes.rs

+ 1
- 1
src/fs.rs View File

@@ -5,7 +5,7 @@ use std::fs::read_dir;
5 5
 use std::io::{Error, ErrorKind};
6 6
 use std::path::PathBuf;
7 7
 
8
-use crate::Post;
8
+use crate::post::Post;
9 9
 
10 10
 pub async fn get_posts_directory_path() -> Result<PathBuf, Error> {
11 11
     match env::var("POSTS_DIR") {

+ 2
- 38
src/main.rs View File

@@ -1,48 +1,12 @@
1
-use std::fs::File;
2
-use std::io::prelude::*;
3
-use std::io::{Error, ErrorKind};
4
-use std::path::PathBuf;
5
-
6 1
 use dotenv;
7
-use serde::{Deserialize, Serialize};
8
-use serde_json;
2
+
9 3
 use tide::utils::After;
10 4
 
11 5
 mod fs;
12 6
 mod middleware;
7
+mod post;
13 8
 mod routes;
14 9
 
15
-#[derive(Debug, Serialize, Deserialize)]
16
-pub struct Post {
17
-    id: String,
18
-    title: String,
19
-    body: String,
20
-    date: String,
21
-}
22
-
23
-impl Post {
24
-    async fn save(&mut self) -> std::io::Result<()> {
25
-        let mut path: PathBuf = fs::get_posts_directory_path().await?;
26
-        let filename = format!("{}.json", self.id);
27
-        path = path.join(&filename);
28
-        let mut file = File::create(&path)?;
29
-        file.write_all(serde_json::to_string(&self)?.as_bytes())?;
30
-        Ok(())
31
-    }
32
-
33
-    fn from_str(blob: &str) -> Result<Post, Error> {
34
-        let mut post: Post = match serde_json::from_str(blob) {
35
-            Ok(p) => Ok(p),
36
-            Err(_) => Err(Error::new(
37
-                ErrorKind::Other,
38
-                format!("Error deserializing post"),
39
-            )),
40
-        }?;
41
-        post.body = post.body.replace("\n", "<br>");
42
-        Ok(post)
43
-    }
44
-}
45
-
46 10
 #[async_std::main]
47 11
 async fn main() -> std::io::Result<()> {
48 12
     dotenv::dotenv().ok();

+ 39
- 0
src/post.rs View File

@@ -0,0 +1,39 @@
1
+use serde::{Deserialize, Serialize};
2
+use serde_json;
3
+use std::fs::File;
4
+use std::io::prelude::*;
5
+use std::io::{Error, ErrorKind};
6
+use std::path::PathBuf;
7
+
8
+use crate::fs;
9
+
10
+#[derive(Debug, Serialize, Deserialize)]
11
+pub struct Post {
12
+    pub id: String,
13
+    pub title: String,
14
+    pub body: String,
15
+    pub date: String,
16
+}
17
+
18
+impl Post {
19
+    pub async fn save(&mut self) -> std::io::Result<()> {
20
+        let mut path: PathBuf = fs::get_posts_directory_path().await?;
21
+        let filename = format!("{}.json", self.id);
22
+        path = path.join(&filename);
23
+        let mut file = File::create(&path)?;
24
+        file.write_all(serde_json::to_string(&self)?.as_bytes())?;
25
+        Ok(())
26
+    }
27
+
28
+    pub fn from_str(blob: &str) -> Result<Post, Error> {
29
+        let mut post: Post = match serde_json::from_str(blob) {
30
+            Ok(p) => Ok(p),
31
+            Err(_) => Err(Error::new(
32
+                ErrorKind::Other,
33
+                format!("Error deserializing post"),
34
+            )),
35
+        }?;
36
+        post.body = post.body.replace("\n", "<br>");
37
+        Ok(post)
38
+    }
39
+}

+ 1
- 1
src/routes.rs View File

@@ -7,7 +7,7 @@ use uuid::Uuid;
7 7
 
8 8
 use std::env;
9 9
 
10
-use crate::{fs, Post};
10
+use crate::{fs, post::Post};
11 11
 
12 12
 #[derive(Debug, Serialize, Deserialize)]
13 13
 struct User {

Loading…
Cancel
Save