generated from emilk/eframe_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
120 lines (91 loc) · 2.73 KB
/
lib.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
115
116
117
118
119
120
#[allow(warnings)]
#[cfg_attr(rustfmt, rustfmt_skip)]
mod bindings;
use std::sync::{LazyLock, Mutex};
use bindings::component::plugin::host::emit;
use bindings::component::plugin::types::Event;
use bindings::exports::component::plugin::run::Guest;
static COUNT: LazyLock<Mutex<i32>> = LazyLock::new(|| Mutex::new(0i32));
// static COUNTER: LazyLock<Counter> = LazyLock::new(Counter::new);
bindings::export!(Counter with_types_in bindings);
struct Counter;
// struct Counter {
// count: i32,
// }
// impl Counter {
// fn new() -> Self {
// Self { count: 0 }
// }
// }
impl Guest for Counter {
/// Say hello!
fn load() -> String {
include_str!(concat!(env!("OUT_DIR"), "/counter.rhai")).to_string()
}
/// Increment the count
fn increment_count() -> i32 {
// let mut count = COUNTER.count;
// count += 1;
//
//
// count
let mut count = COUNT.lock().unwrap();
*count += 1;
emit(&Event {
name: "count".to_string(),
value: (count).to_string(),
});
*count
}
/// Decrement the count
fn decrement() -> i32 {
// let mut count = COUNTER.count;
// count -= 1;
// count
let mut count = COUNT.lock().unwrap();
*count -= 1;
emit(&Event {
name: "count".to_string(),
value: (count).to_string(),
});
*count
}
fn current() -> i32 {
// COUNTER.count
*COUNT.lock().unwrap()
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_string_interpolation() {
let def_count = "a {{test}} string";
let test = format!(
r#"
// call the system function `render` on the template with the ctx from scope
// wasm functions are bound to the rhai script on load?
// let count = current(); // TODO: register all exported functions with rhai engine
// let count = 0;
if !is_def_var("count") || count == "0" {{
render(`{def_count}`)
}} else {{
render(`
<div class="flex flex-row">
<button data-on-click="increment()">Increment</button>
<button data-on-click="decrement()">Decrement</button>
<!-- inline template vars need double double {{}}'s -->
<span>Count is: {{{{count}}}}</span>
</div>
`)
}}
"#
);
eprintln!("{}", test);
}
// print out include_str!(concat!(env!("OUT_DIR"), "/counter.rhai"))
#[test]
fn test_include_str() {
let rhai = include_str!(concat!(env!("OUT_DIR"), "/counter.rhai"));
eprintln!("{}", rhai);
}
}