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

pub trait Switch: Sized {
    fn from_route_part<T: RouteState>(
        part: Route<T>
    ) -> (Option<Self>, Option<T>);
fn build_route_section<T>(self, route: &mut String) -> Option<T>; fn switch<T: RouteState>(route: Route<T>) -> 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::<()>::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.

Loading content...

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.
Loading content...

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.

impl Switch for String[src]

impl Switch for Uuid[src]

impl Switch for bool[src]

impl Switch for f64[src]

impl Switch for f32[src]

impl Switch for usize[src]

impl Switch for u128[src]

impl Switch for u64[src]

impl Switch for u32[src]

impl Switch for u16[src]

impl Switch for u8[src]

impl Switch for isize[src]

impl Switch for i128[src]

impl Switch for i64[src]

impl Switch for i32[src]

impl Switch for i16[src]

impl Switch for i8[src]

impl Switch for NonZeroU128[src]

impl Switch for NonZeroU64[src]

impl Switch for NonZeroU32[src]

impl Switch for NonZeroU16[src]

impl Switch for NonZeroU8[src]

impl Switch for NonZeroI128[src]

impl Switch for NonZeroI64[src]

impl Switch for NonZeroI32[src]

impl Switch for NonZeroI16[src]

impl Switch for NonZeroI8[src]

Loading content...

Implementors

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

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

Loading content...