3 releases
0.1.2 | Dec 23, 2021 |
---|---|
0.1.1 | Feb 28, 2021 |
0.1.0 | Feb 17, 2021 |
#205 in #scripting
10KB
142 lines
Crabzilla
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(Value::String(string)) = args.get(0) {
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);
lib.rs
:
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);
Dependencies
~73MB
~1.5M SLoC