yew_router_nested::switch

Trait Switch

Source
pub trait Switch: Sized {
    // Required methods
    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>;

    // Provided methods
    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§

Source

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

Get self from a part of the state

Source

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

Build part of a route from itself.

Provided Methods§

Source

fn switch<STATE>(route: Route<STATE>) -> Option<Self>

Based on a route, possibly produce an itself.

Source

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.

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.

Implementors§