Skip to content
This repository has been archived by the owner on Jul 19, 2020. It is now read-only.

Commit

Permalink
Add unit as the default state type (#200)
Browse files Browse the repository at this point in the history
* Add unit as the default state type

* fix test

* fix doc tests

* run cargo fmt
  • Loading branch information
hgzimmerman authored Dec 16, 2019
1 parent 6e34c05 commit 80d0320
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 70 deletions.
9 changes: 6 additions & 3 deletions crates/yew_router_macro/src/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ use crate::switch::{
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{export::TokenStream2, parse_macro_input, Data, DeriveInput, Fields, Ident, Variant, Generics, GenericParam};
use syn::{
export::TokenStream2, parse_macro_input, Data, DeriveInput, Fields, GenericParam, Generics,
Ident, Variant,
};

mod attribute;
mod enum_impl;
mod shadow;
mod struct_impl;

use self::attribute::AttrToken;
use yew_router_route_parser::FieldNamingScheme;
use syn::punctuated::Punctuated;
use yew_router_route_parser::FieldNamingScheme;

/// Holds data that is required to derive Switch for a struct or a single enum variant.
pub struct SwitchItem {
Expand Down Expand Up @@ -308,7 +311,7 @@ pub fn impl_line(ident: &Ident, generics: &Generics) -> TokenStream2 {
.collect::<Punctuated<_,syn::token::Comma>>();

let where_clause = &generics.where_clause;
quote!{
quote! {
impl <#params> ::yew_router::Switch for #ident <#param_idents> #where_clause
}
}
Expand Down
10 changes: 7 additions & 3 deletions crates/yew_router_macro/src/switch/enum_impl.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use crate::switch::{build_serializer_for_enum, SwitchItem, impl_line};
use crate::switch::{build_serializer_for_enum, impl_line, SwitchItem};
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{export::TokenStream2, Field, Fields, Ident, Type, Generics};
use syn::{export::TokenStream2, Field, Fields, Generics, Ident, Type};

pub fn generate_enum_impl(enum_ident: Ident, switch_variants: Vec<SwitchItem>, generics: Generics) -> TokenStream {
pub fn generate_enum_impl(
enum_ident: Ident,
switch_variants: Vec<SwitchItem>,
generics: Generics,
) -> TokenStream {
let variant_matchers = switch_variants.iter().map(|sv| {
let SwitchItem {
matcher,
Expand Down
8 changes: 5 additions & 3 deletions crates/yew_router_macro/src/switch/struct_impl.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::switch::{SwitchItem, impl_line};
use crate::switch::{impl_line, SwitchItem};
use proc_macro2::{Ident, Span};
use quote::quote;
use syn::{export::{TokenStream, TokenStream2}, Field, Fields, Type, Generics};

use syn::{
export::{TokenStream, TokenStream2},
Field, Fields, Generics, Type,
};


pub fn generate_struct_impl(item: SwitchItem, generics: Generics) -> TokenStream {
Expand Down
3 changes: 1 addition & 2 deletions examples/servers/warp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::path::PathBuf;
use warp::{
filters::BoxedFilter,
fs::File,
path::{Peek},
path,
path::{self, Peek},
Filter, Reply,
};

Expand Down
5 changes: 2 additions & 3 deletions examples/switch/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ pub struct OtherSingle(u32);

#[derive(Switch, Debug, Clone)]
#[to = "{*:path}#{route}"]
pub struct FragmentAdapter<W: Switch>{
pub struct FragmentAdapter<W: Switch> {
path: String,
route: W
route: W,
}

2 changes: 1 addition & 1 deletion src/agent/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use yew::{
/// A wrapped bridge to the route agent.
///
/// A component that owns this can send and receive messages from the agent.
pub struct RouteAgentBridge<T>(Box<dyn Bridge<RouteAgent<T>>>)
pub struct RouteAgentBridge<T = ()>(Box<dyn Bridge<RouteAgent<T>>>)
where
for<'de> T: AgentState<'de>;

Expand Down
2 changes: 1 addition & 1 deletion src/agent/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use yew::agent::{Dispatched, Dispatcher};
/// A wrapped dispatcher to the route agent.
///
/// A component that owns and instance of this can send messages to the RouteAgent, but not receive them.
pub struct RouteAgentDispatcher<T>(Dispatcher<RouteAgent<T>>)
pub struct RouteAgentDispatcher<T = ()>(Dispatcher<RouteAgent<T>>)
where
for<'de> T: AgentState<'de>;

Expand Down
4 changes: 2 additions & 2 deletions src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub enum Msg<T> {

/// Input message type for interacting with the `RouteAgent'.
#[derive(Serialize, Deserialize, Debug)]
pub enum RouteRequest<T> {
pub enum RouteRequest<T = ()> {
/// Replaces the most recent Route with a new one and alerts connected components to the route
/// change.
ReplaceRoute(Route<T>),
Expand All @@ -59,7 +59,7 @@ pub enum RouteRequest<T> {
///
/// If you use multiple agents with different types, then the Agents won't be able to communicate to
/// each other and associated components may not work as intended.
pub struct RouteAgent<T>
pub struct RouteAgent<T = ()>
where
for<'de> T: AgentState<'de>,
{
Expand Down
2 changes: 1 addition & 1 deletion src/components/router_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use yew::virtual_dom::VNode;

/// Changes the route when clicked.
#[derive(Debug)]
pub struct RouterButton<T: for<'de> RouterState<'de>> {
pub struct RouterButton<T: for<'de> RouterState<'de> = ()> {
link: ComponentLink<Self>,
router: RouteAgentDispatcher<T>,
props: Props<T>,
Expand Down
2 changes: 1 addition & 1 deletion src/components/router_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub type RouterLink<T> = RouterAnchor<T>;

/// An anchor tag Component that when clicked, will navigate to the provided route.
#[derive(Debug)]
pub struct RouterAnchor<T: for<'de> RouterState<'de>> {
pub struct RouterAnchor<T: for<'de> RouterState<'de> = ()> {
link: ComponentLink<Self>,
router: RouteAgentDispatcher<T>,
props: Props<T>,
Expand Down
17 changes: 15 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,21 @@ pub mod unit_state {
/// Prelude module that can be imported when working with the yew_router
pub mod prelude {
pub use super::matcher::Captures;
#[cfg(feature = "unit_alias")]
pub use super::unit_state::*;

#[cfg(feature = "agent")]
pub use crate::agent::RouteAgent;
#[cfg(feature = "agent")]
pub use crate::agent::RouteAgentBridge;
#[cfg(feature = "agent")]
pub use crate::agent::RouteAgentDispatcher;
#[cfg(feature = "components")]
pub use crate::components::RouterAnchor;
#[cfg(feature = "components")]
pub use crate::components::RouterButton;
#[cfg(feature = "router")]
pub use crate::router::Router;
pub use crate::{route::Route, service::RouteService};

pub use crate::switch::Switch;
pub use yew_router_macro::Switch;
// State restrictions
Expand Down
2 changes: 1 addition & 1 deletion src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl<T> RouteState for T where T: Clone + Default + JsSerialize + TryFrom<Value>

/// The representation of a route, segmented into different sections for easy access.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Route<T> {
pub struct Route<T = ()> {
/// The route string
pub route: String,
/// The state stored in the history api
Expand Down
24 changes: 12 additions & 12 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<'de, T> RouterState<'de> for T where T: AgentState<'de> + PartialEq {}
///
/// fn view(&self) -> VNode {
/// html! {
/// <Router<(), S>
/// <Router<S>
/// render = Router::render(|switch: S| {
/// match switch {
/// S::Variant => html!{"variant route was matched"},
Expand All @@ -59,13 +59,13 @@ impl<'de, T> RouterState<'de> for T where T: AgentState<'de> + PartialEq {}
/// ```
// TODO, can M just be removed due to not having to explicitly deal with callbacks anymore? - Just get rid of M
#[derive(Debug)]
pub struct Router<T: for<'de> RouterState<'de>, SW: Switch + Clone + 'static> {
pub struct Router<SW: Switch + Clone + 'static, T: for<'de> RouterState<'de> = ()> {
switch: Option<SW>,
props: Props<T, SW>,
router_agent: RouteAgentBridge<T>,
}

impl<T, SW> Router<T, SW>
impl<SW, T> Router<SW, T>
where
T: for<'de> RouterState<'de>,
SW: Switch + Clone + 'static,
Expand All @@ -85,14 +85,14 @@ where
/// # pub enum Msg {}
///
/// # fn dont_execute() {
/// let render: Render<(), S> = Router::render(|switch: S| -> Html {
/// let render: Render<S> = Router::render(|switch: S| -> Html {
/// match switch {
/// S::Variant => html! {"Variant"},
/// }
/// });
/// # }
/// ```
pub fn render<F: RenderFn<Router<T, SW>, SW> + 'static>(f: F) -> Render<T, SW> {
pub fn render<F: RenderFn<Router<SW, T>, SW> + 'static>(f: F) -> Render<SW, T> {
Render::new(f)
}

Expand All @@ -114,16 +114,16 @@ pub trait RenderFn<CTX: Component, SW>: Fn(SW) -> Html {}
impl<T, CTX: Component, SW> RenderFn<CTX, SW> for T where T: Fn(SW) -> Html {}
/// Owned Render function.
#[derive(Clone)]
pub struct Render<T: for<'de> RouterState<'de>, SW: Switch + Clone + 'static>(
pub(crate) Rc<dyn RenderFn<Router<T, SW>, SW>>,
pub struct Render<SW: Switch + Clone + 'static, T: for<'de> RouterState<'de> = ()>(
pub(crate) Rc<dyn RenderFn<Router<SW, T>, SW>>,
);
impl<T: for<'de> RouterState<'de>, SW: Switch + Clone> Render<T, SW> {
impl<T: for<'de> RouterState<'de>, SW: Switch + Clone> Render<SW, T> {
/// New render function
fn new<F: RenderFn<Router<T, SW>, SW> + 'static>(f: F) -> Self {
fn new<F: RenderFn<Router<SW, T>, SW> + 'static>(f: F) -> Self {
Render(Rc::new(f))
}
}
impl<T: for<'de> RouterState<'de>, SW: Switch + Clone> Debug for Render<T, SW> {
impl<T: for<'de> RouterState<'de>, SW: Switch + Clone> Debug for Render<SW, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Render").finish()
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl<STATE: for<'de> RouterState<'de>, SW: Switch> Debug for Redirect<SW, STATE>
pub struct Props<T: for<'de> RouterState<'de>, SW: Switch + Clone + 'static> {
/// Render function that takes a Switch and produces Html
#[props(required)]
pub render: Render<T, SW>,
pub render: Render<SW, T>,
/// Optional redirect function that will convert the route to a known switch variant if explicit matching fails.
/// This should mostly be used to handle 404s and redirection.
/// It is not strictly necessary as your Switch is capable of handling unknown routes using `#[to="/{*:any}"]`.
Expand All @@ -167,7 +167,7 @@ impl<T: for<'de> RouterState<'de>, SW: Switch + Clone> Debug for Props<T, SW> {
}
}

impl<T, SW> Component for Router<T, SW>
impl<T, SW> Component for Router<SW, T>
where
T: for<'de> RouterState<'de>,
SW: Switch + Clone + 'static,
Expand Down
3 changes: 1 addition & 2 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ use std::marker::PhantomData;
///
/// The `T` determines what route state can be stored in the route service.
#[derive(Debug)]
pub struct RouteService<T> {
pub struct RouteService<T = ()> {
history: History,
location: Location,
event_listener: Option<EventListenerHandle>,

phantom_data: PhantomData<T>,
}

Expand Down
Loading

0 comments on commit 80d0320

Please sign in to comment.