summaryrefslogtreecommitdiff
path: root/src/systems/cmd/macros.rs
blob: c7d576d472df463694523038b27db3147637c04f (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#[macro_export]
/// # JVCS_CLI Command Definition Macro
///
/// ## Import
///
/// Add the following macro to your code
///
/// ```ignore
/// crate::command_template!();
/// ```
///
/// Then paste the following content into your code
///
/// ```ignore
/// use cmd_system_macros::exec;
/// use crate::{
///     cmd_output,
///     systems::cmd::{
///         cmd_system::JVCommandContext,
///         errors::{CmdExecuteError, CmdPrepareError},
///         workspace_reader::LocalWorkspaceReader,
///     },
/// };
///
/// /// Define command type
/// /// Names should match the file name in the following format:
/// /// custom.rs matches JVCustomCommand, invoked using `jv custom <args...>`
/// /// get_data.rs matches JVGetDataCommand, invoked using `jv get data <args...>`
/// pub struct JVCustomCommand;
///
/// /// Command type, should match the definition above
/// type Cmd = JVCustomCommand;
///
/// /// Specify Argument
/// /// ```ignore
/// /// #[derive(Parser, Debug)]
/// /// pub struct JVCustomArgument;
/// /// ```
/// type Arg = JVCustomArgument;
///
/// /// Specify InputData
/// /// ```ignore
/// /// pub struct JVCustomInput;
/// /// ```
/// type In = JVCustomInput;
///
/// /// Specify CollectData
/// /// ```ignore
/// /// pub struct JVCustomCollect;
/// /// ```
/// type Collect = JVCustomCollect;
///
/// /// Return a string, rendered when the user needs help (command specifies `--help` or syntax error)
/// fn help_str() -> String {
///     todo!()
/// }
///
/// /// Preparation phase, preprocess user input and convert to a data format friendly for the execution phase
/// async fn prepare(args: &Arg, ctx: &JVCommandContext) -> Result<In, CmdPrepareError> {
///     todo!()
/// }
///
/// /// Collect necessary local information for execution
/// async fn collect(args: &Arg, ctx: &JVCommandContext) -> Result<Collect, CmdPrepareError> {
///     let reader = LocalWorkspaceReader::default();
///     todo!()
/// }
///
/// /// Execution phase, call core layer or other custom logic
/// #[exec]
/// async fn exec(
///     input: In,
///     collect: Collect,
/// ) -> Result<(Box<dyn std::any::Any + Send + 'static>, String), CmdExecuteError> {
///     todo!();
///
///     // Use the following method to return results
///     cmd_output!(output, JVCustomOutput)
/// }
/// ```
///
/// Of course, you can also use the comment-free version
///
/// ```ignore
/// use cmd_system_macros::exec;
/// use crate::{
///     cmd_output,
///     systems::cmd::{
///         cmd_system::JVCommandContext,
///         errors::{CmdExecuteError, CmdPrepareError},
///         workspace_reader::LocalWorkspaceReader,
///     },
/// };
///
/// pub struct JVCustomCommand;
/// type Cmd = JVCustomCommand;
/// type Arg = JVCustomArgument;
/// type In = JVCustomInput;
/// type Collect = JVCustomCollect;
///
/// fn help_str() -> String {
///     todo!()
/// }
///
/// async fn prepare(args: &Arg, ctx: &JVCommandContext) -> Result<In, CmdPrepareError> {
///     todo!()
/// }
///
/// async fn collect(args: &Arg, ctx: &JVCommandContext) -> Result<Collect, CmdPrepareError> {
///     let reader = LocalWorkspaceReader::default();
///     todo!()
/// }
///
/// #[exec]
/// async fn exec(
///     input: In,
///     collect: Collect,
/// ) -> Result<(Box<dyn std::any::Any + Send + 'static>, String), CmdExecuteError> {
///     todo!();
///     cmd_output!(output, JVCustomOutput)
/// }
/// ```
macro_rules! command_template {
    () => {
        impl $crate::systems::cmd::cmd_system::JVCommand<Arg, In, Collect> for Cmd {
            fn get_help_str() -> String {
                help_str()
            }

            async fn prepare(
                args: &Arg,
                ctx: &$crate::systems::cmd::cmd_system::JVCommandContext,
            ) -> Result<In, $crate::systems::cmd::errors::CmdPrepareError> {
                prepare(args, ctx).await
            }

            async fn collect(
                args: &Arg,
                ctx: &$crate::systems::cmd::cmd_system::JVCommandContext,
            ) -> Result<Collect, $crate::systems::cmd::errors::CmdPrepareError> {
                collect(args, ctx).await
            }

            async fn exec(
                input: In,
                collect: Collect,
            ) -> Result<
                (Box<dyn std::any::Any + Send + 'static>, String),
                $crate::systems::cmd::errors::CmdExecuteError,
            > {
                exec(input, collect).await
            }

            fn get_output_type_mapping() -> std::collections::HashMap<String, std::any::TypeId> {
                get_output_type_mapping()
            }
        }
    };
}

#[macro_export]
macro_rules! cmd_output {
    ($v:expr, $t:ty) => {
        Ok((Box::new($v), stringify!($t).to_string()))
    };
}