From cf2754c405edb1f7c63c4eac60b446ec49ae663b Mon Sep 17 00:00:00 2001 From: Maciej Jur Date: Sun, 8 Sep 2024 23:07:13 +0200 Subject: [PATCH] error handling --- src/html/head.rs | 38 +++++++++++++++++++++++++++----------- src/html/home.rs | 2 +- src/html/list.rs | 2 +- src/html/mod.rs | 20 ++++++++++++-------- src/html/post.rs | 3 ++- src/html/slideshow.rs | 1 + src/html/wiki.rs | 1 + src/main.rs | 2 +- 8 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/html/head.rs b/src/html/head.rs index 6a00827..bcce846 100644 --- a/src/html/head.rs +++ b/src/html/head.rs @@ -14,7 +14,7 @@ pub(crate) fn render_head<'s, 'r>( title: String, styles: &'s [&str], scripts: Option<&'s [String]>, -) -> impl Renderable + 'r +) -> Result where 's: 'r, { @@ -23,7 +23,12 @@ where let css_r = sack.get_style("reveal").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 name="viewport" content="width=device-width, initial-scale=1"; title { @@ -47,11 +52,9 @@ where } @if let Some(scripts) = scripts { - @for script in scripts { - (emit_tag_script(sack, script)) - } + (scripts) } - ) + )) } 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 { - let src = sack.get_script(script).unwrap().path.as_str(); +fn emit_tags_script<'a>(sack: &'a Sack, scripts: &'a [String]) -> Result { + let tags = scripts + .iter() + .map(|script| emit_tag_script(sack, script)) + .collect::, _>>()?; - maud_move!( - script type="module" src=(src) defer {} - ) + Ok(maud_move!( + @for tag in tags { + (tag) + } + )) +} + +fn emit_tag_script<'a>(sack: &'a Sack, script: &'a str) -> Result { + let src = sack + .get_script(script) + .ok_or(format!("Missing script {script}"))?; + + Ok(maud_move!(script type="module" src=(src.path.as_str()) defer {})) } diff --git a/src/html/home.rs b/src/html/home.rs index f089e87..d4a8f16 100644 --- a/src/html/home.rs +++ b/src/html/home.rs @@ -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() } diff --git a/src/html/list.rs b/src/html/list.rs index a2fb691..46ab536 100644 --- a/src/html/list.rs +++ b/src/html/list.rs @@ -7,7 +7,7 @@ pub fn list<'s, 'g, 'html>( sack: &'s Sack, groups: &'g [(i32, Vec)], title: String, -) -> impl Renderable + 'html +) -> Result where 's: 'html, 'g: 'html, diff --git a/src/html/mod.rs b/src/html/mod.rs index 09e3ec9..c706598 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -114,21 +114,23 @@ fn bare<'s, 'p, 'html>( main: impl Renderable + 'p, title: String, js: Option<&'s [String]>, -) -> impl Renderable + 'html +) -> Result where 's: 'html, 'p: 'html, { - maud_move!( + let head = head::render_head(sack, title, &[], js)?; + + Ok(maud_move!( (Raw("")) html lang="en" { - (head::render_head(sack, title, &[], js)) + (head) body { (main) } } - ) + )) } @@ -136,7 +138,7 @@ fn full<'s, 'p, 'html>( sack: &'s Sack, main: impl Renderable + 'p, title: String, -) -> impl Renderable + 'html +) -> Result where 's: 'html, 'p: 'html, @@ -154,7 +156,7 @@ fn page<'s, 'p, 'html>( main: impl Renderable + 'p, title: String, js: Option<&'s [String]>, -) -> impl Renderable + 'html +) -> Result where 's: 'html, 'p: 'html, @@ -185,10 +187,10 @@ pub(crate) fn to_list(sack: &Sack, list: Vec, title: String) -> String 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 where 's: 'html, { @@ -214,6 +216,7 @@ pub(crate) fn search<'s, 'html>(sack: &'s Sack) -> String { String::from("Search"), Some(&["search".into()]) ) + .unwrap() .render() .into() } @@ -292,6 +295,7 @@ pub(crate) fn flox<'p, 's, 'html>( String::from("Flox"), Some(&["editor".into()]) ) + .unwrap() .render() .into() } diff --git a/src/html/post.rs b/src/html/post.rs index c06b109..47587ec 100644 --- a/src/html/post.rs +++ b/src/html/post.rs @@ -33,6 +33,7 @@ impl Content for Post { bibliography: Bibliography, ) -> String { post(self, parsed, sack, outline, bibliography) + .unwrap() .render() .into() } @@ -55,7 +56,7 @@ pub fn post<'s, 'p, 'html>( sack: &'s Sack, outline: Outline, bibliography: Bibliography, -) -> impl Renderable + 'html +) -> Result where 's: 'html, 'p: 'html, diff --git a/src/html/slideshow.rs b/src/html/slideshow.rs index 8c0318e..fb8b64e 100644 --- a/src/html/slideshow.rs +++ b/src/html/slideshow.rs @@ -82,6 +82,7 @@ pub fn show(fm: &Slideshow, sack: &Sack, slides: &str) -> String { fm.title.clone(), Some(&["reveal".into()]) ) + .unwrap() .render() .into() } diff --git a/src/html/wiki.rs b/src/html/wiki.rs index aea78ef..f2594a7 100644 --- a/src/html/wiki.rs +++ b/src/html/wiki.rs @@ -82,6 +82,7 @@ fn wiki( ); crate::html::page(sack, main, matter.title.to_owned(), None) + .unwrap() .render() .into() } diff --git a/src/main.rs b/src/main.rs index 12c6599..144a6bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ fn main() { .js("editor", "./js/flox/main.ts") .js("lambda", "./js/flox/lambda.ts") .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(), ) .add_virtual(