lib.rs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. #![doc(html_logo_url = "https://avatars.githubusercontent.com/u/79236386")]
  2. #![doc(html_favicon_url = "https://avatars.githubusercontent.com/u/79236386")]
  3. extern crate proc_macro;
  4. use layout::Layout;
  5. use nest::{Nest, NestId};
  6. use proc_macro::TokenStream;
  7. use quote::{__private::Span, format_ident, quote, ToTokens};
  8. use redirect::Redirect;
  9. use route::{Route, RouteType};
  10. use segment::RouteSegment;
  11. use syn::{parse::ParseStream, parse_macro_input, Ident, Token, Type};
  12. use proc_macro2::TokenStream as TokenStream2;
  13. use crate::{layout::LayoutId, route_tree::ParseRouteTree};
  14. mod hash;
  15. mod layout;
  16. mod nest;
  17. mod query;
  18. mod redirect;
  19. mod route;
  20. mod route_tree;
  21. mod segment;
  22. /// Derives the Routable trait for an enum of routes
  23. ///
  24. /// Each variant must:
  25. /// 1. Be struct-like with {}'s
  26. /// 2. Contain all of the dynamic parameters of the current and nested routes
  27. /// 3. Have a `#[route("route")]` attribute
  28. ///
  29. /// Route Segments:
  30. /// 1. Static Segments: "/static"
  31. /// 2. Dynamic Segments: "/:dynamic" (where dynamic has a type that is FromStr in all child Variants)
  32. /// 3. Catch all Segments: "/:..segments" (where segments has a type that is FromSegments in all child Variants)
  33. /// 4. Query Segments: "/?:..query" (where query has a type that is FromQuery in all child Variants) or "/?:query&:other_query" (where query and other_query has a type that is FromQueryArgument in all child Variants)
  34. ///
  35. /// Routes are matched:
  36. /// 1. By there specificity this order: Query Routes ("/?:query"), Static Routes ("/route"), Dynamic Routes ("/:route"), Catch All Routes ("/:..route")
  37. /// 2. By the order they are defined in the enum
  38. ///
  39. /// All features:
  40. /// ```rust
  41. /// use dioxus::prelude::*;
  42. ///
  43. /// #[rustfmt::skip]
  44. /// #[derive(Clone, Debug, PartialEq, Routable)]
  45. /// enum Route {
  46. /// // Define routes with the route macro. If the name of the component is not the same as the variant, you can specify it as the second parameter
  47. /// #[route("/", IndexComponent)]
  48. /// Index {},
  49. /// // Nests with parameters have types taken from child routes
  50. /// // Everything inside the nest has the added parameter `user_id: usize`
  51. /// #[nest("/user/:user_id")]
  52. /// // All children of layouts will be rendered inside the Outlet in the layout component
  53. /// // Creates a Layout UserFrame that has the parameter `user_id: usize`
  54. /// #[layout(UserFrame)]
  55. /// // If there is a component with the name Route1, you do not need to pass in the component name
  56. /// #[route("/:dynamic?:query")]
  57. /// Route1 {
  58. /// // The type is taken from the first instance of the dynamic parameter
  59. /// user_id: usize,
  60. /// dynamic: usize,
  61. /// query: String,
  62. /// },
  63. /// #[route("/hello_world")]
  64. /// // You can opt out of the layout by using the `!` prefix
  65. /// #[layout(!UserFrame)]
  66. /// Route2 { user_id: usize },
  67. /// // End layouts with #[end_layout]
  68. /// #[end_layout]
  69. /// // End nests with #[end_nest]
  70. /// #[end_nest]
  71. /// // Redirects take a path and a function that takes the parameters from the path and returns a new route
  72. /// #[redirect("/:id/user", |id: usize| Route::Route3 { dynamic: id.to_string()})]
  73. /// #[route("/:dynamic")]
  74. /// Route3 { dynamic: String },
  75. /// }
  76. /// # #[component]
  77. /// # fn Route1(user_id: usize, dynamic: usize, query: String) -> Element { VNode::empty() }
  78. /// # #[component]
  79. /// # fn Route2(user_id: usize) -> Element { VNode::empty() }
  80. /// # #[component]
  81. /// # fn Route3(dynamic: String) -> Element { VNode::empty() }
  82. /// # #[component]
  83. /// # fn UserFrame(user_id: usize) -> Element { VNode::empty() }
  84. /// # #[component]
  85. /// # fn IndexComponent() -> Element { VNode::empty() }
  86. /// ```
  87. ///
  88. /// # `#[route("path", component)]`
  89. ///
  90. /// The `#[route]` attribute is used to define a route. It takes up to 2 parameters:
  91. /// - `path`: The path to the enum variant (relative to the parent nest)
  92. /// - (optional) `component`: The component to render when the route is matched. If not specified, the name of the variant is used
  93. ///
  94. /// Routes are the most basic attribute. They allow you to define a route and the component to render when the route is matched. The component must take all dynamic parameters of the route and all parent nests.
  95. /// The next variant will be tied to the component. If you link to that variant, the component will be rendered.
  96. ///
  97. /// ```rust
  98. /// use dioxus::prelude::*;
  99. ///
  100. /// #[derive(Clone, Debug, PartialEq, Routable)]
  101. /// enum Route {
  102. /// // Define routes that renders the IndexComponent
  103. /// // The Index component will be rendered when the route is matched (e.g. when the user navigates to /)
  104. /// #[route("/", Index)]
  105. /// Index {},
  106. /// }
  107. /// # #[component]
  108. /// # fn Index() -> Element { VNode::empty() }
  109. /// ```
  110. ///
  111. /// # `#[redirect("path", function)]`
  112. ///
  113. /// The `#[redirect]` attribute is used to define a redirect. It takes 2 parameters:
  114. /// - `path`: The path to the enum variant (relative to the parent nest)
  115. /// - `function`: A function that takes the parameters from the path and returns a new route
  116. ///
  117. /// ```rust
  118. /// use dioxus::prelude::*;
  119. ///
  120. /// #[derive(Clone, Debug, PartialEq, Routable)]
  121. /// enum Route {
  122. /// // Redirects the /:id route to the Index route
  123. /// #[redirect("/:id", |id: usize| Route::Index {})]
  124. /// #[route("/", Index)]
  125. /// Index {},
  126. /// }
  127. /// # #[component]
  128. /// # fn Index() -> Element { VNode::empty() }
  129. /// ```
  130. ///
  131. /// Redirects allow you to redirect a route to another route. The function must take all dynamic parameters of the route and all parent nests.
  132. ///
  133. /// # `#[nest("path")]`
  134. ///
  135. /// The `#[nest]` attribute is used to define a nest. It takes 1 parameter:
  136. /// - `path`: The path to the nest (relative to the parent nest)
  137. ///
  138. /// Nests effect all nests, routes and redirects defined until the next `#[end_nest]` attribute. All children of nests are relative to the nest route and must include all dynamic parameters of the nest.
  139. ///
  140. /// ```rust
  141. /// use dioxus::prelude::*;
  142. ///
  143. /// #[derive(Clone, Debug, PartialEq, Routable)]
  144. /// enum Route {
  145. /// // Nests all child routes in the /blog route
  146. /// #[nest("/blog")]
  147. /// // This is at /blog/:id
  148. /// #[redirect("/:id", |id: usize| Route::Index {})]
  149. /// // This is at /blog
  150. /// #[route("/", Index)]
  151. /// Index {},
  152. /// }
  153. /// # #[component]
  154. /// # fn Index() -> Element { VNode::empty() }
  155. /// ```
  156. ///
  157. /// # `#[end_nest]`
  158. ///
  159. /// The `#[end_nest]` attribute is used to end a nest. It takes no parameters.
  160. ///
  161. /// ```rust
  162. /// use dioxus::prelude::*;
  163. ///
  164. /// #[derive(Clone, Debug, PartialEq, Routable)]
  165. /// enum Route {
  166. /// #[nest("/blog")]
  167. /// // This is at /blog/:id
  168. /// #[redirect("/:id", |id: usize| Route::Index {})]
  169. /// // This is at /blog
  170. /// #[route("/", Index)]
  171. /// Index {},
  172. /// // Ends the nest
  173. /// #[end_nest]
  174. /// // This is at /
  175. /// #[route("/")]
  176. /// Home {},
  177. /// }
  178. /// # #[component]
  179. /// # fn Index() -> Element { VNode::empty() }
  180. /// # #[component]
  181. /// # fn Home() -> Element { VNode::empty() }
  182. /// ```
  183. ///
  184. /// # `#[layout(component)]`
  185. ///
  186. /// The `#[layout]` attribute is used to define a layout. It takes 1 parameter:
  187. /// - `component`: The component to render when the route is matched. If not specified, the name of the variant is used
  188. ///
  189. /// The layout component allows you to wrap all children of the layout in a component. The child routes are rendered in the Outlet of the layout component. The layout component must take all dynamic parameters of the nests it is nested in.
  190. ///
  191. /// ```rust
  192. /// use dioxus::prelude::*;
  193. ///
  194. /// #[derive(Clone, Debug, PartialEq, Routable)]
  195. /// enum Route {
  196. /// #[layout(BlogFrame)]
  197. /// #[redirect("/:id", |id: usize| Route::Index {})]
  198. /// // Index will be rendered in the Outlet of the BlogFrame component
  199. /// #[route("/", Index)]
  200. /// Index {},
  201. /// }
  202. /// # #[component]
  203. /// # fn Index() -> Element { VNode::empty() }
  204. /// # #[component]
  205. /// # fn BlogFrame() -> Element { VNode::empty() }
  206. /// ```
  207. ///
  208. /// # `#[end_layout]`
  209. ///
  210. /// The `#[end_layout]` attribute is used to end a layout. It takes no parameters.
  211. ///
  212. /// ```rust
  213. /// use dioxus::prelude::*;
  214. ///
  215. /// #[derive(Clone, Debug, PartialEq, Routable)]
  216. /// enum Route {
  217. /// #[layout(BlogFrame)]
  218. /// #[redirect("/:id", |id: usize| Route::Index {})]
  219. /// // Index will be rendered in the Outlet of the BlogFrame component
  220. /// #[route("/", Index)]
  221. /// Index {},
  222. /// // Ends the layout
  223. /// #[end_layout]
  224. /// // This will be rendered standalone
  225. /// #[route("/")]
  226. /// Home {},
  227. /// }
  228. /// # #[component]
  229. /// # fn Index() -> Element { VNode::empty() }
  230. /// # #[component]
  231. /// # fn BlogFrame() -> Element { VNode::empty() }
  232. /// # #[component]
  233. /// # fn Home() -> Element { VNode::empty() }
  234. /// ```
  235. #[doc(alias = "route")]
  236. #[proc_macro_derive(
  237. Routable,
  238. attributes(route, nest, end_nest, layout, end_layout, redirect, child)
  239. )]
  240. pub fn routable(input: TokenStream) -> TokenStream {
  241. let routes_enum = parse_macro_input!(input as syn::ItemEnum);
  242. let route_enum = match RouteEnum::parse(routes_enum) {
  243. Ok(route_enum) => route_enum,
  244. Err(err) => return err.to_compile_error().into(),
  245. };
  246. let error_type = route_enum.error_type();
  247. let parse_impl = route_enum.parse_impl();
  248. let display_impl = route_enum.impl_display();
  249. let routable_impl = route_enum.routable_impl();
  250. (quote! {
  251. #error_type
  252. #display_impl
  253. #routable_impl
  254. #parse_impl
  255. })
  256. .into()
  257. }
  258. struct RouteEnum {
  259. name: Ident,
  260. endpoints: Vec<RouteEndpoint>,
  261. nests: Vec<Nest>,
  262. layouts: Vec<Layout>,
  263. site_map: Vec<SiteMapSegment>,
  264. }
  265. impl RouteEnum {
  266. fn parse(data: syn::ItemEnum) -> syn::Result<Self> {
  267. let name = &data.ident;
  268. let mut site_map = Vec::new();
  269. let mut site_map_stack: Vec<Vec<SiteMapSegment>> = Vec::new();
  270. let mut endpoints = Vec::new();
  271. let mut layouts: Vec<Layout> = Vec::new();
  272. let mut layout_stack = Vec::new();
  273. let mut nests = Vec::new();
  274. let mut nest_stack = Vec::new();
  275. for variant in &data.variants {
  276. let mut excluded = Vec::new();
  277. // Apply the any nesting attributes in order
  278. for attr in &variant.attrs {
  279. if attr.path().is_ident("nest") {
  280. let mut children_routes = Vec::new();
  281. {
  282. // add all of the variants of the enum to the children_routes until we hit an end_nest
  283. let mut level = 0;
  284. 'o: for variant in &data.variants {
  285. children_routes.push(variant.fields.clone());
  286. for attr in &variant.attrs {
  287. if attr.path().is_ident("nest") {
  288. level += 1;
  289. } else if attr.path().is_ident("end_nest") {
  290. level -= 1;
  291. if level < 0 {
  292. break 'o;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. let nest_index = nests.len();
  299. let parser = |input: ParseStream| {
  300. Nest::parse(
  301. input,
  302. children_routes
  303. .iter()
  304. .filter_map(|f: &syn::Fields| match f {
  305. syn::Fields::Named(fields) => Some(fields.clone()),
  306. _ => None,
  307. })
  308. .collect(),
  309. nest_index,
  310. )
  311. };
  312. let nest = attr.parse_args_with(parser)?;
  313. // add the current segment to the site map stack
  314. let segments: Vec<_> = nest
  315. .segments
  316. .iter()
  317. .map(|seg| {
  318. let segment_type = seg.into();
  319. SiteMapSegment {
  320. segment_type,
  321. children: Vec::new(),
  322. }
  323. })
  324. .collect();
  325. if !segments.is_empty() {
  326. site_map_stack.push(segments);
  327. }
  328. nests.push(nest);
  329. nest_stack.push(NestId(nest_index));
  330. } else if attr.path().is_ident("end_nest") {
  331. nest_stack.pop();
  332. // pop the current nest segment off the stack and add it to the parent or the site map
  333. if let Some(segment) = site_map_stack.pop() {
  334. let children = site_map_stack
  335. .last_mut()
  336. .map(|seg| &mut seg.last_mut().unwrap().children)
  337. .unwrap_or(&mut site_map);
  338. // Turn the list of segments in the segments stack into a tree
  339. let mut iter = segment.into_iter().rev();
  340. let mut current = iter.next().unwrap();
  341. for mut segment in iter {
  342. segment.children.push(current);
  343. current = segment;
  344. }
  345. children.push(current);
  346. }
  347. } else if attr.path().is_ident("layout") {
  348. let parser = |input: ParseStream| {
  349. let bang: Option<Token![!]> = input.parse().ok();
  350. let exclude = bang.is_some();
  351. Ok((exclude, Layout::parse(input, nest_stack.clone())?))
  352. };
  353. let (exclude, layout): (bool, Layout) = attr.parse_args_with(parser)?;
  354. if exclude {
  355. let Some(layout_index) = layouts.iter().position(|l| l.comp == layout.comp)
  356. else {
  357. return Err(syn::Error::new(
  358. Span::call_site(),
  359. "Attempted to exclude a layout that does not exist",
  360. ));
  361. };
  362. excluded.push(LayoutId(layout_index));
  363. } else {
  364. let layout_index = layouts.len();
  365. layouts.push(layout);
  366. layout_stack.push(LayoutId(layout_index));
  367. }
  368. } else if attr.path().is_ident("end_layout") {
  369. layout_stack.pop();
  370. } else if attr.path().is_ident("redirect") {
  371. let parser = |input: ParseStream| {
  372. Redirect::parse(input, nest_stack.clone(), endpoints.len())
  373. };
  374. let redirect = attr.parse_args_with(parser)?;
  375. endpoints.push(RouteEndpoint::Redirect(redirect));
  376. }
  377. }
  378. let active_nests = nest_stack.clone();
  379. let mut active_layouts = layout_stack.clone();
  380. active_layouts.retain(|&id| !excluded.contains(&id));
  381. let route = Route::parse(active_nests, active_layouts, variant.clone())?;
  382. // add the route to the site map
  383. let mut segment = SiteMapSegment::new(&route.segments);
  384. if let RouteType::Child(child) = &route.ty {
  385. let new_segment = SiteMapSegment {
  386. segment_type: SegmentType::Child(child.ty.clone()),
  387. children: Vec::new(),
  388. };
  389. match &mut segment {
  390. Some(segment) => {
  391. fn set_last_child_to(
  392. segment: &mut SiteMapSegment,
  393. new_segment: SiteMapSegment,
  394. ) {
  395. if let Some(last) = segment.children.last_mut() {
  396. set_last_child_to(last, new_segment);
  397. } else {
  398. segment.children = vec![new_segment];
  399. }
  400. }
  401. set_last_child_to(segment, new_segment);
  402. }
  403. None => {
  404. segment = Some(new_segment);
  405. }
  406. }
  407. }
  408. if let Some(segment) = segment {
  409. let parent = site_map_stack.last_mut();
  410. let children = match parent {
  411. Some(parent) => &mut parent.last_mut().unwrap().children,
  412. None => &mut site_map,
  413. };
  414. children.push(segment);
  415. }
  416. endpoints.push(RouteEndpoint::Route(route));
  417. }
  418. // pop any remaining site map segments
  419. while let Some(segment) = site_map_stack.pop() {
  420. let children = site_map_stack
  421. .last_mut()
  422. .map(|seg| &mut seg.last_mut().unwrap().children)
  423. .unwrap_or(&mut site_map);
  424. // Turn the list of segments in the segments stack into a tree
  425. let mut iter = segment.into_iter().rev();
  426. let mut current = iter.next().unwrap();
  427. for mut segment in iter {
  428. segment.children.push(current);
  429. current = segment;
  430. }
  431. children.push(current);
  432. }
  433. let myself = Self {
  434. name: name.clone(),
  435. endpoints,
  436. nests,
  437. layouts,
  438. site_map,
  439. };
  440. // If we're on the web, only the URL history is preserved between navigation. We need to warn the user that the segment is not present in the URL.
  441. if cfg!(feature = "web") {
  442. for variant in &data.variants {
  443. for field in &variant.fields {
  444. if !myself.field_present_in_url(field.ident.as_ref().unwrap()) {
  445. return Err(syn::Error::new_spanned(
  446. field.ident.as_ref().unwrap(),
  447. format!("The `{}` field must be present in the url for the web history. You can include the field in the url by using the `#[route(\"/:{}\")]` attribute on the enum variant.", field.ident.as_ref().unwrap(), field.ident.as_ref().unwrap()),
  448. ));
  449. }
  450. }
  451. }
  452. }
  453. Ok(myself)
  454. }
  455. fn field_present_in_url(&self, field: &Ident) -> bool {
  456. let mut from_route = false;
  457. for nest in &self.nests {
  458. if nest.dynamic_segments_names().any(|i| &i == field) {
  459. from_route = true
  460. }
  461. }
  462. for route in &self.endpoints {
  463. match route {
  464. RouteEndpoint::Route(route) => match &route.ty {
  465. RouteType::Child(child) => {
  466. if let Some(child) = child.ident.as_ref() {
  467. if child == "child" {
  468. from_route = true
  469. }
  470. }
  471. }
  472. RouteType::Leaf { .. } => {
  473. for segment in &route.segments {
  474. if segment.name().as_ref() == Some(field) {
  475. from_route = true
  476. }
  477. }
  478. if let Some(query) = &route.query {
  479. if query.contains_ident(field) {
  480. from_route = true
  481. }
  482. }
  483. if let Some(hash) = &route.hash {
  484. if hash.contains_ident(field) {
  485. from_route = true
  486. }
  487. }
  488. }
  489. },
  490. RouteEndpoint::Redirect(redirect) => {
  491. for segment in &redirect.segments {
  492. if segment.name().as_ref() == Some(field) {
  493. from_route = true
  494. }
  495. }
  496. if let Some(query) = &redirect.query {
  497. if query.contains_ident(field) {
  498. from_route = true
  499. }
  500. }
  501. if let Some(hash) = &redirect.hash {
  502. if hash.contains_ident(field) {
  503. from_route = true
  504. }
  505. }
  506. }
  507. }
  508. }
  509. from_route
  510. }
  511. fn impl_display(&self) -> TokenStream2 {
  512. let mut display_match = Vec::new();
  513. for route in &self.endpoints {
  514. if let RouteEndpoint::Route(route) = route {
  515. display_match.push(route.display_match(&self.nests));
  516. }
  517. }
  518. let name = &self.name;
  519. quote! {
  520. impl std::fmt::Display for #name {
  521. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  522. #[allow(unused)]
  523. match self {
  524. #(#display_match)*
  525. }
  526. Ok(())
  527. }
  528. }
  529. }
  530. }
  531. fn parse_impl(&self) -> TokenStream2 {
  532. let tree = ParseRouteTree::new(&self.endpoints, &self.nests);
  533. let name = &self.name;
  534. let error_name = format_ident!("{}MatchError", self.name);
  535. let tokens = tree.roots.iter().map(|&id| {
  536. let route = tree.get(id).unwrap();
  537. route.to_tokens(&self.nests, &tree, self.name.clone(), error_name.clone())
  538. });
  539. quote! {
  540. impl<'a> core::convert::TryFrom<&'a str> for #name {
  541. type Error = <Self as std::str::FromStr>::Err;
  542. fn try_from(s: &'a str) -> ::std::result::Result<Self, Self::Error> {
  543. s.parse()
  544. }
  545. }
  546. impl std::str::FromStr for #name {
  547. type Err = dioxus_router::routable::RouteParseError<#error_name>;
  548. fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
  549. let route = s;
  550. let (route, hash) = route.split_once('#').unwrap_or((route, ""));
  551. let (route, query) = route.split_once('?').unwrap_or((route, ""));
  552. // Remove any trailing slashes. We parse /route/ and /route in the same way
  553. // Note: we don't use trim because it includes more code
  554. let route = route.strip_suffix('/').unwrap_or(route);
  555. let query = dioxus_router::exports::urlencoding::decode(query).unwrap_or(query.into());
  556. let hash = dioxus_router::exports::urlencoding::decode(hash).unwrap_or(hash.into());
  557. let mut segments = route.split('/').map(|s| dioxus_router::exports::urlencoding::decode(s).unwrap_or(s.into()));
  558. // skip the first empty segment
  559. if s.starts_with('/') {
  560. let _ = segments.next();
  561. } else {
  562. // if this route does not start with a slash, it is not a valid route
  563. return Err(dioxus_router::routable::RouteParseError {
  564. attempted_routes: Vec::new(),
  565. });
  566. }
  567. let mut errors = Vec::new();
  568. #(#tokens)*
  569. Err(dioxus_router::routable::RouteParseError {
  570. attempted_routes: errors,
  571. })
  572. }
  573. }
  574. }
  575. }
  576. fn error_name(&self) -> Ident {
  577. Ident::new(&(self.name.to_string() + "MatchError"), Span::call_site())
  578. }
  579. fn error_type(&self) -> TokenStream2 {
  580. let match_error_name = self.error_name();
  581. let mut type_defs = Vec::new();
  582. let mut error_variants = Vec::new();
  583. let mut display_match = Vec::new();
  584. for endpoint in &self.endpoints {
  585. match endpoint {
  586. RouteEndpoint::Route(route) => {
  587. let route_name = &route.route_name;
  588. let error_name = route.error_ident();
  589. let route_str = &route.route;
  590. let comment = format!(
  591. " An error that can occur when trying to parse the route [`{}::{}`] ('{}').",
  592. self.name,
  593. route_name,
  594. route_str
  595. );
  596. error_variants.push(quote! {
  597. #[doc = #comment]
  598. #route_name(#error_name)
  599. });
  600. display_match.push(quote! { Self::#route_name(err) => write!(f, "Route '{}' ('{}') did not match:\n{}", stringify!(#route_name), #route_str, err)? });
  601. type_defs.push(route.error_type());
  602. }
  603. RouteEndpoint::Redirect(redirect) => {
  604. let error_variant = redirect.error_variant();
  605. let error_name = redirect.error_ident();
  606. let route_str = &redirect.route;
  607. let comment = format!(
  608. " An error that can occur when trying to parse the redirect '{}'.",
  609. route_str.value()
  610. );
  611. error_variants.push(quote! {
  612. #[doc = #comment]
  613. #error_variant(#error_name)
  614. });
  615. display_match.push(quote! { Self::#error_variant(err) => write!(f, "Redirect '{}' ('{}') did not match:\n{}", stringify!(#error_name), #route_str, err)? });
  616. type_defs.push(redirect.error_type());
  617. }
  618. }
  619. }
  620. for nest in &self.nests {
  621. let error_variant = nest.error_variant();
  622. let error_name = nest.error_ident();
  623. let route_str = &nest.route;
  624. let comment = format!(
  625. " An error that can occur when trying to parse the nested segment {} ('{}').",
  626. error_name, route_str
  627. );
  628. error_variants.push(quote! {
  629. #[doc = #comment]
  630. #error_variant(#error_name)
  631. });
  632. display_match.push(quote! { Self::#error_variant(err) => write!(f, "Nest '{}' ('{}') did not match:\n{}", stringify!(#error_name), #route_str, err)? });
  633. type_defs.push(nest.error_type());
  634. }
  635. let comment = format!(
  636. " An error that can occur when trying to parse the route enum [`{}`].",
  637. self.name
  638. );
  639. quote! {
  640. #(#type_defs)*
  641. #[doc = #comment]
  642. #[allow(non_camel_case_types)]
  643. #[allow(clippy::derive_partial_eq_without_eq)]
  644. pub enum #match_error_name {
  645. #(#error_variants),*
  646. }
  647. impl std::fmt::Debug for #match_error_name {
  648. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  649. write!(f, "{}({})", stringify!(#match_error_name), self)
  650. }
  651. }
  652. impl std::fmt::Display for #match_error_name {
  653. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  654. match self {
  655. #(#display_match),*
  656. }
  657. Ok(())
  658. }
  659. }
  660. }
  661. }
  662. fn routable_impl(&self) -> TokenStream2 {
  663. let name = &self.name;
  664. let site_map = &self.site_map;
  665. let mut matches = Vec::new();
  666. // Collect all routes matches
  667. for route in &self.endpoints {
  668. if let RouteEndpoint::Route(route) = route {
  669. matches.push(route.routable_match(&self.layouts, &self.nests));
  670. }
  671. }
  672. quote! {
  673. impl dioxus_router::routable::Routable for #name where Self: Clone {
  674. const SITE_MAP: &'static [dioxus_router::routable::SiteMapSegment] = &[
  675. #(#site_map,)*
  676. ];
  677. fn render(&self, level: usize) -> dioxus_core::Element {
  678. let myself = self.clone();
  679. match (level, myself) {
  680. #(#matches)*
  681. _ => VNode::empty()
  682. }
  683. }
  684. }
  685. }
  686. }
  687. }
  688. enum RouteEndpoint {
  689. Route(Route),
  690. Redirect(Redirect),
  691. }
  692. struct SiteMapSegment {
  693. pub segment_type: SegmentType,
  694. pub children: Vec<SiteMapSegment>,
  695. }
  696. impl SiteMapSegment {
  697. fn new(segments: &[RouteSegment]) -> Option<Self> {
  698. let mut current = None;
  699. // walk backwards through the new segments, adding children as we go
  700. for segment in segments.iter().rev() {
  701. let segment_type = segment.into();
  702. let mut segment = SiteMapSegment {
  703. segment_type,
  704. children: Vec::new(),
  705. };
  706. // if we have a current segment, add it as a child
  707. if let Some(current) = current.take() {
  708. segment.children.push(current)
  709. }
  710. current = Some(segment);
  711. }
  712. current
  713. }
  714. }
  715. impl ToTokens for SiteMapSegment {
  716. fn to_tokens(&self, tokens: &mut TokenStream2) {
  717. let segment_type = &self.segment_type;
  718. let children = if let SegmentType::Child(ty) = &self.segment_type {
  719. quote! { #ty::SITE_MAP }
  720. } else {
  721. let children = self
  722. .children
  723. .iter()
  724. .map(|child| child.to_token_stream())
  725. .collect::<Vec<_>>();
  726. quote! {
  727. &[
  728. #(#children,)*
  729. ]
  730. }
  731. };
  732. tokens.extend(quote! {
  733. dioxus_router::routable::SiteMapSegment {
  734. segment_type: #segment_type,
  735. children: #children,
  736. }
  737. });
  738. }
  739. }
  740. enum SegmentType {
  741. Static(String),
  742. Dynamic(String),
  743. CatchAll(String),
  744. Child(Type),
  745. }
  746. impl ToTokens for SegmentType {
  747. fn to_tokens(&self, tokens: &mut TokenStream2) {
  748. match self {
  749. SegmentType::Static(s) => {
  750. tokens.extend(quote! { dioxus_router::routable::SegmentType::Static(#s) })
  751. }
  752. SegmentType::Dynamic(s) => {
  753. tokens.extend(quote! { dioxus_router::routable::SegmentType::Dynamic(#s) })
  754. }
  755. SegmentType::CatchAll(s) => {
  756. tokens.extend(quote! { dioxus_router::routable::SegmentType::CatchAll(#s) })
  757. }
  758. SegmentType::Child(_) => {
  759. tokens.extend(quote! { dioxus_router::routable::SegmentType::Child })
  760. }
  761. }
  762. }
  763. }
  764. impl<'a> From<&'a RouteSegment> for SegmentType {
  765. fn from(value: &'a RouteSegment) -> Self {
  766. match value {
  767. RouteSegment::Static(s) => SegmentType::Static(s.to_string()),
  768. RouteSegment::Dynamic(s, _) => SegmentType::Dynamic(s.to_string()),
  769. RouteSegment::CatchAll(s, _) => SegmentType::CatchAll(s.to_string()),
  770. }
  771. }
  772. }