From c2df86fc33ee26a43c56d38169a840f9372cc172 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sun, 26 Nov 2023 16:35:11 +0100 Subject: [PATCH] Remove glob and move to markdowns folder --- Cargo.lock | 7 ------ Cargo.toml | 2 -- {posts => markdowns/posts}/test.md | 0 src/app/mod.rs | 3 --- src/app/models/post.rs | 33 ++++++++++++++++++++------ src/app/server/mod.rs | 1 - src/app/server/utils.rs | 37 ------------------------------ src/main.rs | 2 +- 8 files changed, 27 insertions(+), 58 deletions(-) rename {posts => markdowns/posts}/test.md (100%) delete mode 100644 src/app/server/mod.rs delete mode 100644 src/app/server/utils.rs diff --git a/Cargo.lock b/Cargo.lock index c847a66..ee83d70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -961,12 +961,6 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" -[[package]] -name = "glob" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" - [[package]] name = "gloo-net" version = "0.2.6" @@ -1838,7 +1832,6 @@ dependencies = [ "console_error_panic_hook", "console_log", "futures", - "glob", "gloo-net 0.4.0", "gray_matter", "leptos", diff --git a/Cargo.toml b/Cargo.toml index 8902ae4..c6bd355 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,6 @@ actix-files = { version = "0.6", optional = true } actix-web = { version = "4", features = ["macros"], optional = true } futures = { version = "0.3", optional = true } simple_logger = { version = "4.2", optional = true } -glob = { version = "0.3", optional = true } serde = { version = "1.0", optional = true } pulldown-cmark = { version = "0.9", optional = true } # markdown parser gray_matter = { version = "0.2", optional = true } # frontmatter parser @@ -68,7 +67,6 @@ ssr = [ "dep:actix-files", "dep:futures", "dep:simple_logger", - "dep:glob", "dep:serde", "dep:pulldown-cmark", "dep:gray_matter", diff --git a/posts/test.md b/markdowns/posts/test.md similarity index 100% rename from posts/test.md rename to markdowns/posts/test.md diff --git a/src/app/mod.rs b/src/app/mod.rs index 4bb3261..d355a4d 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,9 +1,6 @@ mod components; pub mod models; -#[cfg(feature = "ssr")] -mod server; - use leptos::*; use leptos_meta::*; use leptos_router::*; diff --git a/src/app/models/post.rs b/src/app/models/post.rs index 0c8da72..fa4d7d5 100644 --- a/src/app/models/post.rs +++ b/src/app/models/post.rs @@ -1,6 +1,5 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Debug, Clone)] +#[cfg_attr(feature = "ssr", derive(serde::Serialize, serde::Deserialize))] pub struct PostMetadata { image_path: Option, title: String, @@ -9,7 +8,8 @@ pub struct PostMetadata { project_link: Option, } -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Debug, Clone)] +#[cfg_attr(feature = "ssr", derive(serde::Serialize, serde::Deserialize))] pub struct Post { metadata: PostMetadata, content: String, @@ -42,12 +42,31 @@ cfg_if::cfg_if! { } impl Post { - pub fn get_all() -> Result, String> { - let files_content = super::super::server::utils::get_files_content_with_blob("posts/*.md")?; + pub fn get_all(folder: &str) -> Result, String> { + use std::{path::Path, fs::read_dir}; let mut posts: Vec = Vec::new(); - for (_filename, content) in files_content { + let folder_path = Path::new("markdowns").join(folder); + let paths = read_dir(folder_path).map_err(|e| e.to_string())?; + + for path_result in paths { + let path = match path_result { + Ok(path) => path.path(), + Err(e) => { + eprintln!("{:?}", e); + continue; + } + }; + + let content = std::fs::read_to_string(path); + let content = match content { + Ok(content) => content, + Err(e) => { + eprintln!("{:?}", e); + continue; + } + }; let post = Self::from(content); posts.push(post); } diff --git a/src/app/server/mod.rs b/src/app/server/mod.rs deleted file mode 100644 index fab870e..0000000 --- a/src/app/server/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod utils; \ No newline at end of file diff --git a/src/app/server/utils.rs b/src/app/server/utils.rs deleted file mode 100644 index 77f6157..0000000 --- a/src/app/server/utils.rs +++ /dev/null @@ -1,37 +0,0 @@ -use std::collections::HashMap; - -pub fn get_files_content_with_blob(blob: &str) -> Result, String> { - let mut files_content: HashMap = HashMap::new(); - - use glob::glob; - - for entry in glob(blob).map_err(|e| e.to_string())? { - match entry { - Ok(path) => { - if path.is_dir() { - continue; - } - - let filename = path.to_str(); - let filename = match filename { - Some(filename) => filename.to_string(), - None => continue, - }; - - let file_content = std::fs::read_to_string(path); - let file_content = match file_content { - Ok(file_content) => file_content, - Err(e) => { - println!("{:?}", e); - continue; - } - }; - - files_content.insert(filename, file_content.to_string()); - }, - Err(e) => println!("{:?}", e), - } - } - - Ok(files_content) -} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b1bf88e..2dfa351 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ cfg_if! { #[actix_web::main] async fn main() -> std::io::Result<()> { - let posts = app::models::Post::get_all(); + let posts = app::models::Post::get_all("posts"); for post in posts.unwrap() { println!("{:?}", post); }