make hooks return a result instead of an option

This commit is contained in:
Adrian Wannenmacher committed 2022-12-13 11:09:24 +01:00
1 parent 3e86c0e364
commit 7d2466ff28
5 files changed
+35 -26

No files matched your search

+1
View File
@@ -18,6 +18,7 @@ dioxus-router-core = { path = "../router-core"}
futures-channel = "0.3.25" futures-channel = "0.3.25"
futures-util = "0.3.25" futures-util = "0.3.25"
log = "0.4.17" log = "0.4.17"
thiserror = "1.0.37"
# for wasm # for wasm
+8
View File
@@ -0,0 +1,8 @@
/// Errors that may occur during routing.
#[derive(Debug, thiserror::Error)]
pub enum RouterError {
/// A hook that needs access to a router was called inside a component, that has no parent
/// calling the [`use_router`](crate::hooks::use_router) hook.
#[error("component needing access not inside router")]
NotInsideRouter,
}
+10 -13
View File
@@ -1,15 +1,14 @@
use dioxus::prelude::{ScopeId, ScopeState}; use dioxus::prelude::{ScopeId, ScopeState};
use dioxus_router_core::Navigator; use dioxus_router_core::Navigator;
use log::error;
use crate::utils::use_router_internal::use_router_internal; use crate::{RouterError, utils::use_router_internal::use_router_internal};
/// A hook that allows for programmatic navigation. /// A hook that allows for programmatic navigation.
/// ///
/// # Return values /// # Return values
/// - [`None`], when the calling component is not nested within another component calling the /// - [`RouterError::NotInsideRouter`], when the calling component is not nested within another
/// [`use_router`] hook. /// component calling the [`use_router`] hook.
/// - Otherwise [`Some`]. /// - Otherwise [`Ok`].
/// ///
/// # Panic /// # Panic
/// - When the calling component is not nested within another component calling the [`use_router`] /// - When the calling component is not nested within another component calling the [`use_router`]
@@ -36,7 +35,7 @@ use crate::utils::use_router_internal::use_router_internal;
/// } /// }
/// ///
/// fn Redirect(cx: Scope) -> Element { /// fn Redirect(cx: Scope) -> Element {
/// let nav = use_navigate(&cx).unwrap(); /// let nav = use_navigate(&cx)?;
/// nav.push("/content"); /// nav.push("/content");
/// render! { () } /// render! { () }
/// } /// }
@@ -58,16 +57,14 @@ use crate::utils::use_router_internal::use_router_internal;
/// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Content</p>"); /// # assert_eq!(dioxus_ssr::render(&vdom), "<h1>App</h1><p>Content</p>");
/// ``` /// ```
#[must_use] #[must_use]
pub fn use_navigate(cx: &ScopeState) -> Option<Navigator<ScopeId>> { pub fn use_navigate(cx: &ScopeState) -> Result<Navigator<ScopeId>, RouterError> {
match use_router_internal(cx) { match use_router_internal(cx) {
Some(r) => Some(r.sender.clone().into()), Some(r) => Ok(r.sender.clone().into()),
#[allow(unreachable_code)]
None => { None => {
let msg = "`use_navigate` must have access to a parent router";
error!("{msg}, will be inactive");
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
panic!("{}", msg); panic!("`use_navigate` must have access to a parent router");
#[cfg(not(debug_assertions))] Err(RouterError::NotInsideRouter)
None
} }
} }
} }
+12 -13
View File
@@ -1,16 +1,15 @@
use async_rwlock::RwLockReadGuard; use async_rwlock::RwLockReadGuard;
use dioxus::{core::Component, prelude::ScopeState}; use dioxus::{core::Component, prelude::ScopeState};
use dioxus_router_core::RouterState; use dioxus_router_core::RouterState;
use log::error;
use crate::utils::use_router_internal::use_router_internal; use crate::{RouterError, utils::use_router_internal::use_router_internal};
/// A hook that provides access to information about the current routing location. /// A hook that provides access to information about the current routing location.
/// ///
/// # Return values /// # Return values
/// - [`None`], when the calling component is not nested within another component calling the /// - [`RouterError::NotInsideRouter`], when the calling component is not nested within another
/// [`use_router`] hook. /// component calling the [`use_router`] hook.
/// - Otherwise [`Some`]. /// - Otherwise [`Ok`].
/// ///
/// # Important usage information /// # Important usage information
/// Make sure to [`drop`] the returned [`RwLockReadGuard`] when done rendering. Otherwise the router /// Make sure to [`drop`] the returned [`RwLockReadGuard`] when done rendering. Otherwise the router
@@ -42,7 +41,7 @@ use crate::utils::use_router_internal::use_router_internal;
/// } /// }
/// ///
/// fn Content(cx: Scope) -> Element { /// fn Content(cx: Scope) -> Element {
/// let state = use_route(&cx).unwrap(); /// let state = use_route(&cx)?;
/// let path = state.path.clone(); /// let path = state.path.clone();
/// ///
/// render! { /// render! {
@@ -58,20 +57,20 @@ use crate::utils::use_router_internal::use_router_internal;
/// ///
/// [`use_router`]: super::use_router /// [`use_router`]: super::use_router
#[must_use] #[must_use]
pub fn use_route<'a>(cx: &'a ScopeState) -> Option<RwLockReadGuard<'a, RouterState<Component>>> { pub fn use_route<'a>(
cx: &'a ScopeState,
) -> Result<RwLockReadGuard<'a, RouterState<Component>>, RouterError> {
match use_router_internal(cx) { match use_router_internal(cx) {
Some(r) => loop { Some(r) => loop {
if let Some(s) = r.state.try_read() { if let Some(s) = r.state.try_read() {
break Some(s); break Ok(s);
} }
}, },
#[allow(unreachable_code)]
None => { None => {
let msg = "`use_route` must have access to a parent router";
error!("{msg}, will be inactive");
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
panic!("{}", msg); panic!("`use_route` must have access to a parent router");
#[cfg(not(debug_assertions))] Err(RouterError::NotInsideRouter)
None
} }
} }
} }
+4
View File
@@ -15,6 +15,10 @@ mod contexts {
pub(crate) mod router; pub(crate) mod router;
} }
#[forbid(missing_docs)]
mod error;
pub use error::RouterError;
pub mod history { pub mod history {
pub use dioxus_router_core::history::*; pub use dioxus_router_core::history::*;
} }