1 unstable release

0.1.0 May 16, 2023

#5 in #user-info

MIT/Apache

12KB
309 lines

Overview

A simple URL constructor with a bit more customizability.

If you do use this library, note that it does not do any error handling nor sanitation of inputs. Do verify the inputs before passing them to the builder (and experiment with the output).

Quick Start

In Cargo.toml:

[dependencies]
url-constructor = "0.1.0"

To create a URL:

use url_constructor::UrlConstructor;

let url = UrlConstructor::new()
    .scheme("http")
    .userinfo("alex:password1")
    .subdomain("api")
    .host("google.com")
    .port(8080)
    .subdir("v2")
    .subdir("users")
    .param("salary", ">10000")
    .param("lastName", "Wallace")
    .fragment("id")
    .build()

assert_eq!(
    url,
    "http://alex:password1@api.google.com:8080/v2/users?lastName=Wallace&salary=>10000#id"
)

Specifications

These are the current "quirks" of the constructor

  • The default scheme is https, there are no defaults for the other components
  • If the scheme is set to the empty string, then the :// part of the URL will not be returned
  • Subdomains are returned left-to-right according to the order in which it is called (see example above)
  • Control characters such as &, ?, /, # and @ are automatically added to their respectively URL components (note: no checks are made to prevent duplicates)
  • Defining each part of the host using the subdomain method should yield the same result as using host directly
  • Multiplicity of each component:
    • scheme: 0...1
    • userinfo: 0...1
    • subdomain: 0...*
    • host: 0...1
    • port: 0...1
    • subdir: 0...*
    • param: 0...*
    • fragment: 0...*

No runtime deps