Crate rhai_url

source ·
Expand description

About rhai-url

License crates.io crates.io API Docs

This crate provides url::Url access for the Rhai scripting language.

Usage

Cargo.toml

[dependencies]
rhai-url = "0.0.4"

Rhai script

// create a new Url
let url = Url("http://example.com/?q=query");

// get the absolute url as a string
print(url.href); // print 'http://example.com/?q=query'
print(url.to_string()); // print 'http://example.com/?q=query'

// get the url query string, without the leading ?
print(url.query); // print 'q=query'

// get the url fragment
print(url.fragment); // print ''

// hash is an alias of fragment
print(url.hash); // print ''

// clear the query
url.query_clear();
print(url.query); // print ''

// remove a query key
url.query_remove("q");

// query_remove with no arguments will clear the query string
url.query_remove();

// adds a query key value pair into the query string
url.query_append("q", "name");

You can see an example on how to use those function in the tests.

Rust source

use rhai::{Engine, EvalAltResult};
use rhai::packages::Package;
use rhai_url::UrlPackage;
use url::Url;

fn main() -> Result<(), Box<EvalAltResult>> {
    // Create Rhai scripting engine
    let mut engine = Engine::new();

    // Create url package and add the package into the engine
    let package = UrlPackage::new();
    package.register_into_engine(&mut engine);

    // Print the url
    let url = engine.eval::<Url>(r#"Url("http://test.dev/")"#)?;

    println!("{}", url);

    // Print the url string, equivalent of to_string()
    let href = engine.eval::<String>(r#"Url("http://test.dev/").href"#)?;

    println!("{}", href);

    Ok(())
}

Features

FeatureDefaultDescription
arrayenabledEnables support for Rhai Array
metadatadisabledEnables support for generating package documentation

API

The following functions are defined in this package:

Url(url: &str) -> Url

Creates a new Url.

Example

let url = Url("http://test.dev/")

property get domain(url: Url) -> String

Gets the Url domain.

Example

let url = Url("http://test.dev/")
let domain = url.domain // 'test.dev'

property get fragment(url: Url) -> String

Gets the Url fragment.

Example

let url = Url("http://test.dev/?#row=4")
let fragment = url.fragment // 'row=4'

property get hash(url: Url) -> String

Gets the Url hash, alias of fragment.

Example

let url = Url("http://test.dev/?#row=4");
let hash = url.hash; // 'row=4'

property get href(url: Url) -> String

Gets the full Url, same as to_string().

Example

let url = Url("http://test.dev/")
let fullUrl = url.href // 'http://test.dev/'

property get path(url: Url) -> String

Gets the Url path.

Example

let url = Url("http://test.dev/path")
let path = url.path // '/path'

property get query(url: Url) -> String

Gets the Url query string.

Example

let url = Url("http://test.dev/?page=2")
let query = url.query // 'page=2'

property get scheme(url: Url) -> String

Gets the Url scheme such as ‘http’ or ‘https’.

Example

let url = Url("http://test.dev/")
let scheme = url.scheme // 'http'

query_append(url: Url, key: &str, value: &str)

query_clear(url: Url)

Clear the query string.

Example

let url = Url("http://test.dev/?q=query&b=1");

url.query_clear();

url == "http://test.dev/"

query_delete

query_delete(url: Url)

Clear the query string.

Example

let url = Url("http://test.dev/?q=query&b=1");

url.query_clear();

url == "http://test.dev/"

query_delete(url: Url, key: &str)

Delete a key from the query

Example

let url = Url("http://test.dev/?q=query&b=1");

url.query_delete("q"); // or
url.query_remove("q");

url == "http://test.dev/?b=1"

query_get(url: Url, key: &str) -> String

Gets a query value for the specified key, it will return the first value found

Example

let url = Url("http://test.dev/?q=query&b=1");

url.query_get("q"); // "query"
url.query_get("b"); // "1"

query_getAll(url: Url, key: &str) -> Array

Gets a list of values for the specified key

Not available under no_index.

Example

let url = Url("http://test.dev/?q=query&q=second-query");

url.query_get("q"); // ["query", "second-query"]

query_gets(url: Url, key: &str) -> Array

Gets a list of values for the specified key

Not available under no_index.

Example

let url = Url("http://test.dev/?q=query&q=second-query");

url.query_get("q"); // ["query", "second-query"]

query_remove

query_remove(url: Url)

Clear the query string.

Example

let url = Url("http://test.dev/?q=query&b=1");

url.query_clear();

url == "http://test.dev/"

query_remove(url: Url, key: &str)

Delete a key from the query

Example

let url = Url("http://test.dev/?q=query&b=1");

url.query_delete("q"); // or
url.query_remove("q");

url == "http://test.dev/?b=1"

query_set(url: Url, key: &str, value: &str)

Sets a query key

Example

let url = Url("http://test.dev/?q=query&b=1");

url.query_set("q", "new-query"); // or
url.query_remove("q");

url == "http://test.dev/?q=new-query&b=1"

property set fragment(url: Url, value: &str)

Sets the Url fragment.

property set fragment(url: Url, value: Option<&str>)

Sets the Url fragment.

property set hash(url: Url, value: Option<&str>)

Sets the Url hash.

property set hash(url: Url, value: &str)

Sets the Url hash.

property set path(url: Url, value: &str)

Sets the Url path.

property set query(url: Url, value: &str)

Sets the Url query string.

property set query(url: Url, value: Option<&str>)

Sets the Url query string.

property set scheme(url: Url, value: &str)

Sets the Url scheme.

Be aware as this method will refuse to change the scheme under the following circumstances:

  • If the new scheme is not in [a-zA-Z][a-zA-Z0-9+.-]+
  • If this URL is cannot-be-a-base and the new scheme is one of http, https, ws, wss or ftp
  • If either the old or new scheme is http, https, ws, wss or ftp and the other is not one of these
  • If the new scheme is file and this URL includes credentials or has a non-null port
  • If this URL’s scheme is file and its host is empty or null

Example

let url = Url("http://test.dev/")
url.scheme = "https"

let scheme = url.scheme // 'https'
let fullUrl = url.href // 'https://test.dev/'

to_debug(url: Url) -> String

Get the absolute url as a string

Example

let url = Url("http://test.dev/path");

url.to_string(); // "http://test.dev/path"
url.to_debug(); // "http://test.dev/path"

to_string(url: Url) -> String

Get the absolute url as a string

Example

let url = Url("http://test.dev/path");

url.to_string(); // "http://test.dev/path"
url.to_debug(); // "http://test.dev/path"

Structs