error handling

This commit is contained in:
Maciej Jur 2024-09-08 23:07:13 +02:00
parent 771eb8cc85
commit cf2754c405
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
8 changed files with 46 additions and 23 deletions

View file

@ -14,7 +14,7 @@ pub(crate) fn render_head<'s, 'r>(
title: String, title: String,
styles: &'s [&str], styles: &'s [&str],
scripts: Option<&'s [String]>, scripts: Option<&'s [String]>,
) -> impl Renderable + 'r ) -> Result<impl Renderable + 'r, String>
where where
's: 'r, 's: 'r,
{ {
@ -23,7 +23,12 @@ where
let css_r = sack.get_style("reveal").expect("Missing styles"); let css_r = sack.get_style("reveal").expect("Missing styles");
let css_p = sack.get_style("leaflet").expect("Missing styles"); let css_p = sack.get_style("leaflet").expect("Missing styles");
maud_move!( let scripts = match scripts {
Some(scripts) => Some(emit_tags_script(sack, scripts)?),
None => None,
};
Ok(maud_move!(
meta charset="utf-8"; meta charset="utf-8";
meta name="viewport" content="width=device-width, initial-scale=1"; meta name="viewport" content="width=device-width, initial-scale=1";
title { title {
@ -47,11 +52,9 @@ where
} }
@if let Some(scripts) = scripts { @if let Some(scripts) = scripts {
@for script in scripts { (scripts)
(emit_tag_script(sack, script))
}
} }
) ))
} }
fn render_style(style: &HashedStyle) -> impl Renderable + '_ { fn render_style(style: &HashedStyle) -> impl Renderable + '_ {
@ -60,10 +63,23 @@ fn render_style(style: &HashedStyle) -> impl Renderable + '_ {
) )
} }
fn emit_tag_script<'a>(sack: &'a Sack, script: &'a str) -> impl Renderable + 'a { fn emit_tags_script<'a>(sack: &'a Sack, scripts: &'a [String]) -> Result<impl Renderable + 'a, String> {
let src = sack.get_script(script).unwrap().path.as_str(); let tags = scripts
.iter()
.map(|script| emit_tag_script(sack, script))
.collect::<Result<Vec<_>, _>>()?;
maud_move!( Ok(maud_move!(
script type="module" src=(src) defer {} @for tag in tags {
) (tag)
}
))
}
fn emit_tag_script<'a>(sack: &'a Sack, script: &'a str) -> Result<impl Renderable + 'a, String> {
let src = sack
.get_script(script)
.ok_or(format!("Missing script {script}"))?;
Ok(maud_move!(script type="module" src=(src.path.as_str()) defer {}))
} }

View file

@ -81,5 +81,5 @@ pub(crate) fn home(sack: &Sack, main: &str) -> String {
} }
); );
crate::html::page(sack, main, "Home".into(), None).render().into() crate::html::page(sack, main, "Home".into(), None).unwrap().render().into()
} }

View file

@ -7,7 +7,7 @@ pub fn list<'s, 'g, 'html>(
sack: &'s Sack, sack: &'s Sack,
groups: &'g [(i32, Vec<LinkDate>)], groups: &'g [(i32, Vec<LinkDate>)],
title: String, title: String,
) -> impl Renderable + 'html ) -> Result<impl Renderable + 'html, String>
where where
's: 'html, 's: 'html,
'g: 'html, 'g: 'html,

View file

@ -114,21 +114,23 @@ fn bare<'s, 'p, 'html>(
main: impl Renderable + 'p, main: impl Renderable + 'p,
title: String, title: String,
js: Option<&'s [String]>, js: Option<&'s [String]>,
) -> impl Renderable + 'html ) -> Result<impl Renderable + 'html, String>
where where
's: 'html, 's: 'html,
'p: 'html, 'p: 'html,
{ {
maud_move!( let head = head::render_head(sack, title, &[], js)?;
Ok(maud_move!(
(Raw("<!DOCTYPE html>")) (Raw("<!DOCTYPE html>"))
html lang="en" { html lang="en" {
(head::render_head(sack, title, &[], js)) (head)
body { body {
(main) (main)
} }
} }
) ))
} }
@ -136,7 +138,7 @@ fn full<'s, 'p, 'html>(
sack: &'s Sack, sack: &'s Sack,
main: impl Renderable + 'p, main: impl Renderable + 'p,
title: String, title: String,
) -> impl Renderable + 'html ) -> Result<impl Renderable + 'html, String>
where where
's: 'html, 's: 'html,
'p: 'html, 'p: 'html,
@ -154,7 +156,7 @@ fn page<'s, 'p, 'html>(
main: impl Renderable + 'p, main: impl Renderable + 'p,
title: String, title: String,
js: Option<&'s [String]>, js: Option<&'s [String]>,
) -> impl Renderable + 'html ) -> Result<impl Renderable + 'html, String>
where where
's: 'html, 's: 'html,
'p: 'html, 'p: 'html,
@ -185,10 +187,10 @@ pub(crate) fn to_list(sack: &Sack, list: Vec<LinkDate>, title: String) -> String
groups.sort_by(|a, b| b.0.cmp(&a.0)); groups.sort_by(|a, b| b.0.cmp(&a.0));
list::list(sack, &groups, title).render().into() list::list(sack, &groups, title).unwrap().render().into()
} }
pub(crate) fn map<'s, 'html>(sack: &'s Sack) -> impl Renderable + 'html pub(crate) fn map<'s, 'html>(sack: &'s Sack) -> Result<impl Renderable + 'html, String>
where where
's: 'html, 's: 'html,
{ {
@ -214,6 +216,7 @@ pub(crate) fn search<'s, 'html>(sack: &'s Sack) -> String {
String::from("Search"), String::from("Search"),
Some(&["search".into()]) Some(&["search".into()])
) )
.unwrap()
.render() .render()
.into() .into()
} }
@ -292,6 +295,7 @@ pub(crate) fn flox<'p, 's, 'html>(
String::from("Flox"), String::from("Flox"),
Some(&["editor".into()]) Some(&["editor".into()])
) )
.unwrap()
.render() .render()
.into() .into()
} }

View file

@ -33,6 +33,7 @@ impl Content for Post {
bibliography: Bibliography, bibliography: Bibliography,
) -> String { ) -> String {
post(self, parsed, sack, outline, bibliography) post(self, parsed, sack, outline, bibliography)
.unwrap()
.render() .render()
.into() .into()
} }
@ -55,7 +56,7 @@ pub fn post<'s, 'p, 'html>(
sack: &'s Sack, sack: &'s Sack,
outline: Outline, outline: Outline,
bibliography: Bibliography, bibliography: Bibliography,
) -> impl Renderable + 'html ) -> Result<impl Renderable + 'html, String>
where where
's: 'html, 's: 'html,
'p: 'html, 'p: 'html,

View file

@ -82,6 +82,7 @@ pub fn show(fm: &Slideshow, sack: &Sack, slides: &str) -> String {
fm.title.clone(), fm.title.clone(),
Some(&["reveal".into()]) Some(&["reveal".into()])
) )
.unwrap()
.render() .render()
.into() .into()
} }

View file

@ -82,6 +82,7 @@ fn wiki(
); );
crate::html::page(sack, main, matter.title.to_owned(), None) crate::html::page(sack, main, matter.title.to_owned(), None)
.unwrap()
.render() .render()
.into() .into()
} }

View file

@ -34,7 +34,7 @@ fn main() {
.js("editor", "./js/flox/main.ts") .js("editor", "./js/flox/main.ts")
.js("lambda", "./js/flox/lambda.ts") .js("lambda", "./js/flox/lambda.ts")
.add_virtual( .add_virtual(
|sack| crate::html::map(sack).render().to_owned().into(), |sack| crate::html::map(sack).unwrap().render().to_owned().into(),
"map/index.html".into(), "map/index.html".into(),
) )
.add_virtual( .add_virtual(