Trait yew_router_nested::switch::Switch [−][src]
pub trait Switch: Sized {
fn from_route_part<STATE>(
part: String,
state: Option<STATE>
) -> (Option<Self>, Option<STATE>);
fn build_route_section<STATE>(self, route: &mut String) -> Option<STATE>;
fn switch<STATE>(route: Route<STATE>) -> Option<Self> { ... }
fn key_not_available() -> Option<Self> { ... }
}
Expand description
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::new_no_state("/test/route")),
Some(TestEnum::TestRoute)
);
assert_eq!(
TestEnum::switch(Route::new_no_state("/capture/string/lorem")),
Some(TestEnum::CaptureString {
path: "lorem".to_string()
})
);
assert_eq!(
TestEnum::switch(Route::new_no_state("/capture/number/22")),
Some(TestEnum::CaptureNumber { num: 22 })
);
assert_eq!(
TestEnum::switch(Route::new_no_state("/capture/unnamed/lorem")),
Some(TestEnum::CaptureUnnamed("lorem".to_string()))
);
Required methods
Get self from a part of the state
fn build_route_section<STATE>(self, route: &mut String) -> Option<STATE>
fn build_route_section<STATE>(self, route: &mut String) -> Option<STATE>
Build part of a route from itself.
Provided methods
Based on a route, possibly produce an itself.
fn key_not_available() -> Option<Self>
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.