src/commands.rs
318 lines · 11374 bytes
1use modalkit::{
2 editing::base::OpenTarget,
3 env::vim::command::{CommandContext, CommandDescription},
4 input::commands::{CommandError, CommandResult, CommandStep},
5 input::InputContext,
6};
7
8use crate::base::{
9 IambAction,
10 IambId,
11 MessageAction,
12 ProgramCommand,
13 ProgramCommands,
14 ProgramContext,
15 RoomAction,
16 SendAction,
17 SetRoomField,
18 VerifyAction,
19};
20
21type ProgContext = CommandContext<ProgramContext>;
22type ProgResult = CommandResult<ProgramCommand>;
23
24fn iamb_verify(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
25 let mut args = desc.arg.strings()?;
26
27 match args.len() {
28 0 => {
29 let open = ctx.switch(OpenTarget::Application(IambId::VerifyList));
30 let step = CommandStep::Continue(open, ctx.context.take());
31
32 return Ok(step);
33 },
34 1 => {
35 return Result::Err(CommandError::InvalidArgument);
36 },
37 2 => {
38 let act = match args[0].as_str() {
39 "accept" => VerifyAction::Accept,
40 "cancel" => VerifyAction::Cancel,
41 "confirm" => VerifyAction::Confirm,
42 "mismatch" => VerifyAction::Mismatch,
43 "request" => {
44 let iact = IambAction::VerifyRequest(args.remove(1));
45 let step = CommandStep::Continue(iact.into(), ctx.context.take());
46
47 return Ok(step);
48 },
49 _ => return Result::Err(CommandError::InvalidArgument),
50 };
51
52 let vact = IambAction::Verify(act, args.remove(1));
53 let step = CommandStep::Continue(vact.into(), ctx.context.take());
54
55 return Ok(step);
56 },
57 _ => {
58 return Result::Err(CommandError::InvalidArgument);
59 },
60 }
61}
62
63fn iamb_dms(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
64 if !desc.arg.text.is_empty() {
65 return Result::Err(CommandError::InvalidArgument);
66 }
67
68 let open = ctx.switch(OpenTarget::Application(IambId::DirectList));
69 let step = CommandStep::Continue(open, ctx.context.take());
70
71 return Ok(step);
72}
73
74fn iamb_members(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
75 if !desc.arg.text.is_empty() {
76 return Result::Err(CommandError::InvalidArgument);
77 }
78
79 let open = IambAction::Room(RoomAction::Members(ctx.clone().into()));
80 let step = CommandStep::Continue(open.into(), ctx.context.take());
81
82 return Ok(step);
83}
84
85fn iamb_rooms(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
86 if !desc.arg.text.is_empty() {
87 return Result::Err(CommandError::InvalidArgument);
88 }
89
90 let open = ctx.switch(OpenTarget::Application(IambId::RoomList));
91 let step = CommandStep::Continue(open, ctx.context.take());
92
93 return Ok(step);
94}
95
96fn iamb_spaces(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
97 if !desc.arg.text.is_empty() {
98 return Result::Err(CommandError::InvalidArgument);
99 }
100
101 let open = ctx.switch(OpenTarget::Application(IambId::SpaceList));
102 let step = CommandStep::Continue(open, ctx.context.take());
103
104 return Ok(step);
105}
106
107fn iamb_welcome(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
108 if !desc.arg.text.is_empty() {
109 return Result::Err(CommandError::InvalidArgument);
110 }
111
112 let open = ctx.switch(OpenTarget::Application(IambId::Welcome));
113 let step = CommandStep::Continue(open, ctx.context.take());
114
115 return Ok(step);
116}
117
118fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
119 let mut args = desc.arg.filenames()?;
120
121 if args.len() != 1 {
122 return Result::Err(CommandError::InvalidArgument);
123 }
124
125 let open = ctx.switch(args.remove(0));
126 let step = CommandStep::Continue(open, ctx.context.take());
127
128 return Ok(step);
129}
130
131fn iamb_set(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
132 let mut args = desc.arg.strings()?;
133
134 if args.len() != 2 {
135 return Result::Err(CommandError::InvalidArgument);
136 }
137
138 let field = args.remove(0);
139 let value = args.remove(0);
140
141 let act: IambAction = match field.as_str() {
142 "room.name" => RoomAction::Set(SetRoomField::Name(value)).into(),
143 "room.topic" => RoomAction::Set(SetRoomField::Topic(value)).into(),
144 _ => {
145 return Result::Err(CommandError::InvalidArgument);
146 },
147 };
148
149 let step = CommandStep::Continue(act.into(), ctx.context.take());
150
151 return Ok(step);
152}
153
154fn iamb_upload(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
155 let mut args = desc.arg.strings()?;
156
157 if args.len() != 1 {
158 return Result::Err(CommandError::InvalidArgument);
159 }
160
161 let sact = SendAction::Upload(args.remove(0));
162 let iact = IambAction::from(sact);
163 let step = CommandStep::Continue(iact.into(), ctx.context.take());
164
165 return Ok(step);
166}
167
168fn iamb_download(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
169 let mut args = desc.arg.strings()?;
170
171 if args.len() > 1 {
172 return Result::Err(CommandError::InvalidArgument);
173 }
174
175 let mact = MessageAction::Download(args.pop(), desc.bang);
176 let iact = IambAction::from(mact);
177 let step = CommandStep::Continue(iact.into(), ctx.context.take());
178
179 return Ok(step);
180}
181
182fn add_iamb_commands(cmds: &mut ProgramCommands) {
183 cmds.add_command(ProgramCommand { names: vec!["dms".into()], f: iamb_dms });
184 cmds.add_command(ProgramCommand { names: vec!["download".into()], f: iamb_download });
185 cmds.add_command(ProgramCommand { names: vec!["join".into()], f: iamb_join });
186 cmds.add_command(ProgramCommand { names: vec!["members".into()], f: iamb_members });
187 cmds.add_command(ProgramCommand { names: vec!["rooms".into()], f: iamb_rooms });
188 cmds.add_command(ProgramCommand { names: vec!["set".into()], f: iamb_set });
189 cmds.add_command(ProgramCommand { names: vec!["spaces".into()], f: iamb_spaces });
190 cmds.add_command(ProgramCommand { names: vec!["upload".into()], f: iamb_upload });
191 cmds.add_command(ProgramCommand { names: vec!["verify".into()], f: iamb_verify });
192 cmds.add_command(ProgramCommand { names: vec!["welcome".into()], f: iamb_welcome });
193}
194
195pub fn setup_commands() -> ProgramCommands {
196 let mut cmds = ProgramCommands::default();
197
198 add_iamb_commands(&mut cmds);
199
200 return cmds;
201}
202
203#[cfg(test)]
204mod tests {
205 use super::*;
206 use modalkit::editing::action::WindowAction;
207
208 #[test]
209 fn test_cmd_verify() {
210 let mut cmds = setup_commands();
211 let ctx = ProgramContext::default();
212
213 let res = cmds.input_cmd(":verify", ctx.clone()).unwrap();
214 let act = WindowAction::Switch(OpenTarget::Application(IambId::VerifyList));
215 assert_eq!(res, vec![(act.into(), ctx.clone())]);
216
217 let res = cmds.input_cmd(":verify request @user1:example.com", ctx.clone()).unwrap();
218 let act = IambAction::VerifyRequest("@user1:example.com".into());
219 assert_eq!(res, vec![(act.into(), ctx.clone())]);
220
221 let res = cmds
222 .input_cmd(":verify accept @user1:example.com/FOOBAR", ctx.clone())
223 .unwrap();
224 let act = IambAction::Verify(VerifyAction::Accept, "@user1:example.com/FOOBAR".into());
225 assert_eq!(res, vec![(act.into(), ctx.clone())]);
226
227 let res = cmds
228 .input_cmd(":verify mismatch @user2:example.com/QUUXBAZ", ctx.clone())
229 .unwrap();
230 let act = IambAction::Verify(VerifyAction::Mismatch, "@user2:example.com/QUUXBAZ".into());
231 assert_eq!(res, vec![(act.into(), ctx.clone())]);
232
233 let res = cmds
234 .input_cmd(":verify cancel @user3:example.com/MYDEVICE", ctx.clone())
235 .unwrap();
236 let act = IambAction::Verify(VerifyAction::Cancel, "@user3:example.com/MYDEVICE".into());
237 assert_eq!(res, vec![(act.into(), ctx.clone())]);
238
239 let res = cmds
240 .input_cmd(":verify confirm @user4:example.com/GOODDEV", ctx.clone())
241 .unwrap();
242 let act = IambAction::Verify(VerifyAction::Confirm, "@user4:example.com/GOODDEV".into());
243 assert_eq!(res, vec![(act.into(), ctx.clone())]);
244
245 let res = cmds.input_cmd(":verify confirm", ctx.clone());
246 assert_eq!(res, Err(CommandError::InvalidArgument));
247
248 let res = cmds.input_cmd(":verify cancel @user4:example.com MYDEVICE", ctx.clone());
249 assert_eq!(res, Err(CommandError::InvalidArgument));
250
251 let res = cmds.input_cmd(":verify mismatch a b c d e f", ctx.clone());
252 assert_eq!(res, Err(CommandError::InvalidArgument));
253 }
254
255 #[test]
256 fn test_cmd_join() {
257 let mut cmds = setup_commands();
258 let ctx = ProgramContext::default();
259
260 let res = cmds.input_cmd("join #foobar:example.com", ctx.clone()).unwrap();
261 let act = WindowAction::Switch(OpenTarget::Name("#foobar:example.com".into()));
262 assert_eq!(res, vec![(act.into(), ctx.clone())]);
263
264 let res = cmds.input_cmd("join #", ctx.clone()).unwrap();
265 let act = WindowAction::Switch(OpenTarget::Alternate);
266 assert_eq!(res, vec![(act.into(), ctx.clone())]);
267
268 let res = cmds.input_cmd("join", ctx.clone());
269 assert_eq!(res, Err(CommandError::InvalidArgument));
270
271 let res = cmds.input_cmd("join foo bar", ctx.clone());
272 assert_eq!(res, Err(CommandError::InvalidArgument));
273 }
274
275 #[test]
276 fn test_cmd_set() {
277 let mut cmds = setup_commands();
278 let ctx = ProgramContext::default();
279
280 let res = cmds
281 .input_cmd("set room.topic \"Lots of fun discussion!\"", ctx.clone())
282 .unwrap();
283 let act = IambAction::Room(SetRoomField::Topic("Lots of fun discussion!".into()).into());
284 assert_eq!(res, vec![(act.into(), ctx.clone())]);
285
286 let res = cmds
287 .input_cmd("set room.topic The\\ Discussion\\ Room", ctx.clone())
288 .unwrap();
289 let act = IambAction::Room(SetRoomField::Topic("The Discussion Room".into()).into());
290 assert_eq!(res, vec![(act.into(), ctx.clone())]);
291
292 let res = cmds.input_cmd("set room.topic Development", ctx.clone()).unwrap();
293 let act = IambAction::Room(SetRoomField::Topic("Development".into()).into());
294 assert_eq!(res, vec![(act.into(), ctx.clone())]);
295
296 let res = cmds.input_cmd("set room.name Development", ctx.clone()).unwrap();
297 let act = IambAction::Room(SetRoomField::Name("Development".into()).into());
298 assert_eq!(res, vec![(act.into(), ctx.clone())]);
299
300 let res = cmds
301 .input_cmd("set room.name \"Application Development\"", ctx.clone())
302 .unwrap();
303 let act = IambAction::Room(SetRoomField::Name("Application Development".into()).into());
304 assert_eq!(res, vec![(act.into(), ctx.clone())]);
305
306 let res = cmds.input_cmd("set", ctx.clone());
307 assert_eq!(res, Err(CommandError::InvalidArgument));
308
309 let res = cmds.input_cmd("set room.name", ctx.clone());
310 assert_eq!(res, Err(CommandError::InvalidArgument));
311
312 let res = cmds.input_cmd("set room.topic", ctx.clone());
313 assert_eq!(res, Err(CommandError::InvalidArgument));
314
315 let res = cmds.input_cmd("set room.topic A B C", ctx.clone());
316 assert_eq!(res, Err(CommandError::InvalidArgument));
317 }
318}