From 440d2922be067db393f20ac155f13602f636b628 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 24 Jul 2023 10:33:55 -0700 Subject: [PATCH 1/3] add is_child_of function to Routable --- packages/router/src/routable.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/router/src/routable.rs b/packages/router/src/routable.rs index b75770a51..5e93f3ea2 100644 --- a/packages/router/src/routable.rs +++ b/packages/router/src/routable.rs @@ -106,6 +106,22 @@ pub trait Routable: std::fmt::Display + std::str::FromStr + Clone + 'static { /// Render the route at the given level fn render<'a>(&self, cx: &'a ScopeState, level: usize) -> Element<'a>; + /// Checks if this route is a child of the given route + fn is_child_of(&self, other: &Self) -> bool { + let self_str = self.to_string(); + let self_str = self_str.trim_matches('/'); + let other_str = other.to_string(); + let other_str = other_str.trim_matches('/'); + let self_segments = self_str.split('/'); + let other_segments = other_str.split('/'); + for (self_seg, other_seg) in self_segments.zip(other_segments) { + if self_seg != other_seg { + return false; + } + } + true + } + /// Gets a list of all static routes fn static_routes() -> Vec { Self::SITE_MAP From 6760c2f961cddea7a5b8430ccea445a717ecbee4 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 24 Jul 2023 11:22:59 -0700 Subject: [PATCH 2/3] add doc example --- packages/router/src/routable.rs | 67 +++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/packages/router/src/routable.rs b/packages/router/src/routable.rs index 5e93f3ea2..9d6ac1a07 100644 --- a/packages/router/src/routable.rs +++ b/packages/router/src/routable.rs @@ -107,14 +107,41 @@ pub trait Routable: std::fmt::Display + std::str::FromStr + Clone + 'static { fn render<'a>(&self, cx: &'a ScopeState, level: usize) -> Element<'a>; /// Checks if this route is a child of the given route + /// + /// # Example + /// ```rust + /// use dioxus_router::prelude::*; + /// use dioxus::prelude::*; + /// + /// #[inline_props] + /// fn Home(cx: Scope) -> Element { todo!() } + /// #[inline_props] + /// fn About(cx: Scope) -> Element { todo!() } + /// + /// #[derive(Routable, Clone, PartialEq, Debug)] + /// enum Route { + /// #[route("/")] + /// Home {}, + /// #[route("/about")] + /// About {}, + /// } + /// + /// let route = Route::About {}; + /// let parent = Route::Home {}; + /// assert!(route.is_child_of(&parent)); + /// ``` fn is_child_of(&self, other: &Self) -> bool { let self_str = self.to_string(); let self_str = self_str.trim_matches('/'); let other_str = other.to_string(); let other_str = other_str.trim_matches('/'); + if other_str.is_empty() { + return true; + } let self_segments = self_str.split('/'); let other_segments = other_str.split('/'); for (self_seg, other_seg) in self_segments.zip(other_segments) { + dbg!(self_seg, other_seg); if self_seg != other_seg { return false; } @@ -122,6 +149,46 @@ pub trait Routable: std::fmt::Display + std::str::FromStr + Clone + 'static { true } + /// Get the parent route of this route + /// + /// # Example + /// ```rust + /// use dioxus_router::prelude::*; + /// use dioxus::prelude::*; + /// + /// #[inline_props] + /// fn Home(cx: Scope) -> Element { todo!() } + /// #[inline_props] + /// fn About(cx: Scope) -> Element { todo!() } + /// + /// #[derive(Routable, Clone, PartialEq, Debug)] + /// enum Route { + /// #[route("/")] + /// Home {}, + /// #[route("/about")] + /// About {}, + /// } + /// + /// let route = Route::About {}; + /// let parent = route.parent().unwrap(); + /// assert_eq!(parent, Route::Home {}); + /// ``` + fn parent(&self) -> Option { + let as_str = self.to_string(); + let as_str = as_str.trim_matches('/'); + let segments = as_str.split('/'); + let segment_count = segments.clone().count(); + let new_route = segments + .take(segment_count - 1) + .fold(String::new(), |mut acc, segment| { + acc.push_str("/"); + acc.push_str(segment); + acc + }); + + Self::from_str(&new_route).ok() + } + /// Gets a list of all static routes fn static_routes() -> Vec { Self::SITE_MAP From 1ddda0cc1741df12d1a98a86667925450b233afb Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 24 Jul 2023 11:30:52 -0700 Subject: [PATCH 3/3] fix clippy --- packages/router/src/routable.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/router/src/routable.rs b/packages/router/src/routable.rs index 9d6ac1a07..2eb7f5b7b 100644 --- a/packages/router/src/routable.rs +++ b/packages/router/src/routable.rs @@ -181,7 +181,7 @@ pub trait Routable: std::fmt::Display + std::str::FromStr + Clone + 'static { let new_route = segments .take(segment_count - 1) .fold(String::new(), |mut acc, segment| { - acc.push_str("/"); + acc.push('/'); acc.push_str(segment); acc });