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