Rust library to generate static websites
5
fork

Configure Feed

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

feat: more ergonomic type safe links

+25 -6
+3 -1
crates/framework/src/page.rs
··· 45 45 fn route_raw(&self) -> String; 46 46 fn route(&self, params: &RouteParams) -> String; 47 47 fn file_path(&self, params: &RouteParams) -> PathBuf; 48 - fn url(&self, params: &RouteParams) -> String; 48 + fn url<P: Into<RouteParams>>(params: P) -> String 49 + where 50 + Self: Sized; 49 51 } 50 52 51 53 pub trait FullPage: Page + InternalPage + DynamicPage + Sync {}
+13 -1
crates/macros/src/lib.rs
··· 94 94 std::path::PathBuf::from(format!(#file_path_for_route)) 95 95 } 96 96 97 - fn url(&self, params: &maudit::page::RouteParams) -> String { 97 + fn url<P: Into<maudit::page::RouteParams>>(params: P) -> String { 98 + let params = params.into(); 98 99 #(#list_params;)* 99 100 format!(#path_for_route) 100 101 } ··· 227 228 RouteParams(map) 228 229 } 229 230 } 231 + 232 + impl Into<RouteParams> for &#struct_name { 233 + fn into(self) -> RouteParams { 234 + let mut map = maudit::FxHashMap::default(); 235 + #( 236 + map.insert(stringify!(#fields).to_string(), self.#fields.to_string()); 237 + )* 238 + RouteParams(map) 239 + } 240 + } 241 + 230 242 }; 231 243 232 244 TokenStream::from(expanded)
+2 -2
crates/user-example/src/pages/dynamic.rs
··· 6 6 pub struct DynamicExample; 7 7 8 8 #[derive(Params)] 9 - struct Params { 10 - page: u128, 9 + pub struct Params { 10 + pub page: u128, 11 11 } 12 12 13 13 impl DynamicPage for DynamicExample {
+7 -2
crates/user-example/src/pages/index.rs
··· 1 1 use maud::html; 2 2 use maudit::maudit_macros::route; 3 - use maudit::page::{Page, RenderResult, RouteContext}; 3 + use maudit::page::{InternalPage, Page, RenderResult, RouteContext}; 4 + 5 + use super::dynamic::{DynamicExample, Params as DynamicExampleParams}; 4 6 5 7 #[route("/")] 6 8 pub struct Index; ··· 8 10 impl Page for Index { 9 11 fn render(&self, ctx: &mut RouteContext) -> RenderResult { 10 12 let image = ctx.assets.add_image("data/logo.svg".into()); 13 + let script = ctx.assets.add_script("data/some_other_script.js".into()); 11 14 12 - let script = ctx.assets.add_script("data/some_other_script.js".into()); 15 + let link_to_first_dynamic = DynamicExample::url(&DynamicExampleParams { page: 1 }); 16 + 17 + println!("Link to endpoint: {}", link_to_first_dynamic); 13 18 14 19 RenderResult::Html(html! { 15 20 h1 { "Index" }