Remove glob and move to markdowns folder

This commit is contained in:
Florian RICHER 2023-11-26 16:35:11 +01:00
parent 4a5e2cd99b
commit c2df86fc33
8 changed files with 27 additions and 58 deletions

7
Cargo.lock generated
View file

@ -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",

View file

@ -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",

View file

@ -1,9 +1,6 @@
mod components;
pub mod models;
#[cfg(feature = "ssr")]
mod server;
use leptos::*;
use leptos_meta::*;
use leptos_router::*;

View file

@ -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<String>,
title: String,
@ -9,7 +8,8 @@ pub struct PostMetadata {
project_link: Option<String>,
}
#[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<Vec<Self>, String> {
let files_content = super::super::server::utils::get_files_content_with_blob("posts/*.md")?;
pub fn get_all(folder: &str) -> Result<Vec<Self>, String> {
use std::{path::Path, fs::read_dir};
let mut posts: Vec<Self> = 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);
}

View file

@ -1 +0,0 @@
pub mod utils;

View file

@ -1,37 +0,0 @@
use std::collections::HashMap;
pub fn get_files_content_with_blob(blob: &str) -> Result<HashMap<String, String>, String> {
let mut files_content: HashMap<String, String> = 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)
}

View file

@ -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);
}