[−][src]Trait yew_router::switch::Switch
Derivable routing trait that allows instances of implementors to be constructed from Routes.
Note
Don't try to implement this yourself, rely on the derive macro.
Example
use yew_router::{route::Route, Switch}; #[derive(Debug, Switch, PartialEq)] enum TestEnum { #[to = "/test/route"] TestRoute, #[to = "/capture/string/{path}"] CaptureString { path: String }, #[to = "/capture/number/{num}"] CaptureNumber { num: usize }, #[to = "/capture/unnamed/{doot}"] CaptureUnnamed(String), } assert_eq!( TestEnum::switch(Route::<()>::from("/test/route")), Some(TestEnum::TestRoute) ); assert_eq!( TestEnum::switch(Route::<()>::from("/capture/string/lorem")), Some(TestEnum::CaptureString { path: "lorem".to_string() }) ); assert_eq!( TestEnum::switch(Route::<()>::from("/capture/number/22")), Some(TestEnum::CaptureNumber { num: 22 }) ); assert_eq!( TestEnum::switch(Route::<()>::from("/capture/unnamed/lorem")), Some(TestEnum::CaptureUnnamed("lorem".to_string())) );
Required methods
fn from_route_part<T: RouteState>(part: Route<T>) -> (Option<Self>, Option<T>)
Get self from a part of the state
fn build_route_section<T>(self, route: &mut String) -> Option<T>
Build part of a route from itself.
Provided methods
fn switch<T: RouteState>(route: Route<T>) -> Option<Self>
Based on a route, possibly produce an itself.
fn key_not_available() -> Option<Self>
Called when the key (the named capture group) can't be located. Instead of failing outright, a default item can be provided instead.
Its primary motivation for existing is to allow implementing Switch for Option. This doesn't make sense at the moment because this only works for the individual key section
- any surrounding literals are pretty much guaranteed to make the parse step fail. because of this, this functionality might be removed in favor of using a nested Switch enum, or multiple variants.
Implementations on Foreign Types
impl<U: Switch> Switch for Option<U>
[src]
fn from_route_part<T: RouteState>(part: Route<T>) -> (Option<Self>, Option<T>)
[src]
Option is very permissive in what is allowed.