Closed zer0tonin closed 6 years ago
The cause is that the macro expects an "ident", in other words a valid Rust variable name. I don't know a way to easily fix this :-/
Could you also take an expr
string literal?
Edit: Looks like @bb010g beat me to it!
Would it be possible to modify the macro to take a string instead of a series of idents (maybe in a version bump in conjunction with other breaking changes (#154))? I've been meaning to take a look since I've run into something similar, allowing dots in urls.
Here's an example: I want to capture a "keys" parameter after /
, but also handle specific exact file requests (for example favicon.ico
and robots.txt
) all in the router:
router!(request,
(GET) (/favicon.ico) => { handlers::match_asset_exact(request)? },
(GET) (/robots.txt) => { handlers::match_asset_exact(request)? },
(GET) (/something_special.txt) => { handlers::match_asset_exact(request)? },
(GET) (/{key: String}) => { ... },
... handle other static files (`/static/<file>`) ...
Currently I have to put "key parameter matching" under a prefix and serve the files with rouille::match_assets
:
.
├── assets
│ ├── favicon.ico
│ ├── robots.txt
│ ├── something_special.txt
│ └── static/
(GET) (/a/{key: String}) => { ... },
_ => {
let resp = rouille::match_assets(&request, "assets");
if resp.is_success() { resp }
else { bail!(DoesNotExist) }
}
Could you also take an expr string literal?
You can't analyse the content of a string literal at compile-time, so a syntax like (GET) ("/users/{id}/name")
isn't possible.
Something like (GET) ("/users/" {id} "/name")
could work but it's not very nice nor friendly.
(GET) (/ "2")
doesn't look horrible, though, and you wouldn't be using strings that often. (Or is it only possible to take either expr
s or ident
s, and not a combo of the two?)
I don't think a mishmash of expr
s and ident
s would be possible. Maybe a workaround could be something like:
(GET) ("/nothing/special") => { ... },
(GET) ("/special/{key}/name/{id}", {key: String, id: u32}) => { ... },
where the macro can branch on the number/format of arguments inside the second section.
Edit: It's much easier to parse (with a macro) as:
(GET) ("/special/{key}/name/{id}", key: String, id: u32) => { ... },
The syntax (GET) ("/special/{key}/name/{id}", {key: String, id: u32}) => { ... },
also requires some runtime parsing which adds an overhead.
However I think it should be possible to allow both idents and strings with two different macro paths.
Right now runtime parsing is only required when params are present. If only a string is provided, then it just does a string comparison (I need to make a small adjustment to avoid creating extra Vec
s).
The alternative router syntax should be available now as of 2.0
Hello,
It looks like the application won't compile if the routes have hardcoded numbers in them. For example this works :
But this doesn't :
It leads to the following error:
Is this normal behaviour? Is there anyway to get around this?