use ? to propogate errors

This commit is contained in:
Adam 2024-01-18 14:02:24 -05:00
parent 4fbba124ee
commit f0f9d1bf1a

View file

@ -1,5 +1,3 @@
use std::{any::Any, fs};
use leptos::*; use leptos::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -13,28 +11,26 @@ pub struct ArticleData {
#[server] #[server]
pub async fn slingshot() -> Result<Vec<ArticleData>, ServerFnError> { pub async fn slingshot() -> Result<Vec<ArticleData>, ServerFnError> {
use std::fs::*;
use std::io::prelude::*;
let mut articles = vec![]; let mut articles = vec![];
let data_dir = "./public/static"; let data_dir = "./pubic/static";
for dir in fs::read_dir(data_dir).unwrap() { for dir in std::fs::read_dir(data_dir)? {
for file in fs::read_dir(dir.unwrap().path()).unwrap() { for file in std::fs::read_dir(dir?.path())? {
let fileinfo = file.unwrap(); let fileinfo = file?;
let filepath = fileinfo.path(); let filepath = fileinfo.path();
let filetype = filepath.extension();
if filetype.unwrap().to_str() == Some("md") { if let Some(filetype) = filepath.extension() {
let file = read_to_string(filepath).unwrap(); if filetype == "md" {
let md1: String = markdown::to_html(&file); let file = std::fs::read_to_string(filepath)?;
let md1: String = markdown::to_html(&file);
articles.push(ArticleData { articles.push(ArticleData {
content_type: String::from("Blog"), content_type: String::from("Blog"),
title: String::from("Test article"), title: String::from("Test article"),
date: String::from("12/21/2022"), date: String::from("12/21/2022"),
content: md1, content: md1,
}) })
}
} }
} }
} }
@ -42,7 +38,7 @@ pub async fn slingshot() -> Result<Vec<ArticleData>, ServerFnError> {
// Simulate lag // Simulate lag
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
sleep(Duration::from_secs(1)); sleep(Duration::from_millis(300));
Ok(articles) Ok(articles)
} }