@@ -9,6 +9,31 @@ use crate::builtins;
9
9
use crate :: commands:: { self , CommandArg } ;
10
10
use crate :: error;
11
11
12
+ /// A simple command that can be registered as a built-in.
13
+ pub trait SimpleCommand {
14
+ /// Returns the content of the built-in command.
15
+ fn get_content ( name : & str , content_type : builtins:: ContentType )
16
+ -> Result < String , error:: Error > ;
17
+
18
+ /// Executes the built-in command.
19
+ fn execute (
20
+ context : commands:: ExecutionContext < ' _ > ,
21
+ args : & [ & str ] ,
22
+ ) -> Result < builtins:: BuiltinResult , error:: Error > ;
23
+ }
24
+
25
+ /// Returns a built-in command registration, given an implementation of the
26
+ /// `SimpleCommand` trait.
27
+ pub fn simple_builtin < B : SimpleCommand + Send + Sync > ( ) -> builtins:: Registration {
28
+ builtins:: Registration {
29
+ execute_func : exec_simple_builtin :: < B > ,
30
+ content_func : B :: get_content,
31
+ disabled : false ,
32
+ special_builtin : false ,
33
+ declaration_builtin : false ,
34
+ }
35
+ }
36
+
12
37
/// Returns a built-in command registration, given an implementation of the
13
38
/// `Command` trait.
14
39
pub fn builtin < B : builtins:: Command + Send + Sync > ( ) -> builtins:: Registration {
@@ -58,6 +83,31 @@ fn get_builtin_content<T: builtins::Command + Send + Sync>(
58
83
T :: get_content ( name, content_type)
59
84
}
60
85
86
+ fn exec_simple_builtin < T : SimpleCommand + Send + Sync > (
87
+ context : commands:: ExecutionContext < ' _ > ,
88
+ args : Vec < CommandArg > ,
89
+ ) -> BoxFuture < ' _ , Result < builtins:: BuiltinResult , error:: Error > > {
90
+ Box :: pin ( async move { exec_simple_builtin_impl :: < T > ( context, args) . await } )
91
+ }
92
+
93
+ #[ allow( clippy:: unused_async) ]
94
+ async fn exec_simple_builtin_impl < T : SimpleCommand + Send + Sync > (
95
+ context : commands:: ExecutionContext < ' _ > ,
96
+ args : Vec < CommandArg > ,
97
+ ) -> Result < builtins:: BuiltinResult , error:: Error > {
98
+ let plain_args: Vec < _ > = args
99
+ . into_iter ( )
100
+ . map ( |arg| match arg {
101
+ CommandArg :: String ( s) => s,
102
+ CommandArg :: Assignment ( a) => a. to_string ( ) ,
103
+ } )
104
+ . collect ( ) ;
105
+
106
+ let plain_args: Vec < _ > = plain_args. iter ( ) . map ( AsRef :: as_ref) . collect ( ) ;
107
+
108
+ T :: execute ( context, plain_args. as_slice ( ) )
109
+ }
110
+
61
111
fn exec_builtin < T : builtins:: Command + Send + Sync > (
62
112
context : commands:: ExecutionContext < ' _ > ,
63
113
args : Vec < CommandArg > ,
0 commit comments