BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / call / remote_command.rs

remote_command.rs

remote_command.rs 1.9 KiB Download Raw
1use gpui_kit::*;
2
3use super::join::JoinMode;
4use crate::app_state::AppState;
5
6pub const JOIN_FLAG: &str = "--join";
7pub const HANG_UP_FLAG: &str = "--hang-up";
8const JOIN_COMMAND: &str = "join ";
9const HANG_UP_COMMAND: &str = "hang-up";
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum RemoteCommand {
13 Join(String),
14 HangUp,
15}
16
17impl RemoteCommand {
18 pub fn encode(&self) -> String {
19 match self {
20 RemoteCommand::Join(link) => format!("{JOIN_COMMAND}{link}"),
21 RemoteCommand::HangUp => HANG_UP_COMMAND.to_owned(),
22 }
23 }
24
25 pub fn decode(text: &str) -> Option<RemoteCommand> {
26 if text == HANG_UP_COMMAND {
27 return Some(RemoteCommand::HangUp);
28 }
29 let link = text.strip_prefix(JOIN_COMMAND)?.trim();
30 (!link.is_empty()).then(|| RemoteCommand::Join(link.to_owned()))
31 }
32}
33
34impl AppState {
35 /// Commands a second `teams` process forwards, used by the call end-to-end test.
36 pub fn run_remote_command(&mut self, text: &str, cx: &mut Context<Self>) {
37 match RemoteCommand::decode(text) {
38 Some(RemoteCommand::Join(link)) => {
39 self.own_engine_next_call = true;
40 self.join_link(&link, JoinMode::Direct, cx);
41 self.own_engine_next_call = false;
42 }
43 Some(RemoteCommand::HangUp) => self.leave_call(cx),
44 None => {}
45 }
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::RemoteCommand;
52
53 #[test]
54 fn commands_survive_the_trip_through_the_pipe() {
55 for command in [
56 RemoteCommand::Join("https://teams.microsoft.com/l/meetup-join/x".to_owned()),
57 RemoteCommand::HangUp,
58 ] {
59 assert_eq!(RemoteCommand::decode(&command.encode()), Some(command));
60 }
61 assert_eq!(RemoteCommand::decode("join "), None);
62 assert_eq!(RemoteCommand::decode("reboot"), None);
63 }
64}