commit
96365c02f4
@ -0,0 +1,2 @@
|
||||
/target
|
||||
/Cargo.lock
|
@ -0,0 +1,21 @@
|
||||
[package]
|
||||
name = "roll_lib"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
caith = "4.2.3"
|
||||
wasm-bindgen = "0.2.87"
|
||||
# add getrandom feature to enable JS usage
|
||||
getrandom = { version = "0.2", features = ["js"] }
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[profile.release]
|
||||
opt-level = "z"
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
|
@ -0,0 +1,30 @@
|
||||
# `roll_lib`
|
||||
|
||||
An example library to show how to set up Rust as a Webassembly module.
|
||||
|
||||
## Building Instructions
|
||||
|
||||
To build the library, use the `wasm-pack` cargo extension.
|
||||
It can be installed with `cargo install wasm-pack`.
|
||||
|
||||
After installing, use `wasm-pack build --release --target web` to build the artifacts into the `pkg` directory.
|
||||
|
||||
Then, you can use WASM APIs in Javascript to use the library within Javascript.
|
||||
Note that you need the entire `pkg/` directory available to your browser to have it function correctly.
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import init, { roll } from "./pkg/roll_lib.js";
|
||||
init().then(() => {
|
||||
console.log(roll("1d20"));
|
||||
});
|
||||
</script>
|
||||
```
|
||||
|
||||
Since WASN files can not generally be loaded from `file://` for security reasons, you can use `python -m http.server` to open a quick localhost server for testing.
|
||||
|
||||
## Features
|
||||
|
||||
There is one function `roll(string) -> string`.
|
||||
It follows the format given in the README of the [`caith`](https://github.com/Geobert/caith) library.
|
||||
It may return an error or "Invalid format" if something goes wrong.
|
@ -0,0 +1,16 @@
|
||||
<!doctype html>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>hello-wasm example</title>
|
||||
</head>
|
||||
<body>
|
||||
<script type="module">
|
||||
import init, { roll } from "./pkg/roll_lib.js";
|
||||
init().then(() => {
|
||||
console.log(roll("1d20"));
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -0,0 +1,30 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
use caith::{RollResultType, Roller, RollError};
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn roll(s: String) -> String {
|
||||
match rust_roll(&s) {
|
||||
Ok(roll) => match roll {
|
||||
RollResultType::Single(single) => {
|
||||
single.to_string(false)
|
||||
},
|
||||
_ => "Unsupported format".to_string(),
|
||||
},
|
||||
Err(err) => err.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn rust_roll(s: &str) -> Result<RollResultType, RollError> {
|
||||
Ok(Roller::new(&s)?.roll()?.get_result().clone())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use super::roll;
|
||||
|
||||
#[test]
|
||||
fn test_roll_d20() {
|
||||
let answer = roll("1d20");
|
||||
println!("Answer: {}", answer);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue