pub trait FromQueryArgument: Default {
type Err;
// Required method
fn from_query_argument(argument: &str) -> Result<Self, Self::Err>;
}
Expand description
Something that can be created from a query argument. This trait must be implemented for any type that is used as a query argument like #[route("/?:query")]
.
This trait is automatically implemented for any types that implement FromStr
and Default
.
use dioxus::prelude::*;
#[derive(Routable, Clone, PartialEq, Debug)]
enum Route {
// FromQuerySegment must be implemented for any types you use in the query segment
// When you don't spread the query, you can parse multiple values form the query
// This url will be in the format `/?query=123&other=456`
#[route("/?:query&:other")]
Home {
query: CustomQuery,
other: i32,
},
}
// We can derive Default for CustomQuery
// If the router fails to parse the query value, it will use the default value instead
#[derive(Default, Clone, PartialEq, Debug)]
struct CustomQuery {
count: i32,
}
// We implement FromStr for CustomQuery so that FromQuerySegment is implemented automatically
impl std::str::FromStr for CustomQuery {
type Err = <i32 as std::str::FromStr>::Err;
fn from_str(query: &str) -> Result<Self, Self::Err> {
Ok(CustomQuery {
count: query.parse()?,
})
}
}
// We also need to implement Display for CustomQuery which will be used to format the query string into the URL
impl std::fmt::Display for CustomQuery {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.count)
}
}
Required Associated Types§
Required Methods§
Sourcefn from_query_argument(argument: &str) -> Result<Self, Self::Err>
fn from_query_argument(argument: &str) -> Result<Self, Self::Err>
Create an instance of Self
from a query string.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.