v2.rs 2.1 KB

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