Rust library to generate static websites

feat(routing): Add function to generate redirects (#85)

* feat(routing): Add function to generate redirects

* test: add

authored by

Erika and committed by
GitHub
14931c9b 3d8e928d

+70 -1
+21
.sampo/changesets/grumpy-windweaver-kalma.md
··· 1 + --- 2 + cargo/maudit: minor 3 + --- 4 + 5 + Adds a new `redirect()` function that can used to generate redirects to other pages and websites. This function is exported from the route prelude. 6 + 7 + ```rs 8 + use maudit::route::prelude::*; 9 + 10 + #[route("/redirect")] 11 + pub struct Redirect; 12 + 13 + impl Route for Redirect { 14 + fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> { 15 + redirect("https://example.com") 16 + 17 + // Use a page's url method to generate type safe links: 18 + // redirect(&OtherPage.url(None)) 19 + } 20 + } 21 + ```
+49 -1
crates/maudit/src/route.rs
··· 230 230 routes 231 231 } 232 232 233 + /// Generates the required HTML for a redirect to the specified URL. 234 + /// 235 + /// This function returns a RenderResult and as such can be used directly as a possible return value inside a page 236 + /// 237 + /// ## Example 238 + /// ```rust 239 + /// use maudit::route::prelude::*; 240 + /// 241 + /// # #[route("/other_page")] 242 + /// # pub struct OtherPage; 243 + /// # impl Route for OtherPage { 244 + /// # fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> {""} 245 + /// # } 246 + /// 247 + /// #[route("/redirect")] 248 + /// pub struct Redirect; 249 + /// 250 + /// impl Route for Redirect { 251 + /// fn render(&self, ctx: &mut PageContext) -> impl Into<RenderResult> { 252 + /// redirect("https://example.com") 253 + /// 254 + /// // Use a page's url method to generate type safe links: 255 + /// // redirect(&OtherPage.url(None)) 256 + /// } 257 + /// } 258 + /// ``` 259 + pub fn redirect(url: &str) -> RenderResult { 260 + RenderResult::Text(format!( 261 + r#"<meta http-equiv="refresh" content="0; url={}" />"#, 262 + url 263 + )) 264 + } 265 + 233 266 /// Allows to access various data and assets in a [`Route`] implementation. 234 267 /// 235 268 /// ## Example ··· 926 959 //! ``` 927 960 pub use super::{ 928 961 CachedRoute, DynamicRouteContext, FullRoute, Page, PageContext, PageParams, Pages, 929 - PaginatedContentPage, PaginationPage, RenderResult, Route, RouteExt, paginate, 962 + PaginatedContentPage, PaginationPage, RenderResult, Route, RouteExt, paginate, redirect, 930 963 }; 931 964 pub use crate::assets::{ 932 965 Asset, Image, ImageFormat, ImageOptions, ImagePlaceholder, RenderWithAlt, Script, Style, ··· 1301 1334 let expected = Path::new("/dist/articles/hello-world/index.html"); 1302 1335 1303 1336 assert_eq!(page.file_path(&route_params, output_dir), expected); 1337 + } 1338 + 1339 + #[test] 1340 + fn test_redirect_simple_url() { 1341 + let result = redirect("https://example.com"); 1342 + 1343 + match result { 1344 + RenderResult::Text(html) => { 1345 + assert_eq!( 1346 + html, 1347 + r#"<meta http-equiv="refresh" content="0; url=https://example.com" />"# 1348 + ); 1349 + } 1350 + _ => panic!("Expected RenderResult::Text variant"), 1351 + } 1304 1352 } 1305 1353 }