canary-rs/book/src/impl-rs/usage.md

96 lines
3.6 KiB
Markdown
Raw Normal View History

2022-08-12 23:05:46 +00:00
# Using `canary-rs`
[`canary-rs`](https://git.tebibyte.media/canary/canary-rs) is the reference
implementation for Canary. It is written in Rust, and is licensed under the
LGPLv3.
2022-08-17 02:07:23 +00:00
`canary-rs` is the central hub for Canary's development. It includes host-side
Rust code, helper crates for Canary hosts, wrapper crates for scripts
authored in Rust, and even the source code for the documentation that you're
currently reading.
2022-08-12 23:05:46 +00:00
2022-10-25 22:57:28 +00:00
`canary-rs` provides a graphical "sandbox" that embeds the Canary runtime
2022-08-17 02:07:23 +00:00
into a lightweight graphical app. It has two purposes: first, to give
2022-08-12 23:05:46 +00:00
script authors a playground independent of a larger framework to safely debug,
2022-08-17 02:07:23 +00:00
benchmark, and experiment with their scripts, and second, to give Canary
2022-08-12 23:05:46 +00:00
embedders a live, functioning example of how Canary can be integrated into their
applications.
2022-10-25 22:57:28 +00:00
# Running the `canary-rs` sandbox
2022-08-12 23:05:46 +00:00
2022-10-25 22:57:28 +00:00
The sandbox requires a Canary script to run. If you don't already have one,
2022-08-12 23:05:46 +00:00
you can follow [these instructions](optional-building-the-sword-art-online-demonstration-ui-script)
2022-08-17 02:07:23 +00:00
to build the example script provided by `canary-rs`.
2022-08-12 23:05:46 +00:00
2022-10-25 22:57:28 +00:00
## Building the sandbox
2022-08-12 23:05:46 +00:00
2022-10-25 22:57:28 +00:00
To build the sandbox from source, first make sure that you have
2022-08-12 23:05:46 +00:00
[installed the standard Rust toolchain](https://www.rustlang.org/tools/install),
including `rustup`, `rustc`, and `cargo`, as well as a frontend to
[Git](https://git-scm.com/). This guide assumes that you are using the Git
command-line interface (CLI).
Next, clone the upstream repository:
```sh
$ git clone https://git.tebibyte.media/canary/canary-rs.git
$ cd canary-rs
```
2022-10-25 22:57:28 +00:00
Then, run `cargo` to build the sandbox package:
2022-08-12 23:05:46 +00:00
```sh
2022-10-25 22:57:28 +00:00
$ cargo build --release -p canary_sandbox
2022-08-12 23:05:46 +00:00
```
2022-10-25 22:57:28 +00:00
Now, the sandbox can be ran with a script:
2022-08-12 23:05:46 +00:00
```sh
2022-10-25 22:57:28 +00:00
$ cargo run --release -p canary_sandbox -- <path-to-script>
2022-08-12 23:05:46 +00:00
```
## (Optional) Building the Sword Art Online demonstration UI script
2022-08-17 02:07:23 +00:00
`canary-rs` provides an example of a fully-functioning script which, optionally,
2022-10-25 22:57:28 +00:00
can be built and loaded into the sandbox to ensure its functioning.
2022-08-12 23:05:46 +00:00
2022-08-17 02:07:23 +00:00
To build it, you must first follow [the instructions above](#building-the-test-harness)
2022-10-25 22:57:28 +00:00
to clone and build the sandbox and to set up the Rust toolchain.
2022-08-12 23:05:46 +00:00
Then, add the `wasm32-unknown-unknown` target so that Rust can compile to
WebAssembly:
```sh
$ rustup target add wasm32-unknown-unknown
```
Next, compile the example script:
```sh
2022-09-12 17:58:47 +00:00
$ cargo build --release -p sao-ui-rs --target wasm32-unknown-unknown
2022-08-12 23:05:46 +00:00
```
The path to the built example script is `target/wasm32-unknown-unknown/release/sao_ui_rs.wasm`.
2022-10-25 22:57:28 +00:00
Now it can be run using the sandbox:
2022-08-12 23:05:46 +00:00
```sh
2022-10-25 22:57:28 +00:00
$ cargo run --release -p canary_sandbox -- target/wasm32-unknown-unknown/release/sao_ui_rs.wasm
2022-08-12 23:05:46 +00:00
```
# Using `canary-rs` as a Rust library
2022-08-17 02:07:23 +00:00
***WARNING***: `canary-rs` is still in alpha development so both its API and its
version number are unstable. It is not recommended to use it in your own
projects unless you are involved with Canary's development.
2022-08-12 23:05:46 +00:00
`canary-rs` is not yet available on [crates.io](https://crates.io), so to add it
as a dependency, you must add its [upstream git repository](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories)
instead:
```toml
[dependencies]
canary = { git = "https://git.tebibyte.media/canary/canary-rs", rev = "deadbeef" }
```
Because `canary-rs` is still under active development, it is recommended to
2022-08-17 02:07:23 +00:00
pull a fixed, specific commit using the `rev` key. That can be a specific tag,
some point in the commit history, or whatever the latest commit on `main` is.
2022-08-12 23:05:46 +00:00
2022-08-17 02:07:23 +00:00
[Tebibyte Media](https://tebibyte.media) is not capable of hosting rustdocs yet,
so to learn how the API works, you can read the source code for the test
harness, or dig through the source code itself.