소스 검색

fix link component

Evan Almloff 1 년 전
부모
커밋
e7a9161066

+ 1 - 1
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);
                         }
                     }

+ 6 - 2
packages/cli/src/cli/check/mod.rs

@@ -106,12 +106,16 @@ async fn check_files_and_report(files_to_check: Vec<PathBuf>) -> Result<()> {
 }
 
 fn collect_rs_files(folder: &Path, files: &mut Vec<PathBuf>) {
-    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();
 

+ 8 - 6
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<dyn AnyDisplay>),
+    Route(Box<dyn Any>),
 }
 
 impl<R: Routable> From<R> for IntoRoutable {
     fn from(value: R) -> Self {
-        IntoRoutable::Route(Box::new(value))
+        IntoRoutable::Route(Box::new(value) as Box<dyn Any>)
     }
 }
 
 impl<R: Routable> From<NavigationTarget<R>> for IntoRoutable {
     fn from(value: NavigationTarget<R>) -> Self {
         match value {
-            NavigationTarget::Internal(route) => IntoRoutable::Route(Box::new(route)),
+            NavigationTarget::Internal(route) => {
+                IntoRoutable::Route(Box::new(route) as Box<dyn Any>)
+            }
             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<Box<dyn Any>> = 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<Box<dyn Any>> = match router.route_from_str(&href) {
                 Ok(route) => NavigationTarget::Internal(route.into()),

+ 23 - 7
packages/router/src/contexts/router.rs

@@ -37,6 +37,8 @@ pub struct RouterContext {
     routing_callback: Option<Arc<dyn Fn(RouterContext) -> Option<NavigationTarget<Box<dyn Any>>>>>,
 
     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::<R>()
+                    .unwrap_or_else(|| {
+                        panic!(
+                            "Route is not of the expected type: {}\n found typeid: {:?}\n expected typeid: {:?}",
+                            std::any::type_name::<R>(),
+                            route.type_id(),
+                            std::any::TypeId::of::<R>()
+                        )
+                    })
+                    .to_string()
+            },
         };
 
         // set the updater
@@ -189,7 +205,8 @@ impl RouterContext {
 
     /// The route that is currently active.
     pub fn current<R: Routable>(&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.

+ 4 - 22
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<R: Routable> {
     fn updater(&mut self, callback: Arc<dyn Fn() + Send + Sync>) {}
 }
 
-/// 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<T: Any + Clone>(self: Box<dyn AnyDisplay>) -> Option<T> {
-        self.as_any().downcast_ref::<T>().cloned()
-    }
-}
-
-impl<T: Display + Any> AnyDisplay for T {
-    fn as_any(&self) -> &dyn Any {
-        self
-    }
-}
-
 pub(crate) trait AnyHistoryProvider {
     #[must_use]
     fn parse_route(&self, route: &str) -> Result<Box<dyn Any>, 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<dyn AnyDisplay>;
+    fn current_route(&self) -> Box<dyn Any>;
 
     #[must_use]
     fn current_prefix(&self) -> Option<String> {
@@ -375,9 +356,10 @@ where
         type_id == &std::any::TypeId::of::<R>()
     }
 
-    fn current_route(&self) -> Box<dyn AnyDisplay> {
+    fn current_route(&self) -> Box<dyn Any> {
         let route = self.inner.current_route();
         println!("current_route {route}");
+        println!("current_route type {}", std::any::type_name::<R>());
         Box::new(route)
     }
 

+ 0 - 1
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;
             }