Quellcode durchsuchen

Move Post to its own module

master
Dylan Baker vor 3 Jahren
Ursprung
Commit
f853c4fa2a
4 geänderte Dateien mit 43 neuen und 40 gelöschten Zeilen
  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 Datei anzeigen

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

+ 2
- 38
src/main.rs Datei anzeigen

1
-use std::fs::File;
2
-use std::io::prelude::*;
3
-use std::io::{Error, ErrorKind};
4
-use std::path::PathBuf;
5
-
6
 use dotenv;
1
 use dotenv;
7
-use serde::{Deserialize, Serialize};
8
-use serde_json;
2
+
9
 use tide::utils::After;
3
 use tide::utils::After;
10
 
4
 
11
 mod fs;
5
 mod fs;
12
 mod middleware;
6
 mod middleware;
7
+mod post;
13
 mod routes;
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
 #[async_std::main]
10
 #[async_std::main]
47
 async fn main() -> std::io::Result<()> {
11
 async fn main() -> std::io::Result<()> {
48
     dotenv::dotenv().ok();
12
     dotenv::dotenv().ok();

+ 39
- 0
src/post.rs Datei anzeigen

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 Datei anzeigen

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

Laden…
Abbrechen
Speichern