Expand description
Crabzilla provides a simple interface for running JavaScript modules alongside Rust code.
§Example
use crabzilla::*;
use std::io::stdin;
#[import_fn(name="read", scope="Stdin")]
fn read_from_stdin() -> Value {
let mut buffer = String::new();
println!("Type your name: ");
stdin().read_line(&mut buffer)?;
buffer.pop(); // Remove newline
if buffer.is_empty() {
throw!("Expected name!");
}
json!(buffer)
}
#[import_fn(name="sayHello", scope="Stdout")]
fn say_hello(args: Vec<Value>) {
if let Some(string) = args.get(0) {
if let Value::String(string) = string {
println!("Hello, {}", string);
}
}
}
#[tokio::main]
async fn main() {
let mut runtime = runtime! {
read_from_stdin,
say_hello,
};
if let Err(error) = runtime.load_module("./module.js").await {
eprintln!("{}", error);
}
}In module.js:
const user = Stdin.read();
Stdout.sayHello(user);Macros§
- json
- Construct a
serde_json::Valuefrom a JSON literal. - runtime
- Creates a runtime object and imports a list of functions.
- throw
- Throws an error with a custom message in an imported Rust function.
Structs§
- Imported
Fn - Represents an imported Rust function.
- Runtime
- Represents a JavaScript runtime instance.
Enums§
- Value
- Represents any valid JSON value.
Functions§
- create_
sync_ fn - Receives a Rust function and returns a structure that can be imported in to a runtime.
- custom_
error - Creates a new error with a caller-specified error class name and message.
Type Aliases§
- AnyError
- A generic wrapper that can encapsulate any concrete error type.
Attribute Macros§
- import_
fn - An attribute macro to convert Rust functions so they can be imported into a runtime.
The meta attributes
nameandscopecan be used to define the scoping of a particular when calling from javascript, for examplescope = "Foo", name = "bar"would assign the function as Foo.bar. Without a scope the function will be attached to the global object, and without a name it will be assigned with the Rust function name.