Crates.io | url-prefix |
lib.rs | url-prefix |
version | 2.0.4 |
source | src |
created_at | 2018-10-12 06:10:22.149232 |
updated_at | 2022-03-19 05:44:01.627225 |
description | A library for creating URL prefix strings. |
homepage | https://magiclen.org/url-prefix |
repository | https://github.com/magiclen/url-prefix |
max_upload_size | |
id | 89402 |
size | 7,448 |
This crate can be used to create URL prefix strings by inputting a protocol, a domain, a port number and a path without additional parsing.
Sometimes our web applications are run on different protocols(HTTP/HTTPS) and domains. And it is boring to write some code like below to format a URL:
let mut url_prefix = String::new();
if is_https {
url_prefix.push_str("https://");
} else {
url_prefix.push_str("http://");
}
url_prefix.push_str(domain);
if is_https && port != 443 || !is_https && port != 80 {
url_prefix.push_str(":");
url_prefix.push_str(&port.to_string());
}
Instead, we can easily use this crate to create URL prefix strings. For examples,
let prefix = url_prefix::create_prefix(url_prefix::Protocol::HTTPS, "magiclen.org", None, None);
assert_eq!("https://magiclen.org", prefix);
let prefix = url_prefix::create_prefix(url_prefix::Protocol::HTTPS, "magiclen.org", Some(8100), Some("url-prefix"));
assert_eq!("https://magiclen.org:8100/url-prefix", prefix);
https://crates.io/crates/url-prefix