Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: reduce boilerplate needed for assets and templates

+53 -24
+31 -10
crates/framework/src/assets.rs
··· 18 18 } 19 19 20 20 impl PageAssets { 21 - pub fn add_image(&mut self, image_path: PathBuf) -> Image { 22 - let image = Box::new(Image { path: image_path }); 21 + pub fn add_image<P>(&mut self, image_path: P) -> Image 22 + where 23 + P: Into<PathBuf>, 24 + { 25 + let image = Box::new(Image { 26 + path: image_path.into(), 27 + }); 23 28 24 29 self.assets.insert(image.clone()); 25 30 26 31 *image 27 32 } 28 33 29 - pub fn add_script(&mut self, script_path: PathBuf) -> Script { 30 - let script = Script { path: script_path }; 34 + pub fn add_script<P>(&mut self, script_path: P) -> Script 35 + where 36 + P: Into<PathBuf>, 37 + { 38 + let script = Script { 39 + path: script_path.into(), 40 + }; 31 41 32 42 self.scripts.insert(script.clone()); 33 43 34 44 script 35 45 } 36 46 37 - pub fn include_script(&mut self, script_path: PathBuf) { 38 - let script = Script { path: script_path }; 47 + pub fn include_script<P>(&mut self, script_path: P) 48 + where 49 + P: Into<PathBuf>, 50 + { 51 + let script = Script { 52 + path: script_path.into(), 53 + }; 39 54 40 55 self.scripts.insert(script.clone()); 41 56 self.included_scripts.push(script); 42 57 } 43 58 44 - pub fn add_style(&mut self, style_path: PathBuf, tailwind: bool) -> Style { 59 + pub fn add_style<P>(&mut self, style_path: P, tailwind: bool) -> Style 60 + where 61 + P: Into<PathBuf>, 62 + { 45 63 let style = Style { 46 - path: style_path, 64 + path: style_path.into(), 47 65 tailwind, 48 66 }; 49 67 ··· 52 70 style 53 71 } 54 72 55 - pub fn include_style(&mut self, style_path: PathBuf, tailwind: bool) { 73 + pub fn include_style<P>(&mut self, style_path: P, tailwind: bool) 74 + where 75 + P: Into<PathBuf>, 76 + { 56 77 let style = Style { 57 - path: style_path, 78 + path: style_path.into(), 58 79 tailwind, 59 80 }; 60 81
+6
crates/framework/src/page.rs
··· 9 9 Text(String), 10 10 } 11 11 12 + impl From<maud::Markup> for RenderResult<maud::Markup> { 13 + fn from(val: maud::Markup) -> Self { 14 + RenderResult::Html(val) 15 + } 16 + } 17 + 12 18 pub struct RouteContext<'a> { 13 19 pub params: RouteParams, 14 20 pub assets: &'a mut PageAssets,
+5 -4
crates/user-example/src/pages/dynamic.rs
··· 25 25 impl Page for DynamicExample { 26 26 fn render(&self, ctx: &mut RouteContext) -> RenderResult { 27 27 let params = ctx.params.parse_into::<Params>(); 28 - let image = ctx.assets.add_image("data/social-card.png".into()); 29 - ctx.assets.include_style("data/tailwind.css".into(), true); 28 + let image = ctx.assets.add_image("data/social-card.png"); 29 + ctx.assets.include_style("data/tailwind.css", true); 30 30 31 - RenderResult::Html(html! { 31 + html! { 32 32 head { 33 33 title { "Index" } 34 34 } 35 35 h1 { "Hello, world!" } 36 36 (image) 37 37 p { (params.page) } 38 - }) 38 + } 39 + .into() 39 40 } 40 41 }
+2 -3
crates/user-example/src/pages/endpoint.rs
··· 5 5 6 6 impl Page for Endpoint { 7 7 fn render(&self, ctx: &mut RouteContext) -> RenderResult { 8 - let image = ctx.assets.add_image("data/logo.svg".into()); 9 - 10 - let some_script = ctx.assets.add_script("data/script.js".into()); 8 + let image = ctx.assets.add_image("data/logo.svg"); 9 + let some_script = ctx.assets.add_script("data/script.js"); 11 10 12 11 // Return some JSON 13 12 RenderResult::Text(format!(
+3 -2
crates/user-example/src/pages/hello_world.rs
··· 6 6 7 7 impl Page for HelloWorld { 8 8 fn render(&self, _: &mut RouteContext) -> RenderResult { 9 - RenderResult::Html(html! { 9 + html! { 10 10 h1 { "Hello World" } 11 - }) 11 + } 12 + .into() 12 13 } 13 14 }
+6 -5
crates/user-example/src/pages/index.rs
··· 9 9 10 10 impl Page for Index { 11 11 fn render(&self, ctx: &mut RouteContext) -> RenderResult { 12 - let image = ctx.assets.add_image("data/logo.svg".into()); 13 - let script = ctx.assets.add_script("data/some_other_script.js".into()); 14 - let style = ctx.assets.add_style("data/tailwind.css".into(), true); 12 + let image = ctx.assets.add_image("data/logo.svg"); 13 + let script = ctx.assets.add_script("data/some_other_script.js"); 14 + let style = ctx.assets.add_style("data/tailwind.css", true); 15 15 16 16 let link_to_first_dynamic = DynamicExample::url_unsafe(&DynamicExampleParams { page: 1 }); 17 17 ··· 19 19 .url(&DynamicExampleParams { page: 0 }) 20 20 .unwrap(); 21 21 22 - RenderResult::Html(html! { 22 + html! { 23 23 head { 24 24 title { "Index" } 25 25 link rel="stylesheet" href=(style.url().unwrap()) {} ··· 29 29 script src=(script.url().unwrap()) {} 30 30 a."text-red-500" href=(link_to_first_dynamic) { "Go to first dynamic page" } 31 31 a href=(safe_link_to_first_dynamic) { "Go to first dynamic page (safe)" } 32 - }) 32 + } 33 + .into() 33 34 } 34 35 }