Write JSON metadata to file for repl.

This commit is contained in:
Stephen Chung 2022-03-26 18:19:42 +08:00
parent 77c06b94a2
commit 2b10c33b81

View File

@ -1,3 +1,4 @@
use rhai::plugin::*;
use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST, INT}; use rhai::{Dynamic, Engine, EvalAltResult, Module, Scope, AST, INT};
use rustyline::config::Builder; use rustyline::config::Builder;
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
@ -58,7 +59,7 @@ fn print_help() {
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
println!("functions => print all functions defined"); println!("functions => print all functions defined");
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
println!("json => output all functions in JSON format"); println!("json => output all functions to `metadata.json`");
println!("ast => print the last AST (optimized)"); println!("ast => print the last AST (optimized)");
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
println!("astu => print the last raw, un-optimized AST"); println!("astu => print the last raw, un-optimized AST");
@ -267,6 +268,41 @@ fn setup_editor() -> Editor<()> {
rl rl
} }
#[export_module]
mod sample_functions {
/// This is a sample function.
///
/// It takes two numbers and prints them to a string.
///
/// # Example
///
/// ```rhai
/// let result = test(42, 123);
///
/// print(result); // prints "42 123"
/// ```
pub fn test(x: INT, y: INT) -> String {
format!("{} {}", x, y)
}
/// This is a sample method for integers.
///
/// # Example
///
/// ```rhai
/// let x = 42;
///
/// x.test(123, "hello");
///
/// print(x); // prints 170
/// ```
#[rhai_fn(name = "test")]
pub fn test2(x: &mut INT, y: INT, z: &str) {
*x += y + (z.len() as INT);
println!("{} {} {}", x, y, z);
}
}
fn main() { fn main() {
let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION")); let title = format!("Rhai REPL tool (version {})", env!("CARGO_PKG_VERSION"));
println!("{}", title); println!("{}", title);
@ -296,12 +332,7 @@ fn main() {
} }
// Register sample functions // Register sample functions
engine engine.register_global_module(exported_module!(sample_functions).into());
.register_fn("test", |x: INT, y: INT| format!("{} {}", x, y))
.register_fn("test", |x: &mut INT, y: INT, z: &str| {
*x += y;
println!("{} {} {}", x, y, z);
});
// Create scope // Create scope
let mut scope = Scope::new(); let mut scope = Scope::new();
@ -463,12 +494,15 @@ fn main() {
} }
#[cfg(feature = "metadata")] #[cfg(feature = "metadata")]
"json" => { "json" => {
println!( use std::io::Write;
"{}",
engine let json = engine
.gen_fn_metadata_with_ast_to_json(&main_ast, true) .gen_fn_metadata_with_ast_to_json(&main_ast, false)
.unwrap() .unwrap();
); let mut f = std::fs::File::create("metadata.json")
.expect("Unable to create `metadata.json`");
f.write_all(json.as_bytes()).expect("Unable to write data");
println!("Functions metadata written to `metadata.json`.");
continue; continue;
} }
"!!" => { "!!" => {