router.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. use dioxus::prelude::*;
  2. use dioxus::router::Router;
  3. #[derive(PartialEq, Debug, Clone)]
  4. pub enum Route {
  5. // #[at("/")]
  6. Home,
  7. // #[at("/:id")]
  8. AllUsers { page: u32 },
  9. // #[at("/:id")]
  10. User { id: u32 },
  11. // #[at("/:id")]
  12. BlogList { page: u32 },
  13. // #[at("/:id")]
  14. BlogPost { post_id: u32 },
  15. // #[at("/404")]
  16. // #[not_found]
  17. NotFound,
  18. }
  19. static App: Component<()> = |cx, props| {
  20. let route = use_router(cx, Route::parse)?;
  21. cx.render(rsx! {
  22. nav {
  23. Link { to: Route::Home, "Go home!" }
  24. Link { to: Route::AllUsers { page: 0 }, "List all users" }
  25. Link { to: Route::BlogList { page: 0 }, "Blog posts" }
  26. }
  27. {match route {
  28. Route::Home => rsx!("Home"),
  29. Route::AllUsers { page } => rsx!("All users - page {page}"),
  30. Route::User { id } => rsx!("User - id: {id}"),
  31. Route::BlogList { page } => rsx!("Blog posts - page {page}"),
  32. Route::BlogPost { post_id } => rsx!("Blog post - post {post_id}"),
  33. Route::NotFound => rsx!("Not found"),
  34. }}
  35. footer {}
  36. })
  37. };
  38. impl Route {
  39. // Generate the appropriate route from the "tail" end of the URL
  40. fn parse(url: &str) -> Self {
  41. use Route::*;
  42. match url {
  43. "/" => Home,
  44. "/users" => AllUsers { page: 1 },
  45. "/users/:page" => AllUsers { page: 1 },
  46. "/users/:page/:id" => User { id: 1 },
  47. "/blog" => BlogList { page: 1 },
  48. "/blog/:page" => BlogList { page: 1 },
  49. "/blog/:page/:id" => BlogPost { post_id: 1 },
  50. _ => NotFound,
  51. }
  52. }
  53. }
  54. impl Routable for Route {
  55. fn from_path(path: &str, params: &std::collections::HashMap<&str, &str>) -> Option<Self> {
  56. todo!()
  57. }
  58. fn to_path(&self) -> String {
  59. todo!()
  60. }
  61. fn routes() -> Vec<&'static str> {
  62. todo!()
  63. }
  64. fn not_found_route() -> Option<Self> {
  65. todo!()
  66. }
  67. fn recognize(pathname: &str) -> Option<Self> {
  68. todo!()
  69. }
  70. }
  71. fn main() {}