From 57ed1dd8e1605762b4e7763f9347c28a1535df56 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 19 Apr 2023 16:57:16 +0200 Subject: [PATCH] better error handling --- src/routes/portfolio.rs | 53 +++++++++++++++++++++++++++------------- src/template.rs | 9 ++++--- templates/portfolio.html | 15 +++++++++--- 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index 347476a..f202066 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -14,10 +14,14 @@ pub async fn page(config: web::Data) -> impl Responder { } #[derive(Content)] -struct PortfolioTemplate { - bots_app: Vec, - persos_app: Vec, - univ_content: String, +struct PortfolioTemplate<'a> { + bots_app: Option>, + bots_loc: Option, + persos_app: Option>, + persos_loc: Option, + univ_content: Option, + univ_loc: Option, + err_msg: &'a str, } #[once(time = 60)] @@ -29,30 +33,45 @@ pub fn get_page(config: Config) -> String { let bots_apps_loc = format!("{projects_dir}/bots"); let bots_apps = glob(&format!("{bots_apps_loc}/*{ext}")) .unwrap() - .map(|e| read_md_file(&e.unwrap().to_string_lossy())) + .map(|e| read_md_file(&e.unwrap().to_string_lossy()).unwrap()) .collect::>(); // Get perso apps let perso_apps_loc = format!("{projects_dir}/perso"); let perso_apps = glob(&format!("{perso_apps_loc}/*{ext}")) .unwrap() - .map(|e| read_md_file(&e.unwrap().to_string_lossy())) + .map(|e| read_md_file(&e.unwrap().to_string_lossy()).unwrap()) .collect::>(); + let univ_loc = format!("{projects_dir}/univ{ext}"); + + let (bots_app, bots_loc) = if bots_apps.is_empty() { + (None, Some(bots_apps_loc)) + } else { + (Some(bots_apps), None) + }; + + let (persos_app, persos_loc) = if perso_apps.is_empty() { + (None, Some(perso_apps_loc)) + } else { + (Some(perso_apps), None) + }; + + let (univ_content, univ_loc) = match read_md_file(&univ_loc) { + Some(data) => (Some(data.content), None), + _ => (None, Some(univ_loc)), + }; + config.tmpl.render( "portfolio.html", PortfolioTemplate { - bots_app: if bots_apps.is_empty() { - vec![read_md_file(&bots_apps_loc)] - } else { - bots_apps - }, - persos_app: if perso_apps.is_empty() { - vec![read_md_file(&perso_apps_loc)] - } else { - perso_apps - }, - univ_content: read_md_file(&format!("{projects_dir}/univ{ext}")).content, + bots_app, + bots_loc, + persos_app, + persos_loc, + univ_content, + univ_loc, + err_msg: "is empty", }, Infos { page_title: Some("Portfolio".to_string()), diff --git a/src/template.rs b/src/template.rs index dc0edb5..bd7b560 100644 --- a/src/template.rs +++ b/src/template.rs @@ -71,11 +71,12 @@ pub struct File { pub content: String, } -pub fn read_md_file(filename: &str) -> File { +pub fn read_md_file(filename: &str) -> Option { // Read markdown file - let text = fs::read_to_string(filename).unwrap_or(format!("{filename} is empty")); - - read_md(&text) + match fs::read_to_string(filename) { + Ok(text) => Some(read_md(&text)), + _ => None, + } } pub fn read_md(raw_text: &str) -> File { diff --git a/templates/portfolio.html b/templates/portfolio.html index f98bec2..ff5b560 100644 --- a/templates/portfolio.html +++ b/templates/portfolio.html @@ -12,14 +12,21 @@ {{title}} {{/info}} {{/metadata}} -
{{&content }}
+
{{&content}}
+ {{/bots_app}} {{^bots_app}} +

{{bots_loc}} {{err_msg}}

{{/bots_app}}

Projet de l'université

-
{{&univ_content}}
+
+ {{&univ_content}} {{^univ_content}} +

{{univ_loc}} {{err_msg}}

+ {{/univ_content}} +
+

Projets perso

{{#persos_app}} {{#metadata}} {{#info}} @@ -27,7 +34,9 @@ {{title}} {{/info}} {{/metadata}} -
{{&content }}
+
{{&content}}
+ {{/persos_app}} {{^persos_app}} +

{{persos_loc}} {{err_msg}}

{{/persos_app}}