-
Notifications
You must be signed in to change notification settings - Fork 168
/
custom_types_and_methods.rs
114 lines (97 loc) · 3.08 KB
/
custom_types_and_methods.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! An example showing how to register a Rust type and methods/getters/setters for it.
#[cfg(feature = "no_object")]
fn main() {
panic!("This example does not run under 'no_object'.");
}
use rhai::{CustomType, Engine, EvalAltResult, TypeBuilder};
#[cfg(not(feature = "no_object"))]
fn main() -> Result<(), Box<EvalAltResult>> {
/// This is a test structure. If the metadata feature
/// is enabled, this comment will be exported.
#[derive(Debug, Clone, CustomType)]
#[rhai_type(extra = Self::build_extra)]
struct TestStruct {
/// A number.
///
/// ```js
/// let t = new_ts();
/// print(t.x); // Get the value of x.
/// t.x = 42; // Set the value of x.
/// ```
x: i64,
}
impl TestStruct {
pub fn new() -> Self {
Self { x: 1 }
}
pub fn update(&mut self) {
self.x += 1000;
}
pub fn calculate(&mut self, data: i64) -> i64 {
self.x * data
}
fn build_extra(builder: &mut TypeBuilder<Self>) {
builder
.with_fn("new_ts", TestStruct::new)
.with_fn("update", TestStruct::update)
.with_fn("calc", TestStruct::calculate);
}
}
let mut engine = Engine::new();
engine.build_type::<TestStruct>();
#[cfg(feature = "metadata")]
{
println!("Functions registered:");
engine
.gen_fn_signatures(false)
.into_iter()
.for_each(|func| println!("{func}"));
println!();
let docs: serde_json::Value =
serde_json::from_str(&engine.gen_fn_metadata_to_json(false).unwrap()).unwrap();
// compare comments from the type.
assert_eq!(
docs["customTypes"][0]["docComments"],
serde_json::json!([
"/// This is a test structure. If the metadata feature",
"/// is enabled, this comment will be exported."
])
);
// compare comments from the getter.
assert_eq!(
docs["functions"][1]["docComments"],
serde_json::json!([
"/// A number.",
"///",
"/// ```js",
"/// let t = new_ts();",
"/// print(t.x); // Get the value of x.",
"/// t.x = 42; // Set the value of x.",
"/// ```"
])
);
// compare comments from the setter.
assert_eq!(
docs["functions"][3]["docComments"],
serde_json::json!([
"/// A number.",
"///",
"/// ```js",
"/// let t = new_ts();",
"/// print(t.x); // Get the value of x.",
"/// t.x = 42; // Set the value of x.",
"/// ```"
])
);
}
let result = engine.eval::<i64>(
"
let x = new_ts();
x.x = 42;
x.update();
x.calc(x.x)
",
)?;
println!("result: {result}"); // prints 1085764
Ok(())
}