Skip to content

Commit

Permalink
don't allow wildcards for now
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Feb 18, 2022
1 parent cc2e1be commit b2bd51d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
19 changes: 13 additions & 6 deletions axum-macros/src/typed_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ pub(crate) fn expand(item_struct: ItemStruct) -> syn::Result<TokenStream> {

match fields {
syn::Fields::Named(_) => {
let segments = parse_path(&path);
let segments = parse_path(&path)?;
Ok(expand_named_fields(ident, path, &segments))
}
syn::Fields::Unnamed(fields) => {
let segments = parse_path(&path);
let segments = parse_path(&path)?;
expand_unnamed_fields(fields, ident, path, &segments)
}
syn::Fields::Unit => Ok(expand_unit_fields(ident, path)?),
Expand Down Expand Up @@ -200,7 +200,7 @@ fn simple_pluralize(count: usize, word: &str) -> String {
}

fn expand_unit_fields(ident: &syn::Ident, path: LitStr) -> syn::Result<TokenStream> {
for segment in parse_path(&path) {
for segment in parse_path(&path)? {
match segment {
Segment::Capture(_, span) => {
return Err(syn::Error::new(
Expand Down Expand Up @@ -275,14 +275,21 @@ fn captures_from_path(segments: &[Segment]) -> Vec<syn::Ident> {
.collect::<Vec<_>>()
}

fn parse_path(path: &LitStr) -> Vec<Segment> {
fn parse_path(path: &LitStr) -> syn::Result<Vec<Segment>> {
path.value()
.split('/')
.map(|segment| {
if segment.contains('*') {
return Err(syn::Error::new_spanned(
path,
"`typed_path` cannot contain wildcards",
));
}

if let Some(capture) = segment.strip_prefix(':') {
Segment::Capture(capture.to_owned(), path.span())
Ok(Segment::Capture(capture.to_owned(), path.span()))
} else {
Segment::Static(segment.to_owned())
Ok(Segment::Static(segment.to_owned()))
}
})
.collect()
Expand Down
7 changes: 7 additions & 0 deletions axum-macros/tests/typed_path/fail/wildcard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use axum_extra::routing::TypedPath;

#[derive(TypedPath)]
#[typed_path("/users/*rest")]
struct MyPath;

fn main() {}
5 changes: 5 additions & 0 deletions axum-macros/tests/typed_path/fail/wildcard.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `typed_path` cannot contain wildcards
--> tests/typed_path/fail/wildcard.rs:4:14
|
4 | #[typed_path("/users/*rest")]
| ^^^^^^^^^^^^^^

0 comments on commit b2bd51d

Please sign in to comment.