#url #prefix #http #https #ftp

no-std url-prefix

A library for creating URL prefix strings

23 stable releases

2.0.3 Apr 22, 2021
2.0.2 Mar 11, 2021
2.0.1 Jul 29, 2020
1.3.5 Jun 27, 2020
1.3.0 Nov 16, 2018

#24 in Value formatting

Download history 25/week @ 2021-08-05 25/week @ 2021-08-12 1/week @ 2021-08-19 3/week @ 2021-08-26 2/week @ 2021-09-16 24/week @ 2021-09-30 23/week @ 2021-10-07 1/week @ 2021-10-14 1/week @ 2021-10-21 2/week @ 2021-10-28 42/week @ 2021-11-04 1/week @ 2021-11-11 5/week @ 2021-11-18

51 downloads per month

MIT license

6KB
74 lines

URL Prefix

CI

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.

Why We Need This?

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,

extern crate url_prefix;

let prefix = url_prefix::create_prefix(url_prefix::Protocol::HTTPS, "magiclen.org", None, None);

assert_eq!("https://magiclen.org", prefix);
extern crate url_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);

Crates.io

https://crates.io/crates/url-prefix

Documentation

https://docs.rs/url-prefix

License

MIT

Dependencies

~63KB