[][src]Trait yew_router::switch::Switch

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> { ... } }

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

fn from_route_part<STATE>(
    part: String,
    state: Option<STATE>
) -> (Option<Self>, Option<STATE>)

Get self from a part of the state

fn build_route_section<STATE>(self, route: &mut String) -> Option<STATE>

Build part of a route from itself.

Loading content...

Provided methods

fn switch<STATE>(route: Route<STATE>) -> 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.
Loading content...

Implementors

impl<T: FromStr + Display> Switch for T[src]

impl<U: Switch + Debug> Switch for AllowMissing<U>[src]

impl<U: Switch> Switch for LeadingSlash<U>[src]

impl<U: Switch> Switch for Permissive<U>[src]

fn from_route_part<STATE>(
    part: String,
    state: Option<STATE>
) -> (Option<Self>, Option<STATE>)
[src]

Option is very permissive in what is allowed.

Loading content...