general cleanup

This commit is contained in:
Evan Almloff committed 2023-05-31 17:12:52 -05:00
1 parent 09cabe4e8b
commit ece8f0fb22
15 files changed
+155 -767

No files matched your search

+24 -17
View File
@@ -1,13 +1,17 @@
//! Types pertaining to navigation.
use std::{fmt::Display, str::FromStr};
use std::{
convert::TryFrom,
fmt::{Debug, Display},
str::FromStr,
};
use url::{ParseError, Url};
use crate::routable::Routable;
/// A target for the router to navigate to.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum NavigationTarget<R: Routable> {
/// An internal path that the router can navigate to by itself.
///
@@ -26,7 +30,7 @@ pub enum NavigationTarget<R: Routable> {
/// Index {},
/// }
/// let explicit = NavigationTarget::Internal(Route::Index {});
/// let implicit: NavigationTarget::<Route> = "/".into();
/// let implicit: NavigationTarget::<Route> = "/".parse().unwrap();
/// assert_eq!(explicit, implicit);
/// ```
Internal(R),
@@ -47,25 +51,17 @@ pub enum NavigationTarget<R: Routable> {
/// Index {},
/// }
/// let explicit = NavigationTarget::<Route>::External(String::from("https://dioxuslabs.com/"));
/// let implicit: NavigationTarget::<Route> = "https://dioxuslabs.com/".into();
/// let implicit: NavigationTarget::<Route> = "https://dioxuslabs.com/".parse().unwrap();
/// assert_eq!(explicit, implicit);
/// ```
External(String),
}
impl<R: Routable> From<&str> for NavigationTarget<R>
where
<R as FromStr>::Err: Display,
{
fn from(value: &str) -> Self {
Self::from_str(value).unwrap_or_else(|err| match err {
NavigationTargetParseError::InvalidUrl(e) => {
panic!("Failed to parse `{}` as a URL: {}", value, e)
}
NavigationTargetParseError::InvalidInternalURL(e) => {
panic!("Failed to parse `{}` as a `Routable`: {}", value, e)
}
})
impl<R: Routable> TryFrom<&str> for NavigationTarget<R> {
type Error = NavigationTargetParseError<R>;
fn try_from(value: &str) -> Result<Self, Self::Error> {
value.parse()
}
}
@@ -92,6 +88,17 @@ pub enum NavigationTargetParseError<R: Routable> {
InvalidInternalURL(<R as FromStr>::Err),
}
impl<R: Routable> Debug for NavigationTargetParseError<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NavigationTargetParseError::InvalidUrl(e) => write!(f, "Invalid URL: {}", e),
NavigationTargetParseError::InvalidInternalURL(_) => {
write!(f, "Invalid internal URL")
}
}
}
}
impl<R: Routable> FromStr for NavigationTarget<R> {
type Err = NavigationTargetParseError<R>;