1 unstable release

0.1.1 Dec 5, 2021
0.1.0 Dec 5, 2021

#2185 in Parser implementations

Custom license

115KB
2.5K SLoC

Uri

This is a library which implements IETF RFC 3986, "Uniform Resource Identifier (URI): Generic Syntax".

More information about this library can be found in the crate documentation.

A URI is a compact sequence of characters that identifies an abstract or physical resource. One common form of URI is the Uniform Resource Locator (URL), used to reference web resources:

http://www.example.com/foo?bar#baz

Another kind of URI is the path reference:

/usr/bin/zip

The purpose of this library is to provide a Uri type to represent a URI, with functions to parse URIs from their string representations, as well as assemble URIs from their various components.

Credits

This library has been forked from rhymuri because it was unmaintained at the time. Thanks to Richard Walters for the original implementation!

License

Licensed under the MIT license.


lib.rs:

This crate implements IETF RFC 3986, "Uniform Resource Identifier (URI): Generic Syntax". The Uri type can be used to parse and generate RFC-conformant URI strings to and from their various components.

A Uniform Resource Identifier (URI) is a compact sequence of characters that identifies an abstract or physical resource. One common form of URI is the Uniform Resource Locator (URL), used to reference web resources:

http://www.example.com/foo?bar#baz

Another kind of URI is the path reference:

/usr/bin/zip

Examples

Parsing a URI into its components

use uris::Uri;

let uri = Uri::parse("http://www.example.com/foo?bar#baz").unwrap();
let authority = uri.authority().unwrap();
assert_eq!("www.example.com".as_bytes(), authority.host());
assert_eq!(
    Some("www.example.com"),
    uri.host_to_string().unwrap().as_deref()
);
assert_eq!("/foo", uri.path_to_string().unwrap());
assert_eq!(Some("bar"), uri.query_to_string().unwrap().as_deref());
assert_eq!(Some("baz"), uri.fragment_to_string().unwrap().as_deref());

Generating a URI from its components

use uris::{
    Authority,
    Uri,
};

let mut uri = Uri::default();
assert!(uri.set_scheme(String::from("http")).is_ok());
let mut authority = Authority::default();
authority.set_host("www.example.com");
uri.set_authority(Some(authority));
uri.set_path_from_str("/foo");
uri.set_query(Some("bar".into()));
uri.set_fragment(Some("baz".into()));
assert_eq!("http://www.example.com/foo?bar#baz", uri.to_string());

Dependencies

~2MB
~47K SLoC