fn build_variant_from_captures(
    enum_ident: &Ident,
    variant_ident: &Ident,
    fields: &Fields,
) -> TokenStream2 {
    match fields {
        Fields::Named(named_fields) => {
            let fields: Vec<TokenStream2> = named_fields
                .named
                .iter()
                .filter_map(|field: &Field| {
                    let field_ty: &Type = &field.ty;
                    field.ident.as_ref().map(|i: &Ident| {
                        let key = i.to_string();
                        (i, key, field_ty)
                    })
                })
                .map(|(field_name, key, field_ty): (&Ident, String, &Type)| {
                    quote! {
                        #field_name: {
                            let (v, s) = match captures.remove(#key) {
                                Some(value) => {
                                    <#field_ty as ::yew_router::Switch>::from_route_part(
                                        ::yew_router::route::Route {
                                            route: value,
                                            state,
                                        }
                                    )
                                }
                                None => {
                                    (
                                        <#field_ty as ::yew_router::Switch>::key_not_available(),
                                        state,
                                    )
                                }
                            };
                            match v {
                                Some(val) => {
                                    state = s; // Set state for the next var.
                                    val
                                },
                                None => return (None, s) // Failed
                            }
                        }
                    }
                })
                .collect();

            quote! {
                let mut state = if let Some(mut captures) = matcher.capture_route_into_map(&route_string).ok().map(|x| x.1) {
                    let create_item = || {
                         (
                            Some(
                                #enum_ident::#variant_ident {
                                    #(#fields),*
                                }
                            ),
                            state
                        )
                    };
                    let (val, state) = create_item();

                    if val.is_some() {
                        return (val, state);
                    }
                    state
                } else {
                    state
                };
            }
        }
        Fields::Unnamed(unnamed_fields) => {
            let fields = unnamed_fields.unnamed.iter().map(|f: &Field| {
                let field_ty = &f.ty;
                quote! {
                    {
                        let (v, s) = match drain.next() {
                            Some((_key, value)) => {
                                <#field_ty as ::yew_router::Switch>::from_route_part(
                                    ::yew_router::route::Route {
                                        route: value,
                                        state,
                                    }
                                )
                            },
                            None => {
                                (
                                    <#field_ty as ::yew_router::Switch>::key_not_available(),
                                    state,
                                )
                            }
                        };
                        match v {
                            Some(val) => {
                                state = s; // Set state for the next var.
                                val
                            },
                            None => return (None, s) // Failed
                        }
                    }
                }
            });

            quote! {
                // TODO put an annotation here allowing unused muts.
                let mut state = if let Some(mut captures) = matcher.capture_route_into_vec(&route_string).ok().map(|x| x.1) {
                    let mut drain = captures.drain(..);
                    let create_item = || {
                         (
                            Some(
                                #enum_ident::#variant_ident(
                                    #(#fields),*
                                )
                            ),
                            state
                        )
                    };
                    let (val, state) = create_item();
                    if val.is_some() {
                        return (val, state);
                    }
                    state
                } else {
                    state
                };
            }
        }
        Fields::Unit => {
            quote! {
                let mut state = if let Some(_captures) = matcher.capture_route_into_map(&route_string).ok().map(|x| x.1) {
                    return (Some(#enum_ident::#variant_ident), state);
                } else {
                    state
                };
            }
        }
    }
}
/// Once the 'captures' exists, attempt to populate the fields from the list of captures.
Once the 'captures' exists, attempt to populate the fields from the list of captures.
Once the ' captures ' exists attempt to populate the fields from the list of captures .
fn build_variant_from_captures(
    enum_ident: &Ident,
    variant_ident: &Ident,
    fields: &Fields,
) -> TokenStream2 {
    match fields {
        Fields::Named(named_fields) => {
            let fields: Vec<TokenStream2> = named_fields
                .named
                .iter()
                .filter_map(|field: &Field| {
                    let field_ty: &Type = &field.ty;
                    field.ident.as_ref().map(|i: &Ident| {
                        let key = i.to_string();
                        (i, key, field_ty)
                    })
                })
                .map(|(field_name, key, field_ty): (&Ident, String, &Type)| {
                    quote! {
                        #field_name: {
                            let (v, s) = match captures.remove(#key) {
                                Some(value) => {
                                    <#field_ty as ::yew_router::Switch>::from_route_part(
                                        ::yew_router::route::Route {
                                            route: value,
                                            state,
                                        }
                                    )
                                }
                                None => {
                                    (
                                        <#field_ty as ::yew_router::Switch>::key_not_available(),
                                        state,
                                    )
                                }
                            };
                            match v {
                                Some(val) => {
                                    state = s; // Set state for the next var.
                                    val
                                },
                                None => return (None, s) // Failed
                            }
                        }
                    }
                })
                .collect();
            quote! {
                let mut state = if let Some(mut captures) = matcher.capture_route_into_map(&route_string).ok().map(|x| x.1) {
                    let create_item = || {
                         (
                            Some(
                                #enum_ident::#variant_ident {
                                    #(#fields),*
                                }
                            ),
                            state
                        )
                    };
                    let (val, state) = create_item();
                    if val.is_some() {
                        return (val, state);
                    }
                    state
                } else {
                    state
                };
            }
        }
        Fields::Unnamed(unnamed_fields) => {
            let fields = unnamed_fields.unnamed.iter().map(|f: &Field| {
                let field_ty = &f.ty;
                quote! {
                    {
                        let (v, s) = match drain.next() {
                            Some((_key, value)) => {
                                <#field_ty as ::yew_router::Switch>::from_route_part(
                                    ::yew_router::route::Route {
                                        route: value,
                                        state,
                                    }
                                )
                            },
                            None => {
                                (
                                    <#field_ty as ::yew_router::Switch>::key_not_available(),
                                    state,
                                )
                            }
                        };
                        match v {
                            Some(val) => {
                                state = s; // Set state for the next var.
                                val
                            },
                            None => return (None, s) // Failed
                        }
                    }
                }
            });
            quote! {
                // TODO put an annotation here allowing unused muts.
                let mut state = if let Some(mut captures) = matcher.capture_route_into_vec(&route_string).ok().map(|x| x.1) {
                    let mut drain = captures.drain(..);
                    let create_item = || {
                         (
                            Some(
                                #enum_ident::#variant_ident(
                                    #(#fields),*
                                )
                            ),
                            state
                        )
                    };
                    let (val, state) = create_item();
                    if val.is_some() {
                        return (val, state);
                    }
                    state
                } else {
                    state
                };
            }
        }
        Fields::Unit => {
            quote! {
                let mut state = if let Some(_captures) = matcher.capture_route_into_map(&route_string).ok().map(|x| x.1) {
                    return (Some(#enum_ident::#variant_ident), state);
                } else {
                    state
                };
            }
        }
    }
}
fn build_variant_from_captures ( enum_ident : & Ident , variant_ident : & Ident , fields : & Fields , ) -> TokenStream2 { match fields { Fields :: Named ( named_fields ) => { let fields : Vec < TokenStream2 > = named_fields . named . iter ( ) . filter_map ( | field : & Field | { let field_ty : & Type = & field . ty ; field . ident . as_ref ( ) . map ( | i : & Ident | { let key = i . to_string ( ) ; ( i , key , field_ty ) } ) } ) . map ( | ( field_name , key , field_ty ) : ( & Ident , String , & Type ) | { quote ! { field_name { let ( v s ) match captures remove ( key ) { Some ( value ) { field_ty as yew_router Switch from_route_part ( yew_router route Route { route value state } ) } None { ( field_ty as yew_router Switch key_not_available ( ) state ) } } match v { Some ( val ) { state s // Set state for the next var. val } None return ( None s ) // Failed } } } } ) . collect ( ) ; quote ! { let mut state if let Some ( mut captures ) matcher capture_route_into_map ( route_string ) ok ( ) map ( x x 1 ) { let create_item { ( Some ( enum_ident variant_ident { ( fields ) } ) state ) } let ( val state ) create_item ( ) if val is_some ( ) { return ( val state ) } state } else { state } } } Fields :: Unnamed ( unnamed_fields ) => { let fields = unnamed_fields . unnamed . iter ( ) . map ( | f : & Field | { let field_ty = & f . ty ; quote ! { { let ( v s ) match drain next ( ) { Some ( ( _key value ) ) { field_ty as yew_router Switch from_route_part ( yew_router route Route { route value state } ) } None { ( field_ty as yew_router Switch key_not_available ( ) state ) } } match v { Some ( val ) { state s // Set state for the next var. val } None return ( None s ) // Failed } } } } ) ; quote ! { // TODO put an annotation here allowing unused muts. let mut state if let Some ( mut captures ) matcher capture_route_into_vec ( route_string ) ok ( ) map ( x x 1 ) { let mut drain captures drain ( ) let create_item { ( Some ( enum_ident variant_ident ( ( fields ) ) ) state ) } let ( val state ) create_item ( ) if val is_some ( ) { return ( val state ) } state } else { state } } } Fields :: Unit => { quote ! { let mut state if let Some ( _captures ) matcher capture_route_into_map ( route_string ) ok ( ) map ( x x 1 ) { return ( Some ( enum_ident variant_ident ) state ) } else { state } } } } }
Once the 'captures' exists, attempt to populate the fields from the list of captures.
Once the ' captures ' exists attempt to populate the fields from the list of captures .
enum_ident
&Ident
variant_ident
&Ident
fields
&Fields
enum_ident
&Ident
variant_ident
&Ident
fields
&Fields