-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
69 lines (65 loc) · 2.12 KB
/
lib.rs
File metadata and controls
69 lines (65 loc) · 2.12 KB
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
mod bindings {
wit_bindgen::generate!({
path: "../../assets/recorder.wit",
world: "guest",
});
}
use trace::Logger;
struct Component;
impl bindings::exports::proxy::recorder::record::Guest for Component {
fn record_args(method: Option<String>, args: Vec<String>, is_export: bool) {
let mut logger = Logger::new();
let call = logger.record_args(method, args, is_export);
let json = serde_json::to_string(&call).unwrap();
println!("{json}");
}
fn record_ret(method: Option<String>, ret: Option<String>, is_export: bool) {
let mut logger = Logger::new();
let call = logger.record_ret(method, ret, is_export);
let json = serde_json::to_string(&call).unwrap();
println!("{json}");
}
}
use std::cell::RefCell;
thread_local! {
static TRACE: RefCell<Option<Logger>> = RefCell::new(None);
}
fn load_trace() {
let load = TRACE.with_borrow(|v| v.is_none());
if load {
TRACE.with_borrow_mut(|v| {
use std::io::Read;
let mut input = String::new();
std::io::stdin().read_to_string(&mut input).unwrap();
let mut logger = Logger::new();
logger.load_trace(&input);
*v = Some(logger);
});
}
}
impl bindings::exports::proxy::recorder::replay::Guest for Component {
fn replay_export() -> Option<(String, Vec<String>)> {
load_trace();
TRACE.with_borrow_mut(|v| v.as_mut().unwrap().replay_export())
}
fn assert_export_ret(assert_method: Option<String>, assert_ret: Option<String>) {
TRACE.with_borrow_mut(|v| {
v.as_mut()
.unwrap()
.assert_export_ret(assert_method, assert_ret)
});
}
fn replay_import(
assert_method: Option<String>,
assert_args: Option<Vec<String>>,
) -> Option<String> {
TRACE.with_borrow_mut(|v| {
let (_, ret) = v
.as_mut()
.unwrap()
.replay_import(assert_method, assert_args, true);
ret
})
}
}
bindings::export!(Component with_types_in bindings);