From e7a9161066b80c293522a008d35756ddbc817914 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Wed, 26 Jul 2023 10:18:39 -0700 Subject: [PATCH] fix link component --- examples/read_size.rs | 2 +- packages/cli/src/cli/check/mod.rs | 8 +++++-- packages/router/src/components/link.rs | 14 ++++++------ packages/router/src/contexts/router.rs | 30 ++++++++++++++++++++------ packages/router/src/history/mod.rs | 26 ++++------------------ packages/router/src/routable.rs | 1 - 6 files changed, 42 insertions(+), 39 deletions(-) diff --git a/examples/read_size.rs b/examples/read_size.rs index 20635eeab..806f71f80 100644 --- a/examples/read_size.rs +++ b/examples/read_size.rs @@ -48,7 +48,7 @@ fn app(cx: Scope) -> Element { let read = div_element.read(); let client_rect = read.as_ref().map(|el| el.get_client_rect()); if let Some(client_rect) = client_rect { - if let Ok(rect) = dbg!(client_rect.await) { + if let Ok(rect) = client_rect.await { dimentions.set(rect); } } diff --git a/packages/cli/src/cli/check/mod.rs b/packages/cli/src/cli/check/mod.rs index ad086af5a..a4d84addb 100644 --- a/packages/cli/src/cli/check/mod.rs +++ b/packages/cli/src/cli/check/mod.rs @@ -106,12 +106,16 @@ async fn check_files_and_report(files_to_check: Vec) -> Result<()> { } fn collect_rs_files(folder: &Path, files: &mut Vec) { - let Ok(folder) = folder.read_dir() else { return }; + let Ok(folder) = folder.read_dir() else { + return; + }; // load the gitignore for entry in folder { - let Ok(entry) = entry else { continue; }; + let Ok(entry) = entry else { + continue; + }; let path = entry.path(); diff --git a/packages/router/src/components/link.rs b/packages/router/src/components/link.rs index efed7fb5f..f6840b5b4 100644 --- a/packages/router/src/components/link.rs +++ b/packages/router/src/components/link.rs @@ -5,7 +5,7 @@ use dioxus::prelude::*; use log::error; use crate::navigation::NavigationTarget; -use crate::prelude::{AnyDisplay, Routable}; +use crate::prelude::Routable; use crate::utils::use_router_internal::use_router_internal; /// Something that can be converted into a [`NavigationTarget`]. @@ -13,19 +13,21 @@ pub enum IntoRoutable { /// A raw string target. FromStr(String), /// A internal target. - Route(Box), + Route(Box), } impl From for IntoRoutable { fn from(value: R) -> Self { - IntoRoutable::Route(Box::new(value)) + IntoRoutable::Route(Box::new(value) as Box) } } impl From> for IntoRoutable { fn from(value: NavigationTarget) -> Self { match value { - NavigationTarget::Internal(route) => IntoRoutable::Route(Box::new(route)), + NavigationTarget::Internal(route) => { + IntoRoutable::Route(Box::new(route) as Box) + } NavigationTarget::External(url) => IntoRoutable::FromStr(url), } } @@ -193,7 +195,7 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element { let current_url = router.current_route_string(); let href = match to { IntoRoutable::FromStr(url) => url.to_string(), - IntoRoutable::Route(route) => route.to_string(), + IntoRoutable::Route(route) => router.any_route_to_string(&**route), }; let parsed_route: NavigationTarget> = match router.route_from_str(&href) { Ok(route) => NavigationTarget::Internal(route.into()), @@ -219,7 +221,7 @@ pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element { if do_default && is_router_nav { let href = match to { IntoRoutable::FromStr(url) => url.to_string(), - IntoRoutable::Route(route) => route.to_string(), + IntoRoutable::Route(route) => router.any_route_to_string(&**route), }; let parsed_route: NavigationTarget> = match router.route_from_str(&href) { Ok(route) => NavigationTarget::Internal(route.into()), diff --git a/packages/router/src/contexts/router.rs b/packages/router/src/contexts/router.rs index 136e9e3b7..70b493a56 100644 --- a/packages/router/src/contexts/router.rs +++ b/packages/router/src/contexts/router.rs @@ -37,6 +37,8 @@ pub struct RouterContext { routing_callback: Option Option>>>>, failure_external_navigation: fn(Scope) -> Element, + + any_route_to_string: fn(&dyn Any) -> String, } impl RouterContext { @@ -79,6 +81,20 @@ impl RouterContext { }), failure_external_navigation: cfg.failure_external_navigation, + + any_route_to_string: |route| { + route + .downcast_ref::() + .unwrap_or_else(|| { + panic!( + "Route is not of the expected type: {}\n found typeid: {:?}\n expected typeid: {:?}", + std::any::type_name::(), + route.type_id(), + std::any::TypeId::of::() + ) + }) + .to_string() + }, }; // set the updater @@ -189,7 +205,8 @@ impl RouterContext { /// The route that is currently active. pub fn current(&self) -> R { - self.state + *self + .state .read() .unwrap() .history @@ -200,12 +217,11 @@ impl RouterContext { /// The route that is currently active. pub fn current_route_string(&self) -> String { - self.state - .read() - .unwrap() - .history - .current_route() - .to_string() + self.any_route_to_string(&*self.state.read().unwrap().history.current_route()) + } + + pub(crate) fn any_route_to_string(&self, route: &dyn Any) -> String { + (self.any_route_to_string)(route) } /// The prefix that is currently active. diff --git a/packages/router/src/history/mod.rs b/packages/router/src/history/mod.rs index e666e8550..591bdb326 100644 --- a/packages/router/src/history/mod.rs +++ b/packages/router/src/history/mod.rs @@ -10,7 +10,7 @@ //! 1) [`MemoryHistory`] for desktop/mobile/ssr platforms //! 2) [`WebHistory`] for web platforms -use std::{any::Any, fmt::Display, sync::Arc}; +use std::{any::Any, sync::Arc}; mod memory; pub use memory::*; @@ -278,25 +278,6 @@ pub trait HistoryProvider { fn updater(&mut self, callback: Arc) {} } -/// Something that can be displayed and is also an [`Any`] -pub trait AnyDisplay: Display + Any { - /// Get a reference to the inner [`Any`] object. - fn as_any(&self) -> &dyn Any; -} - -impl dyn AnyDisplay { - /// Try to downcast the inner [`Any`] object to a concrete type. - pub fn downcast(self: Box) -> Option { - self.as_any().downcast_ref::().cloned() - } -} - -impl AnyDisplay for T { - fn as_any(&self) -> &dyn Any { - self - } -} - pub(crate) trait AnyHistoryProvider { #[must_use] fn parse_route(&self, route: &str) -> Result, String>; @@ -305,7 +286,7 @@ pub(crate) trait AnyHistoryProvider { fn accepts_type_id(&self, type_id: &std::any::TypeId) -> bool; #[must_use] - fn current_route(&self) -> Box; + fn current_route(&self) -> Box; #[must_use] fn current_prefix(&self) -> Option { @@ -375,9 +356,10 @@ where type_id == &std::any::TypeId::of::() } - fn current_route(&self) -> Box { + fn current_route(&self) -> Box { let route = self.inner.current_route(); println!("current_route {route}"); + println!("current_route type {}", std::any::type_name::()); Box::new(route) } diff --git a/packages/router/src/routable.rs b/packages/router/src/routable.rs index 2eb7f5b7b..cd3dd354d 100644 --- a/packages/router/src/routable.rs +++ b/packages/router/src/routable.rs @@ -141,7 +141,6 @@ pub trait Routable: std::fmt::Display + std::str::FromStr + Clone + 'static { 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; }