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

Add unnamed matches for unnamed structs and variants. #155

Merged
merged 13 commits into from
Oct 25, 2019
Next Next commit
add variants to support unnamed variants
  • Loading branch information
hgzimmerman committed Oct 25, 2019
commit bd5ea2e734fad9907209c34d01f3ad17209b28f6
17 changes: 8 additions & 9 deletions crates/yew_router_macro/src/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,16 @@ fn write_for_token(token: &ShadowMatcherToken, naming_scheme: FieldType) -> Toke
state = state.or(#name.build_route_section(buf));
}
}
ShadowCaptureVariant::Unnamed
| ShadowCaptureVariant::ManyUnnamed
| ShadowCaptureVariant::NumberedUnnamed { .. } => panic!("Unnamed matcher sections not allowed for named field types")
},
FieldType::Unnamed { index } => match &capture {
ShadowCaptureVariant::Named(_)
| ShadowCaptureVariant::ManyNamed(_)
| ShadowCaptureVariant::NumberedNamed { .. } => {
let name = unnamed_field_index_item(index);
quote! {
state = state.or(#name.build_route_section(&mut buf));
}
FieldType::Unnamed { index } => {
let name = unnamed_field_index_item(index);
quote! {
state = state.or(#name.build_route_section(&mut buf));
}
},
}
},
ShadowMatcherToken::End => quote! {},
}
Expand Down
23 changes: 22 additions & 1 deletion crates/yew_router_macro/src/switch/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ pub enum ShadowMatcherToken {
}

pub enum ShadowCaptureVariant {
/// {}
Unnamed,
/// {*}
ManyUnnamed,
/// {5}
NumberedUnnamed{
/// Number of sections to match.
sections: usize
},
/// {name} - captures a section and adds it to the map with a given name
Named(String),
/// {*:name} - captures over many sections and adds it to the map with a given name.
Expand All @@ -49,6 +58,15 @@ impl ToTokens for ShadowCaptureVariant {
ShadowCaptureVariant::NumberedNamed { sections, name } => {
quote! {::yew_router::matcher::CaptureVariant::NumberedNamed{sections: #sections, name: #name.to_string()}}
}
ShadowCaptureVariant::Unnamed => {
quote! {::yew_router::matcher::CaptureVariant::Unnamed}
}
ShadowCaptureVariant::ManyUnnamed => {
quote! {::yew_router::matcher::CaptureVariant::ManyUnnamed}
}
ShadowCaptureVariant::NumberedUnnamed { sections} => {
quote! {::yew_router::matcher::CaptureVariant::NumberedUnnamed{sections: #sections}}
}
};
ts.extend(t)
}
Expand All @@ -74,7 +92,10 @@ impl From<CaptureVariant> for ShadowCaptureVariant {
CaptureVariant::ManyNamed(name) => SCV::ManyNamed(name),
CaptureVariant::NumberedNamed { sections, name } => {
SCV::NumberedNamed { sections, name }
}
},
CaptureVariant::Unnamed => SCV::Unnamed,
CaptureVariant::ManyUnnamed => SCV::ManyUnnamed,
CaptureVariant::NumberedUnnamed { sections } => SCV::NumberedUnnamed {sections}
}
}
}
9 changes: 9 additions & 0 deletions crates/yew_router_route_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ pub enum MatcherToken {
/// Variants that indicate how part of a string should be captured.
#[derive(Debug, PartialEq, Clone)]
pub enum CaptureVariant {
/// {}
Unnamed,
/// {*}
ManyUnnamed,
/// {5}
NumberedUnnamed{
/// Number of sections to match.
sections: usize
},
/// {name} - captures a section and adds it to the map with a given name.
Named(String),
/// {*:name} - captures over many sections and adds it to the map with a given name.
Expand Down
4 changes: 4 additions & 0 deletions src/matcher/matcher_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ fn matcher_impl<'a, 'b: 'a, CAP: CaptureCollection<'b>>(
CaptureVariant::NumberedNamed { sections, name } => {
capture_numbered_named(i, &mut iter, Some((&name, &mut captures)), *sections)?
}
CaptureVariant::Unnamed => capture_named(i, &mut iter, "", &mut captures)?,
CaptureVariant::ManyUnnamed => capture_many_named(i, &mut iter, "", &mut captures)?,
CaptureVariant::NumberedUnnamed { sections } =>
capture_numbered_named(i, &mut iter, Some(("", &mut captures)), *sections)?
},
MatcherToken::End => {
if !i.is_empty() {
Expand Down
3 changes: 3 additions & 0 deletions src/matcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ impl RouteMatcher {
| CaptureVariant::NumberedNamed { name, .. } => {
acc.insert(&name);
}
CaptureVariant::Unnamed
| CaptureVariant::ManyUnnamed
| CaptureVariant::NumberedUnnamed { .. } => {}
},
}
acc
Expand Down