Skip to content

Commit 1e4258f

Browse files
committed
Add run_function_alias()
1 parent d6ae499 commit 1e4258f

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

examples/xbd-net/src/xbd/shell.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -145,30 +145,39 @@ const ARRAY_ALIAS_NAMED: &[(&str, &str)] = &[
145145
("h", "help"),
146146
];
147147

148-
const ARRAY_ALIAS_FUNCTION: &[(&str, fn())] = &[
149-
("f0", || println!("hello world!")),
150-
//---- kludge
151-
("f1", || println!("async fn test_async_sleep()")),
152-
("f2", || println!("async fn test_async_blockwise_get()")),
153-
("f3", || println!("async fn crate::test_blockwise()")),
154-
//----
155-
];
156-
157148
const ARRAY_ALIAS_ENUMERATED: &[&str] = &[
158149
"version",
159150
"ifconfig",
160151
"gcoap get [::1] /riot/board",
161152
"gcoap get [::1] /const/song.txt",
162153
];
163154

155+
const ARRAY_ALIAS_FUNCTION: &[&str] = &[
156+
"f0",
157+
"f1",
158+
"f2",
159+
"f3",
160+
//"f99", // test undefined
161+
];
162+
163+
async fn run_function_alias(name: &str) {
164+
match name {
165+
"f0" => (|| println!("hello world!"))(),
166+
"f1" => test_async_sleep().await,
167+
"f2" => test_async_blockwise_get().await,
168+
"f3" => crate::test_blockwise("[::1]:5683").await.unwrap(),
169+
_ => println!("function alias [{}] is not defined!", name),
170+
}
171+
}
172+
164173
fn print_aliases() {
165174
println!("---- named alias ----");
166175
ARRAY_ALIAS_NAMED.iter()
167176
.for_each(|(name, cmd)| println!("[{}] {}", name, cmd));
168177

169178
println!("---- function alias ----");
170179
ARRAY_ALIAS_FUNCTION.iter()
171-
.for_each(|(name, f)| println!("[{}] {:?}", name, f));
180+
.for_each(|name| println!("[{}] <function>", name));
172181

173182
println!("---- enumerated alias ----");
174183
ARRAY_ALIAS_ENUMERATED.iter().enumerate()
@@ -193,16 +202,8 @@ async fn match_alias(line: &mut ShellBuf) -> bool {
193202
return expand(line, "");
194203
} else if let Some(item) = ARRAY_ALIAS_NAMED.iter().find(|item| item.0 == ln) {
195204
return expand(line, item.1);
196-
} else if let Some(item) = ARRAY_ALIAS_FUNCTION.iter().find(|item| item.0 == ln) {
197-
match item.0 {
198-
//---- kludge
199-
"f1" => test_async_sleep().await,
200-
"f2" => test_async_blockwise_get().await,
201-
"f3" => crate::test_blockwise("[::1]:5683").await.unwrap(),
202-
//----
203-
_ => item.1(),
204-
}
205-
205+
} else if let Some(item) = ARRAY_ALIAS_FUNCTION.iter().find(|item| **item == ln) {
206+
run_function_alias(item).await;
206207
return expand(line, "");
207208
} else if let Ok(x) = ln.parse::<usize>() {
208209
if x < ARRAY_ALIAS_ENUMERATED.len() {

0 commit comments

Comments
 (0)