Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacicada committed Oct 23, 2022
0 parents commit 64802b6
Show file tree
Hide file tree
Showing 7 changed files with 412 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
/Cargo.lock
/.vscode
14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "rhai-url"
version = "0.0.1"
edition = "2021"
repository = "https://github.com/lucacicada/rhai-url"
readme = "README.md"
license = "MIT"
description = "Url package for Rhai"
keywords = ["scripting", "scripting-language", "embedded", "rhai", "url"]
categories = ["embedded"]

[dependencies]
rhai = { version = ">=1.9" }
url = { version = ">=2.0" }
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Luca

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# `rhai-url`

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

## Usage

### `Cargo.toml`

```toml
[dependencies]
rhai-url = "0.0.1"
```

### [Rhai] script

```js
let url = Url("http://example.com/?q=query");

print(url.href); // print 'http://example.com/?q=query'
print(url.to_string()); // print 'http://example.com/?q=query'

print(url.query); // print 'q=query'

// fragment and hash are aliases
print(url.fragment); // print ''
print(url.hash); // print ''

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

url.query_remove("q");
url.query_append("q", "name");
```

### Rust source

```rust
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(())
}
```
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use rhai::def_package;
use rhai::plugin::*;

pub(crate) mod url;

def_package! {
pub UrlPackage(lib) {
combine_with_exported_module!(lib, "rhai_url", url::url_module);
}
}
172 changes: 172 additions & 0 deletions src/url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
use rhai::plugin::*;

#[export_module]
pub mod url_module {
use url::Url;

#[rhai_fn(name = "Url", return_raw)]
pub fn new(url: &str) -> Result<Url, Box<EvalAltResult>> {
Url::parse(url).map_err(|e| Box::<EvalAltResult>::from(e.to_string()))
}

#[rhai_fn(global, get = "href", pure)]
pub fn href(url: &mut Url) -> ImmutableString {
url.to_string().into()
}

#[rhai_fn(global, get = "scheme", pure)]
pub fn scheme(url: &mut Url) -> ImmutableString {
url.scheme().into()
}

#[rhai_fn(global, set = "scheme", pure)]
pub fn set_scheme(url: &mut Url, value: &str) {
_ = url.set_scheme(value);
}

#[rhai_fn(global, get = "domain", pure)]
pub fn domain(url: &mut Url) -> ImmutableString {
url.domain().unwrap_or("").into()
}

#[rhai_fn(global, get = "path", pure)]
pub fn path(url: &mut Url) -> ImmutableString {
url.path().into()
}

#[rhai_fn(global, set = "path", pure)]
pub fn set_path(url: &mut Url, value: &str) {
url.set_path(value)
}

#[rhai_fn(global, get = "query", pure)]
pub fn query(url: &mut Url) -> ImmutableString {
url.query().unwrap_or("").into()
}

#[rhai_fn(global, set = "query", pure)]
pub fn set_query(url: &mut Url, value: &str) {
if value.len() > 0 {
url.set_query(Some(value))
} else {
url.set_query(None)
}
}

#[rhai_fn(global, set = "query", pure)]
pub fn set_query_option(url: &mut Url, value: Option<&str>) {
match value {
Some(value) => url.set_query(Some(value)),
None => url.set_query(None),
}
}

#[rhai_fn(global, get = "fragment", pure)]
pub fn fragment(url: &mut Url) -> ImmutableString {
url.fragment().unwrap_or("").into()
}

#[rhai_fn(global, set = "fragment", pure)]
pub fn set_fragment(url: &mut Url, value: &str) {
if value.len() > 0 {
url.set_fragment(Some(value))
} else {
url.set_fragment(None)
}
}

#[rhai_fn(global, set = "fragment", pure)]
pub fn set_fragment_option(url: &mut Url, value: Option<&str>) {
match value {
Some(value) => url.set_fragment(Some(value)),
None => url.set_query(None),
}
}

#[rhai_fn(global, get = "hash", pure)]
pub fn hash(url: &mut Url) -> ImmutableString {
url.fragment().unwrap_or("").into()
}

#[rhai_fn(global, set = "hash", pure)]
pub fn set_hash(url: &mut Url, value: &str) {
if value.len() > 0 {
url.set_fragment(Some(value))
} else {
url.set_fragment(None)
}
}

#[rhai_fn(global, set = "hash", pure)]
pub fn set_hash_option(url: &mut Url, value: Option<&str>) {
match value {
Some(value) => url.set_fragment(Some(value)),
None => url.set_query(None),
}
}

/*************************************************************
* Functions
************************************************************/

#[rhai_fn(
global,
name = "query_clear",
name = "query_delete",
name = "query_remove",
pure
)]
pub fn query_clear(url: &mut Url) {
url.set_query(None)
}

#[rhai_fn(global, name = "query_delete", name = "query_remove", pure)]
pub fn query_delete(url: &mut Url, key: &str) {
let query: Vec<(String, String)> = url
.query_pairs()
.filter(|(name, _)| name != key)
.map(|(name, value)| (name.into_owned(), value.into_owned()))
.collect();

url.query_pairs_mut().clear().extend_pairs(&query);

// cleanup
if let Some(q) = url.query() {
if q.len() == 0 {
url.set_query(None)
}
}
}

#[rhai_fn(global, name = "query_append", pure)]
pub fn query_append(url: &mut Url, key: &str, value: &str) {
url.query_pairs_mut().append_pair(key, value);
}

#[rhai_fn(global, name = "query_set", pure)]
pub fn query_set(url: &mut Url, key: &str, value: &str) {
query_delete(url, key);
query_append(url, key, value);
}

#[rhai_fn(global, name = "query_get", pure)]
pub fn query_get(url: &mut Url, key: &str) -> ImmutableString {
match url.query_pairs().find(|(name, _)| name == key) {
Some((_, value)) => ImmutableString::from(value.as_ref()),
None => ImmutableString::from(""),
}
}

#[rhai_fn(global, name = "query_gets", name = "query_getAll", pure)]
pub fn query_gets(url: &mut Url, key: &str) -> Vec<ImmutableString> {
url.query_pairs()
.filter(|(name, _)| name == key)
.map(|(_, value)| ImmutableString::from(value.as_ref()))
.collect()
}

#[rhai_fn(global, name = "to_string", name = "to_debug", pure)]
pub fn to_string(url: &mut Url) -> ImmutableString {
url.to_string().into()
}
}
Loading

0 comments on commit 64802b6

Please sign in to comment.