Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add initial support for wasm target #92

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rust-version = "1.64.0"
members = [
"rrule",
"rrule-debugger",
"rrule-wasm"
fmeringdal marked this conversation as resolved.
Show resolved Hide resolved
]

# These are the 2 packages to mainly work on.
Expand Down
20 changes: 20 additions & 0 deletions rrule-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "rrule-wasm"
version = "0.1.0"
authors = ["Ralph Bisschops <ralph.bisschops.dev@gmail.com>"]
publish = false
license.workspace = true
edition.workspace = true
rust-version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rrule = { path = "../rrule" }
wasm-bindgen = "0.2.63"

[lib]
crate-type = ["cdylib", "rlib"]

[dev-dependencies]
wasm-bindgen-test = "0.3.13"
17 changes: 17 additions & 0 deletions rrule-wasm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
install-wasm-pack:
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

build-wasm-nodejs:
wasm-pack build --target nodejs

test-wasm-on-nodejs:
node example/nodejs/app.js

build-wasm-web:
wasm-pack build --target web

test-wasm-on-web-browser:
cd example/web && npx http-server

run-unit-tests:
wasm-pack test --node
20 changes: 20 additions & 0 deletions rrule-wasm/example/nodejs/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { get_all_date_recurrences } = require('../../pkg/rrule_wasm.js');

const http = require('http');
const url = require('url');
const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
const queryObject = url.parse(req.url,true).query;
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');

var data = get_all_date_recurrences("DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3", 100);
fmeringdal marked this conversation as resolved.
Show resolved Hide resolved
console.log(data);
res.end(data.toString());
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
23 changes: 23 additions & 0 deletions rrule-wasm/example/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
</head>
<body>

<script type="module">
import init, { get_all_date_recurrences } from '../../pkg/rrule_wasm.js';

async function run_wasm() {
await init();
var data = get_all_date_recurrences("DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3", 100);
fmeringdal marked this conversation as resolved.
Show resolved Hide resolved
console.log(data);
alert(data);
}

run_wasm();
</script>
</body>
</html>
19 changes: 19 additions & 0 deletions rrule-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use rrule::RRuleSet;
fmeringdal marked this conversation as resolved.
Show resolved Hide resolved
use wasm_bindgen::prelude::*;

/**
Get all recurrences of the rrule!
rule_set_string: List of rrules
limit: Limit must be set in order to prevent infinite loops
*/
#[wasm_bindgen]
pub fn get_all_date_recurrences(rule_set_string: &str, limit: Option<u16>) -> Vec<JsValue> {
fmeringdal marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we try to mimic the API for the rrule.js library? I think that would make it much simpler for users to swap out rrule.js for this wasm library.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I think it's a good idea

let rrule: RRuleSet = rule_set_string.parse().unwrap();
fmeringdal marked this conversation as resolved.
Show resolved Hide resolved
// Set hard limit in case of infinitely recurring rules
let rule_set = rrule.all(limit.unwrap_or(100));
let result: Vec<JsValue> = rule_set.dates
.into_iter()
.map(|s| JsValue::from_str(&Some(s).unwrap().to_string()))
.collect();
return result;
}
11 changes: 11 additions & 0 deletions rrule-wasm/tests/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use wasm_bindgen_test::*;
use rrule_wasm::get_all_date_recurrences;

#[wasm_bindgen_test]
fn test_date_recurrences() {
fmeringdal marked this conversation as resolved.
Show resolved Hide resolved
let dates: Vec<wasm_bindgen::JsValue> = get_all_date_recurrences("DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3", Some(100));
assert_eq!(dates.len(), 3);
assert_eq!(dates.get(0).unwrap().as_string().unwrap(), "2012-02-01 09:30:00 UTC");
assert_eq!(dates.get(1).unwrap().as_string().unwrap(), "2012-02-02 09:30:00 UTC");
assert_eq!(dates.get(2).unwrap().as_string().unwrap(), "2012-02-03 09:30:00 UTC");
}