macro.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. use dioxus::prelude::*;
  2. use dioxus_router_core::*;
  3. use dioxus_router_macro::*;
  4. use std::str::FromStr;
  5. #[inline_props]
  6. fn Route1(cx: Scope, dynamic: String) -> Element {
  7. render! {
  8. div{
  9. "Route1: {dynamic}"
  10. }
  11. }
  12. }
  13. #[inline_props]
  14. fn Route2(cx: Scope) -> Element {
  15. render! {
  16. div{
  17. "Route2"
  18. }
  19. }
  20. }
  21. #[inline_props]
  22. fn Route3(cx: Scope, dynamic: u32) -> Element {
  23. render! {
  24. div{
  25. "Route3: {dynamic}"
  26. }
  27. }
  28. }
  29. #[inline_props]
  30. fn Route4(cx: Scope, number1: u32, number2: u32) -> Element {
  31. render! {
  32. div{
  33. "Route4: {number1} {number2}"
  34. }
  35. }
  36. }
  37. #[inline_props]
  38. fn Route5(cx: Scope, query: String) -> Element {
  39. render! {
  40. div{
  41. "Route5: {query}"
  42. }
  43. }
  44. }
  45. #[inline_props]
  46. fn Route6(cx: Scope, extra: Vec<String>) -> Element {
  47. render! {
  48. div{
  49. "Route5: {extra:?}"
  50. }
  51. }
  52. }
  53. #[derive(Routable, Clone, Debug, PartialEq)]
  54. enum Route {
  55. #[route("/(dynamic)" Route1)]
  56. Route1 { dynamic: String },
  57. #[route("/hello_world" Route2)]
  58. Route2 {},
  59. // #[redirect("/(dynamic)/hello_world")]
  60. #[route("/hello_world/(dynamic)" Route3)]
  61. Route3 { dynamic: u32 },
  62. #[route("/(number1)/(number2)" Route4)]
  63. Route4 { number1: u32, number2: u32 },
  64. #[route("/?(query)" Route5)]
  65. Route5 { query: String },
  66. #[route("/(...extra)" Route6)]
  67. Route6 { extra: Vec<String> },
  68. }
  69. #[test]
  70. fn display_works() {
  71. let route = Route::Route1 {
  72. dynamic: "hello".to_string(),
  73. };
  74. assert_eq!(route.to_string(), "/hello");
  75. let route = Route::Route3 { dynamic: 1234 };
  76. assert_eq!(route.to_string(), "/hello_world/1234");
  77. let route = Route::Route1 {
  78. dynamic: "hello_world2".to_string(),
  79. };
  80. assert_eq!(route.to_string(), "/hello_world2");
  81. let route = Route::Route4 {
  82. number1: 1234,
  83. number2: 5678,
  84. };
  85. assert_eq!(route.to_string(), "/1234/5678");
  86. let route = Route::Route5 {
  87. query: "hello".to_string(),
  88. };
  89. assert_eq!(route.to_string(), "/?hello");
  90. let route = Route::Route6 {
  91. extra: vec!["hello".to_string(), "world".to_string()],
  92. };
  93. assert_eq!(route.to_string(), "/hello/world");
  94. }
  95. #[test]
  96. fn from_string_works() {
  97. let w = "/hello";
  98. assert_eq!(
  99. Route::from_str(w),
  100. Ok(Route::Route1 {
  101. dynamic: "hello".to_string()
  102. })
  103. );
  104. let w = "/hello/";
  105. assert_eq!(
  106. Route::from_str(w),
  107. Ok(Route::Route1 {
  108. dynamic: "hello".to_string()
  109. })
  110. );
  111. let w = "/hello_world/1234";
  112. assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
  113. let w = "/hello_world/1234/";
  114. assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
  115. let w = "/hello_world2";
  116. assert_eq!(
  117. Route::from_str(w),
  118. Ok(Route::Route1 {
  119. dynamic: "hello_world2".to_string()
  120. })
  121. );
  122. let w = "/?x=1234&y=hello";
  123. assert_eq!(
  124. Route::from_str(w),
  125. Ok(Route::Route5 {
  126. query: "x=1234&y=hello".to_string()
  127. })
  128. );
  129. let w = "/hello_world/hello_world/hello_world";
  130. assert_eq!(
  131. Route::from_str(w),
  132. Ok(Route::Route6 {
  133. extra: vec![
  134. "hello_world".to_string(),
  135. "hello_world".to_string(),
  136. "hello_world".to_string()
  137. ]
  138. })
  139. );
  140. }
  141. #[test]
  142. fn round_trip() {
  143. // Route1
  144. let string = "hello_world2";
  145. let route = Route::Route1 {
  146. dynamic: string.to_string(),
  147. };
  148. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  149. // Route2
  150. for num in 0..100 {
  151. let route = Route::Route3 { dynamic: num };
  152. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  153. }
  154. // Route3
  155. for num1 in 0..100 {
  156. for num2 in 0..100 {
  157. let route = Route::Route4 {
  158. number1: num1,
  159. number2: num2,
  160. };
  161. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  162. }
  163. }
  164. // Route4
  165. let string = "x=1234&y=hello";
  166. let route = Route::Route5 {
  167. query: string.to_string(),
  168. };
  169. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  170. // Route5
  171. let route = Route::Route6 {
  172. extra: vec![
  173. "hello_world".to_string(),
  174. "hello_world".to_string(),
  175. "hello_world".to_string(),
  176. ],
  177. };
  178. assert_eq!(Route::from_str(&route.to_string()), Ok(route));
  179. }
  180. fn main() {}