Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile-time url parsing 'url!' macro #136

Closed
frewsxcv opened this issue Nov 18, 2015 · 9 comments
Closed

Compile-time url parsing 'url!' macro #136

frewsxcv opened this issue Nov 18, 2015 · 9 comments
Assignees

Comments

@frewsxcv
Copy link
Contributor

Inspired by sfackler/rust-postgres-macros which is a compile time SQL parser

There are ton of instances in servo, where we do something like Url::parse("http://mozilla.com").unwrap(). It'd be great if we could do that validation at compile time with a macro (e.g. url!("http://mozilla.com")).

I have no idea how feasible this is, just throwing around the idea.

@SimonSapin
Copy link
Member

Sounds feasible with a plugin, maybe not specific to URLs. Anything that implements FromStr?

@KiChjang
Copy link
Contributor

I wonder whether regex would be enough to parse URLs? The language of URLs seem regular enough to me (I may be wrong though).

@SimonSapin
Copy link
Member

@KiChjang A regex may(?) be able to separate the components of an URL, but the parser as specified in https://url.spec.whatwg.org/#url-parsing does more stuff along the way such as percent-decoding, IDNA, resolving relative URLs, normalizing (anti)slashes, collapsing . and .. path components, etc. Anyway, this is unrelated to this issue.


Making a plugin generic over T: FromStr wouldn’t work, the plugin needs to know at its own compile-time which crates it should depend on.

Another approach might be to skip string parsing entirely. All fields of Url are public, so it can be constructed directly as a struct literal.

@SimonSapin
Copy link
Member

Larger explanation of the above, copied from IRC:

  • SimonSapin: You can make a compiler plugin (syntax extension) so that url!("http://example") runs Url::parse() at compile time and expands the result to a struct literal. Then I was thinking, why not generalize this to anything that implements FromStr?
  • SimonSapin: like from_str!(::url::Url, "http://example")
  • SimonSapin: the problem is that by the time such a plugin would see the ::url::Url path, it’s already compiled and can not add to itself a new dependency to the url crate

@frewsxcv
Copy link
Contributor Author

@SimonSapin @KiChjang just to prevent duplicate efforts, are either of you working on this currently?

@KiChjang
Copy link
Contributor

No I'm not working on it at all. You can assign the issue to yourself.

@frewsxcv frewsxcv self-assigned this Nov 19, 2015
@frewsxcv frewsxcv changed the title Compile time url parsing 'url!' macro Compile-time url parsing 'url!' macro Nov 19, 2015
frewsxcv added a commit that referenced this issue Nov 20, 2015
@frewsxcv
Copy link
Contributor Author

Opened a PR #137

frewsxcv added a commit to frewsxcv/servo that referenced this issue Nov 20, 2015
frewsxcv added a commit to frewsxcv/servo that referenced this issue Nov 20, 2015
bors-servo pushed a commit to servo/servo that referenced this issue Nov 20, 2015
Implement 'url!(..)' macro

servo/rust-url#136

servo/rust-url#137

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8622)
<!-- Reviewable:end -->
frewsxcv added a commit to frewsxcv/servo that referenced this issue Nov 20, 2015
bors-servo pushed a commit to servo/servo that referenced this issue Nov 21, 2015
Implement 'url!(..)' macro

servo/rust-url#136

servo/rust-url#137

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8622)
<!-- Reviewable:end -->
@nox
Copy link
Contributor

nox commented Nov 21, 2015

#137

@nox nox closed this as completed Nov 21, 2015
frewsxcv added a commit to frewsxcv/servo that referenced this issue Nov 21, 2015
bors-servo pushed a commit to servo/servo that referenced this issue Nov 21, 2015
Implement 'url!(..)' macro

servo/rust-url#136

servo/rust-url#137

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8622)
<!-- Reviewable:end -->
paulrouget pushed a commit to paulrouget/servo that referenced this issue Nov 25, 2015
jrmuizel pushed a commit to jrmuizel/gecko-cinnabar that referenced this issue Jun 12, 2017
…ugin); r=SimonSapin

servo/rust-url#136

servo/rust-url#137

Source-Repo: https://github.com/servo/servo
Source-Revision: ea690a2dff64d1cb4eb668473d62f1bbcb19f7c8
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this issue Oct 1, 2019
…ugin); r=SimonSapin

servo/rust-url#136

servo/rust-url#137

Source-Repo: https://github.com/servo/servo
Source-Revision: ea690a2dff64d1cb4eb668473d62f1bbcb19f7c8

UltraBlame original commit: 5bb91f3403d68562020f7bb5215820646e7cc5c6
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this issue Oct 1, 2019
…ugin); r=SimonSapin

servo/rust-url#136

servo/rust-url#137

Source-Repo: https://github.com/servo/servo
Source-Revision: ea690a2dff64d1cb4eb668473d62f1bbcb19f7c8

UltraBlame original commit: 5bb91f3403d68562020f7bb5215820646e7cc5c6
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this issue Oct 1, 2019
…ugin); r=SimonSapin

servo/rust-url#136

servo/rust-url#137

Source-Repo: https://github.com/servo/servo
Source-Revision: ea690a2dff64d1cb4eb668473d62f1bbcb19f7c8

UltraBlame original commit: 5bb91f3403d68562020f7bb5215820646e7cc5c6
@DenisGorbachev
Copy link

DenisGorbachev commented Jul 19, 2024

There's a url-macro crate that provides url! macro for compile-time validation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants