Skip to content

Commit

Permalink
Fix build for newer versions of Rhai.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Oct 25, 2023
1 parent 5c4d5fd commit 98845b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rhai-ml"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["Chris McComb <[email protected]>"]
description = "Machine learning in the Rhai scripting language"
Expand All @@ -20,16 +20,16 @@ metadata = ["rhai/metadata"]
[dependencies]
rhai = ">=1.0.0"
smartstring = ">=1.0.0"
smartcorelib = {version = ">=0.3.0", package = "smartcore", features=["serde"]}
bincode = { version = ">=1.0.0"}
smartcorelib = { version = ">=0.3.0", package = "smartcore", features = ["serde"] }
bincode = { version = ">=1.0.0" }

[build-dependencies]
rhai = ">=1.0.0"
serde_json = ">=1.0.0"
serde = ">=1.0.0"
smartstring = ">=1.0.0"
smartcorelib = {version = ">=0.3.0", package = "smartcore", features=["serde"]}
bincode = { version = ">=1.0.0"}
smartcorelib = { version = ">=0.3.0", package = "smartcore", features = ["serde"] }
bincode = { version = ">=1.0.0" }

[package.metadata.docs.rs]
all-features = true
21 changes: 16 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ fn main() {
use std::collections::HashMap;
use std::io::Write;

#[derive(Serialize, Deserialize, Debug, Clone)]
struct Metadata {
#[serde(default)]
pub functions: Vec<Function>,
}

#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Function {
Expand All @@ -40,25 +46,30 @@ fn main() {
std::fs::File::create(std::env::var("OUT_DIR").unwrap() + "/rhai-ml-docs.md").unwrap();

// Make a file for tests
let mut test_file = std::fs::File::create(std::env::var("OUT_DIR").unwrap() + "/rhai-ml-tests.rs").unwrap();
let mut test_file =
std::fs::File::create(std::env::var("OUT_DIR").unwrap() + "/rhai-ml-tests.rs").unwrap();

// Build an engine for doctests
let mut engine = Engine::new();

// Add custom functions from Rust
let mut lib = Module::new();
combine_with_exported_module!(&mut lib, "rhai_ml_train_and_predict", train_and_predict_functions);
combine_with_exported_module!(
&mut lib,
"rhai_ml_train_and_predict",
train_and_predict_functions
);
engine.register_global_module(rhai::Shared::new(lib));

// Extract metadata
let json_fns = engine.gen_fn_metadata_to_json(false).unwrap();
println!("{json_fns}");
let v: HashMap<String, Vec<Function>> = serde_json::from_str(&json_fns).unwrap();
for function in v["functions"].clone() {
let v: Metadata = serde_json::from_str(&json_fns).unwrap();
for function in &v.functions {
println!("{:?}", function);
}

let function_list = v["functions"].clone();
let function_list = v.functions;

// Write functions
write!(doc_file, "\n# API\n This package provides a large variety of functions to help with machine learning and artificial intelligence:\n").expect("Cannot write to {doc_file}");
Expand Down

0 comments on commit 98845b5

Please sign in to comment.