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
121
122
123
124
125
126
127
128
129
130
fn error_display_chain(error: &dyn std::error::Error) -> String {
let mut s = error.to_string();
if let Some(source) = error.source() {
s.push_str(" -> ");
s.push_str(&error_display_chain(source));
}
s
}
#[doc(hidden)]
pub fn run<R, E>(arg_str: &str, run_fn: R) -> u32
where
E: AsRef<dyn std::error::Error>,
R: FnOnce(&[String]) -> Result<(), E>,
{
crate::init();
let args = if arg_str.is_empty() {
vec![]
} else {
arg_str
.split(' ')
.map(|s| s.to_owned())
.collect::<Vec<String>>()
};
match run_fn(&args) {
Ok(_) => 0,
Err(err) => {
let _ = ark_api::core::return_slice(error_display_chain(err.as_ref()).as_bytes());
1
}
}
}
#[doc(hidden)]
#[cfg(feature = "with_async")]
pub fn async_run<R, F, E>(arg_str: &str, run_fn: R) -> u32
where
E: AsRef<dyn std::error::Error>,
F: std::future::Future<Output = Result<(), E>>,
R: FnOnce(Vec<String>) -> F,
{
crate::init();
let args = if arg_str.is_empty() {
vec![]
} else {
arg_str
.split(' ')
.map(|s| s.to_owned())
.collect::<Vec<String>>()
};
let mut rt = crate::Runtime::new();
let f = run_fn(args);
match rt.block_on(f) {
Ok(_) => 0,
Err(err) => {
let _ = ark_api::core::return_slice(error_display_chain(err.as_ref()).as_bytes());
1
}
}
}
#[macro_export(local_inner_macros)]
macro_rules! impl_blocking_cmdlet {
($run_fn:ident, $help_fn:ident) => {
#[no_mangle]
unsafe fn ark_cmdlet_run(arg_str_ptr: *const u8, arg_str_size: u32) -> u32 {
let arg_str = unsafe { $crate::util::param_str(arg_str_ptr, arg_str_size) };
ark_module::cmdlet::run(arg_str, $run_fn)
}
#[no_mangle]
fn ark_cmdlet_help() {
ark::core::return_slice($help_fn().as_bytes());
}
};
}
#[cfg(feature = "with_async")]
#[macro_export(local_inner_macros)]
macro_rules! impl_async_cmdlet {
($run_fn:ident, $help_fn:ident) => {
#[no_mangle]
unsafe fn ark_cmdlet_run(arg_str_ptr: *const u8, arg_str_size: u32) -> u32 {
let arg_str = unsafe { $crate::util::param_str(arg_str_ptr, arg_str_size) };
ark_module::cmdlet::async_run(arg_str, $run_fn)
}
#[no_mangle]
fn ark_cmdlet_help() {
ark::core::return_slice($help_fn().as_bytes());
}
};
}
#[macro_export(local_inner_macros)]
macro_rules! impl_wasi_cmdlet {
($help_fn:ident) => {
#[no_mangle]
fn ark_wasi_cmdlet_help() {
ark::core::return_slice($help_fn().as_bytes());
}
};
}