| Crates.io | include_url_macro |
| lib.rs | include_url_macro |
| version | 0.1.2 |
| created_at | 2025-02-12 13:09:11.149183+00 |
| updated_at | 2025-11-24 12:44:55.423267+00 |
| description | A procedural macro to include URL content as static strings at compile time |
| homepage | |
| repository | https://github.com/hahihula/include_url_macro |
| max_upload_size | |
| id | 1552988 |
| size | 61,577 |
A Rust procedural macro that fetches URL content at compile time and includes it as a static string. It also provides JSON parsing capabilities through the include_json_url macro.
Add this to your Cargo.toml:
[dependencies]
include_url_macro = "0.1.2"
serde = { version = "1.0", features = ["derive"] } # Required for JSON parsing
serde_json = "1.0" # Required for JSON parsing
This crate supports multiple TLS backend options. Choose the one that best fits your needs:
Uses the system's native TLS implementation (OpenSSL on Linux, Secure Transport on macOS, SChannel on Windows):
[dependencies]
include_url_macro = "0.1.2"
Compiles and statically links OpenSSL, useful for CI/CD environments or when system OpenSSL is unavailable:
[dependencies]
include_url_macro = { version = "0.1.2", default-features = false, features = ["native-tls-vendored"] }
Uses rustls, a modern TLS library written in Rust with no C dependencies:
[dependencies]
include_url_macro = { version = "0.1.2", default-features = false, features = ["rustls"] }
Feature comparison:
| Feature | Default (native-tls) |
native-tls-vendored |
rustls |
|---|---|---|---|
| System certificates | ✅ Yes | ✅ Yes | ⚠️ Configurable |
| Cross-compilation | ⚠️ Requires system libs | ✅ Easy | ✅ Easy |
| Compile time | ✅ Fast | ⚠️ Slower | ✅ Fast |
| Binary size | ✅ Small | ⚠️ Larger | ✅ Small |
| Dependencies | C (OpenSSL) | C (vendored OpenSSL) | Pure Rust |
Use include_url to fetch and include raw content:
use include_url_macro::include_url;
fn main() {
// Content will be fetched at compile time
let readme = include_url!("https://raw.githubusercontent.com/rust-lang/rust/master/README.md");
println!("{}", readme);
}
Use include_json_url to fetch and parse JSON content:
use include_url_macro::include_json_url;
use serde::Deserialize;
// Parse into serde_json::Value
let json = include_json_url!("https://api.example.com/data.json");
println!("API Version: {}", json["version"]);
// Parse into a specific type
#[derive(Deserialize)]
struct User {
id: u64,
name: String,
email: String,
}
let user = include_json_url!("https://api.example.com/user.json", User);
println!("User name: {}", user.name);
include_str! macroserde_json::Valueserde::DeserializeThis macro only allows HTTP and HTTPS URLs. It performs URL validation before making any requests. Note that using this macro will make your build process dependent on network connectivity and the availability of the URLs you're including.
Both macros provide compile-time errors for:
include_json_url)Example error messages:
error: Invalid URL: relative URL without a base
--> src/main.rs:4:20
|
4 | let data = include_url!("not-a-url");
| ^^^^^^^^^
error: Failed to parse JSON into the specified type
--> src/main.rs:12:24
|
12 | let user = include_json_url!("https://api.example.com/data.json", User);
| ^^^^^^^^^^^^^
This project is licensed under the MIT License.