url-static
A simple macro for compile-time URL validation.
Features
- Validate literal URL strings at compile time
- Usable in
staticcontexts (with caveats, see below) - Basic support for values from certain other macros that resolve to string literals (
env!andconcat!so far, see below) - 100% safe (as in
#![forbid(unsafe_code)])
Installation
To use the macro, install both the url-static and url crates as dependencies. You can do this using cargo like so:
Examples
use url;
let api = url!;
let crate_repository = url!;
Use with static
To use this macro in static contexts, wrap the value with std::sync::LazyLock:
use LazyLock;
use Url;
use url;
static API: = new;
This is a technical restriction of the url crate. The only public constructors for url::Url aren't available in const contexts. As a result, this macro can only ensure that a value won't cause the URL parser to panic at runtime 🤷♀️Actually creating the URL must happen in a non-const context.
Macros
Due to technical limitations of proc-macros in Rust, url! can only resolve certain specific macro expressions. So far, these include:
This restriction is more relaxed in +nightly builds. When using the unstable crate feature, we use TokenStream::expand_expr to have the compiler expand macros for us.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/license/MIT)
Contributions
Issues and pull requests are welcome at git.average.name. You may create an account there to contribute, or use an existing Codeberg or GitHub account.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.