parse query strings
This commit is contained in:
1 parent
e26ff6a5c0
commit
2aadeb8046
13 files changed
+389
-320
No files matched your search
@@ -1,30 +1,30 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![forbid(missing_docs)]
|
||||
// #![forbid(missing_docs)]
|
||||
|
||||
pub mod history;
|
||||
|
||||
mod router;
|
||||
pub mod router;
|
||||
pub use router::*;
|
||||
|
||||
pub mod navigation;
|
||||
// pub mod navigation;
|
||||
|
||||
mod navigator;
|
||||
pub use navigator::*;
|
||||
// mod navigator;
|
||||
// pub use navigator::*;
|
||||
|
||||
mod service;
|
||||
pub use service::*;
|
||||
// mod service;
|
||||
// pub use service::*;
|
||||
|
||||
mod state;
|
||||
pub use state::*;
|
||||
// mod state;
|
||||
// pub use state::*;
|
||||
|
||||
mod utils {
|
||||
mod sitemap;
|
||||
pub use sitemap::*;
|
||||
}
|
||||
// mod utils {
|
||||
// mod sitemap;
|
||||
// pub use sitemap::*;
|
||||
// }
|
||||
|
||||
/// A collection of useful types most applications might need.
|
||||
pub mod prelude {
|
||||
pub use crate::navigation::*;
|
||||
// pub use crate::navigation::*;
|
||||
|
||||
/// An external navigation failure.
|
||||
///
|
||||
|
||||
@@ -6,7 +6,7 @@ use url::{ParseError, Url};
|
||||
|
||||
/// A target for the router to navigate to.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum NavigationTarget {
|
||||
pub enum NavigationTarget<R: Routable> {
|
||||
/// An internal path that the router can navigate to by itself.
|
||||
///
|
||||
/// ```rust
|
||||
@@ -15,45 +15,7 @@ pub enum NavigationTarget {
|
||||
/// let implicit: NavigationTarget = "/internal".into();
|
||||
/// assert_eq!(explicit, implicit);
|
||||
/// ```
|
||||
Internal(String),
|
||||
/// An internal target that the router can navigate to by itself.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use std::collections::HashMap;
|
||||
/// # use dioxus_router_core::{Name, navigation::{named, NavigationTarget}};
|
||||
/// let mut parameters = HashMap::new();
|
||||
/// parameters.insert(Name::of::<bool>(), String::from("some parameter"));
|
||||
///
|
||||
/// let explicit = NavigationTarget::Named {
|
||||
/// name: Name::of::<bool>(),
|
||||
/// parameters,
|
||||
/// query: Some("some=query".into())
|
||||
/// };
|
||||
///
|
||||
/// let implicit = named::<bool>().parameter::<bool>("some parameter").query("some=query");
|
||||
///
|
||||
/// assert_eq!(explicit, implicit);
|
||||
/// ```
|
||||
///
|
||||
/// It will automatically find the route with the matching name, insert all required parameters
|
||||
/// and add the query.
|
||||
///
|
||||
/// **Note:** The dioxus-router-core documentation and tests mostly use standard Rust types. This is only
|
||||
/// for brevity. It is recommend to use types with descriptive names, and create unit structs if
|
||||
/// needed.
|
||||
Named {
|
||||
/// The name of the [`Route`](crate::routes::Route) or
|
||||
/// [`ParameterRoute`](crate::routes::ParameterRoute) to navigate to.
|
||||
///
|
||||
/// **Note:** The dioxus-router-core documentation and tests mostly use standard Rust types. This is
|
||||
/// only for brevity. It is recommend to use types with descriptive names, and create unit
|
||||
/// structs if needed.
|
||||
name: Name,
|
||||
/// The parameters required to get to the specified route.
|
||||
parameters: HashMap<Name, String>,
|
||||
/// A query to add to the route.
|
||||
query: Option<Query>,
|
||||
},
|
||||
Internal(R),
|
||||
/// An external target that the router doesn't control.
|
||||
///
|
||||
/// ```rust
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use crate::history::HistoryProvider;
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct RouteParseError<E: std::fmt::Display> {
|
||||
attempted_routes: Vec<E>,
|
||||
pub struct RouteParseError<E: std::fmt::Display> {
|
||||
pub attempted_routes: Vec<E>,
|
||||
}
|
||||
|
||||
impl<E: std::fmt::Display> std::fmt::Display for RouteParseError<E> {
|
||||
@@ -37,127 +39,76 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// #[derive(Props, PartialEq)]
|
||||
// struct RouterProps {
|
||||
// current_route: String,
|
||||
// }
|
||||
pub trait FromQuery {
|
||||
fn from_query(query: &str) -> Self;
|
||||
}
|
||||
|
||||
trait Routable: FromStr + std::fmt::Display + Clone
|
||||
impl<T: for<'a> From<&'a str>> FromQuery for T {
|
||||
fn from_query(query: &str) -> Self {
|
||||
T::from(query)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FromRouteSegment: Sized {
|
||||
type Err;
|
||||
|
||||
fn from_route_segment(route: &str) -> Result<Self, Self::Err>;
|
||||
}
|
||||
|
||||
impl<T: FromStr> FromRouteSegment for T
|
||||
where
|
||||
<T as FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
type Err = <T as FromStr>::Err;
|
||||
|
||||
fn from_route_segment(route: &str) -> Result<Self, Self::Err> {
|
||||
T::from_str(route)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ToRouteSegments {
|
||||
fn to_route_segments(&self) -> Vec<String>;
|
||||
}
|
||||
|
||||
pub trait FromRouteSegments: Sized {
|
||||
type Err;
|
||||
|
||||
fn from_route_segments(segments: &[&str], query: &str) -> Result<Self, Self::Err>;
|
||||
}
|
||||
|
||||
impl<T: FromRouteSegment> FromRouteSegments for Vec<T> {
|
||||
type Err = <T as FromRouteSegment>::Err;
|
||||
|
||||
fn from_route_segments(segments: &[&str], query: &str) -> Result<Self, Self::Err> {
|
||||
segments.iter().map(|s| T::from_route_segment(s)).collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Props, PartialEq)]
|
||||
pub struct RouterProps {
|
||||
pub current_route: String,
|
||||
}
|
||||
|
||||
pub trait Routable: FromStr + std::fmt::Display + Clone
|
||||
where
|
||||
<Self as FromStr>::Err: std::fmt::Display,
|
||||
{
|
||||
// fn render(self, cx: &ScopeState) -> Element;
|
||||
fn render(self, cx: &ScopeState) -> Element;
|
||||
|
||||
// fn comp(cx: Scope<RouterProps>) -> Element
|
||||
// where
|
||||
// Self: 'static,
|
||||
// {
|
||||
// let router = Self::from_str(&cx.props.current_route);
|
||||
// match router {
|
||||
// Ok(router) => router.render(cx),
|
||||
// Err(err) => {
|
||||
// render! {pre {
|
||||
// "{err}"
|
||||
// }}
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
#[derive(Routable, Clone, Debug, PartialEq)]
|
||||
enum Route {
|
||||
#[route("/(dynamic)")]
|
||||
Route1 { dynamic: String },
|
||||
#[route("/hello_world")]
|
||||
Route2 {},
|
||||
#[redirect("/(dynamic)/hello_world")]
|
||||
#[route("/hello_world/(dynamic)")]
|
||||
Route3 { dynamic: u32 },
|
||||
#[route("/(number1)/(number2)")]
|
||||
Route4 { number1: u32, number2: u32 },
|
||||
#[route("/")]
|
||||
Route5 {},
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_works() {
|
||||
let route = Route::Route1 {
|
||||
dynamic: "hello".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(route.to_string(), "/hello");
|
||||
|
||||
let route = Route::Route3 { dynamic: 1234 };
|
||||
|
||||
assert_eq!(route.to_string(), "/hello_world/1234");
|
||||
|
||||
let route = Route::Route1 {
|
||||
dynamic: "hello_world2".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(route.to_string(), "/hello_world2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_string_works() {
|
||||
let w = "/hello";
|
||||
assert_eq!(
|
||||
Route::from_str(w),
|
||||
Ok(Route::Route1 {
|
||||
dynamic: "hello".to_string()
|
||||
})
|
||||
);
|
||||
let w = "/hello/";
|
||||
assert_eq!(
|
||||
Route::from_str(w),
|
||||
Ok(Route::Route1 {
|
||||
dynamic: "hello".to_string()
|
||||
})
|
||||
);
|
||||
|
||||
let w = "/hello_world/1234";
|
||||
assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
|
||||
let w = "/hello_world/1234/";
|
||||
assert_eq!(Route::from_str(w), Ok(Route::Route3 { dynamic: 1234 }));
|
||||
|
||||
let w = "/hello_world2";
|
||||
assert_eq!(
|
||||
Route::from_str(w),
|
||||
Ok(Route::Route1 {
|
||||
dynamic: "hello_world2".to_string()
|
||||
})
|
||||
);
|
||||
|
||||
let w = "/hello_world/-1";
|
||||
match Route::from_str(w) {
|
||||
Ok(r) => panic!("should not parse {r:?}"),
|
||||
Err(err) => println!("{err}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_trip() {
|
||||
// Route1
|
||||
let string = "hello_world2";
|
||||
let route = Route::Route1 {
|
||||
dynamic: string.to_string(),
|
||||
};
|
||||
assert_eq!(Route::from_str(&route.to_string()), Ok(route));
|
||||
|
||||
// Route2
|
||||
for num in 0..100 {
|
||||
let route = Route::Route3 { dynamic: num };
|
||||
assert_eq!(Route::from_str(&route.to_string()), Ok(route));
|
||||
}
|
||||
|
||||
// Route3
|
||||
for num1 in 0..100 {
|
||||
for num2 in 0..100 {
|
||||
let route = Route::Route4 {
|
||||
number1: num1,
|
||||
number2: num2,
|
||||
};
|
||||
assert_eq!(Route::from_str(&route.to_string()), Ok(route));
|
||||
fn comp(cx: Scope<RouterProps>) -> Element
|
||||
where
|
||||
Self: 'static,
|
||||
{
|
||||
let router = Self::from_str(&cx.props.current_route);
|
||||
match router {
|
||||
Ok(router) => router.render(cx),
|
||||
Err(err) => {
|
||||
render! {
|
||||
pre {
|
||||
"{err}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,18 +15,15 @@ use crate::{
|
||||
prelude::{
|
||||
FailureExternalNavigation, FailureNamedNavigation, FailureRedirectionLimit, RootIndex,
|
||||
},
|
||||
routes::{ContentAtom, Segment},
|
||||
segments::{NameMap, NamedSegment},
|
||||
utils::{resolve_target, route_segment},
|
||||
Name, RouterState,
|
||||
RouterState,
|
||||
};
|
||||
|
||||
/// Messages that the [`RouterService`] can handle.
|
||||
pub enum RouterMessage<I> {
|
||||
pub enum RouterMessage<I, R> {
|
||||
/// Subscribe to router update.
|
||||
Subscribe(Arc<I>),
|
||||
/// Navigate to the specified target.
|
||||
Push(NavigationTarget),
|
||||
Push(NavigationTarget<R>),
|
||||
/// Replace the current location with the specified target.
|
||||
Replace(NavigationTarget),
|
||||
/// Trigger a routing update.
|
||||
@@ -63,9 +60,9 @@ impl<I: PartialEq> PartialEq for RouterMessage<I> {
|
||||
|
||||
impl<I: Eq> Eq for RouterMessage<I> {}
|
||||
|
||||
enum NavigationFailure {
|
||||
enum NavigationFailure<R: Routable> {
|
||||
External(String),
|
||||
Named(Name),
|
||||
Internal(<R as std::str::FromStr>::Err),
|
||||
}
|
||||
|
||||
/// A function the router will call after every routing update.
|
||||
|
||||
@@ -153,7 +153,7 @@ impl<T: Clone> Default for RouterState<T> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{navigation::named, prelude::RootIndex, routes::Segment, segments::NamedSegment};
|
||||
use crate::{navigation::named, prelude::RootIndex};
|
||||
|
||||
use super::*;
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ use std::collections::BTreeMap;
|
||||
|
||||
use urlencoding::encode;
|
||||
|
||||
|
||||
pub fn gen_sitemap<T: Clone>(seg: &Segment<T>, current: &str, map: &mut Vec<String>) {
|
||||
for (p, r) in &seg.fixed {
|
||||
let current = format!("{current}/{p}");
|
||||
|
||||
Reference in new issue
Block a user