better error handling

This commit is contained in:
Mylloon 2023-04-19 16:57:16 +02:00
parent 2f0c0934ac
commit 57ed1dd8e1
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 53 additions and 24 deletions

View file

@ -14,10 +14,14 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
} }
#[derive(Content)] #[derive(Content)]
struct PortfolioTemplate { struct PortfolioTemplate<'a> {
bots_app: Vec<File>, bots_app: Option<Vec<File>>,
persos_app: Vec<File>, bots_loc: Option<String>,
univ_content: String, persos_app: Option<Vec<File>>,
persos_loc: Option<String>,
univ_content: Option<String>,
univ_loc: Option<String>,
err_msg: &'a str,
} }
#[once(time = 60)] #[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_loc = format!("{projects_dir}/bots");
let bots_apps = glob(&format!("{bots_apps_loc}/*{ext}")) let bots_apps = glob(&format!("{bots_apps_loc}/*{ext}"))
.unwrap() .unwrap()
.map(|e| read_md_file(&e.unwrap().to_string_lossy())) .map(|e| read_md_file(&e.unwrap().to_string_lossy()).unwrap())
.collect::<Vec<File>>(); .collect::<Vec<File>>();
// Get perso apps // Get perso apps
let perso_apps_loc = format!("{projects_dir}/perso"); let perso_apps_loc = format!("{projects_dir}/perso");
let perso_apps = glob(&format!("{perso_apps_loc}/*{ext}")) let perso_apps = glob(&format!("{perso_apps_loc}/*{ext}"))
.unwrap() .unwrap()
.map(|e| read_md_file(&e.unwrap().to_string_lossy())) .map(|e| read_md_file(&e.unwrap().to_string_lossy()).unwrap())
.collect::<Vec<File>>(); .collect::<Vec<File>>();
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( config.tmpl.render(
"portfolio.html", "portfolio.html",
PortfolioTemplate { PortfolioTemplate {
bots_app: if bots_apps.is_empty() { bots_app,
vec![read_md_file(&bots_apps_loc)] bots_loc,
} else { persos_app,
bots_apps persos_loc,
}, univ_content,
persos_app: if perso_apps.is_empty() { univ_loc,
vec![read_md_file(&perso_apps_loc)] err_msg: "is empty",
} else {
perso_apps
},
univ_content: read_md_file(&format!("{projects_dir}/univ{ext}")).content,
}, },
Infos { Infos {
page_title: Some("Portfolio".to_string()), page_title: Some("Portfolio".to_string()),

View file

@ -71,11 +71,12 @@ pub struct File {
pub content: String, pub content: String,
} }
pub fn read_md_file(filename: &str) -> File { pub fn read_md_file(filename: &str) -> Option<File> {
// Read markdown file // Read markdown file
let text = fs::read_to_string(filename).unwrap_or(format!("{filename} is empty")); match fs::read_to_string(filename) {
Ok(text) => Some(read_md(&text)),
read_md(&text) _ => None,
}
} }
pub fn read_md(raw_text: &str) -> File { pub fn read_md(raw_text: &str) -> File {

View file

@ -12,14 +12,21 @@
<a href="{{link}} ">{{title}}</a> <a href="{{link}} ">{{title}}</a>
</h3> </h3>
{{/info}} {{/metadata}} {{/info}} {{/metadata}}
<div class="subcontent">{{&content }}</div> <div class="subcontent">{{&content}}</div>
{{/bots_app}} {{^bots_app}}
<p>{{bots_loc}} {{err_msg}}</p>
{{/bots_app}} {{/bots_app}}
<br /> <br />
</div> </div>
<h2 class="subtitle"> <h2 class="subtitle">
<a href="https://git.mylloon.fr/Paris8">Projet de l'université</a> <a href="https://git.mylloon.fr/Paris8">Projet de l'université</a>
</h2> </h2>
<div class="subcontent">{{&univ_content}}</div> <div class="subcontent">
{{&univ_content}} {{^univ_content}}
<p>{{univ_loc}} {{err_msg}}</p>
{{/univ_content}}
</div>
<h2 class="subtitle">Projets perso</h2> <h2 class="subtitle">Projets perso</h2>
<div class="subcontent"> <div class="subcontent">
{{#persos_app}} {{#metadata}} {{#info}} {{#persos_app}} {{#metadata}} {{#info}}
@ -27,7 +34,9 @@
<a href="{{link}} ">{{title}}</a> <a href="{{link}} ">{{title}}</a>
</h3> </h3>
{{/info}} {{/metadata}} {{/info}} {{/metadata}}
<div class="subcontent">{{&content }}</div> <div class="subcontent">{{&content}}</div>
{{/persos_app}} {{^persos_app}}
<p>{{persos_loc}} {{err_msg}}</p>
{{/persos_app}} {{/persos_app}}
<br /> <br />
</div> </div>