example_communication_common/
communication.rs1use std::cmp::PartialEq;
2use std::fmt;
3use std::fmt::{Display, Formatter};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub enum ConnectionType {
8 Client,
9 Controller
10}
11
12impl PartialEq<ConnectionType> for &ConnectionType {
13 fn eq(&self, other: &ConnectionType) -> bool {
14 match (self,other) {
15 (&ConnectionType::Client, &ConnectionType::Client) => true,
16 (&ConnectionType::Controller, &ConnectionType::Controller) => true,
17 _ => false
18 }
19 }
20}
21
22impl ConnectionType {
23 pub fn as_str(&self) -> &str {
24 match self {
25 ConnectionType::Client => {"Client"}
26 ConnectionType::Controller => {"Controller"}
27 }
28 }
29}
30impl Display for ConnectionType {
31 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
32 write!(f, "{}", self.as_str())
33 }
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct ConnectionInfo {
38 pub uuid: String,
39 pub name: String,
40 pub connection_type: ConnectionType,
41}
42
43impl Display for ConnectionInfo {
44 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
45 write!(f, "(uuid: {} name: {} connection type: {})", self.uuid, self.name, self.connection_type)
46 }
47}
48
49#[derive(Serialize, Deserialize, Clone)]
50pub enum ControlTypes {
51 Message
52}
53
54pub struct ControlOption {
55 pub display_name: String,
56 pub name: String,
57 pub default_value: String,
58}
59
60pub struct ControlDefinition {
61 pub display_name: String,
62 pub name: String,
63 pub options: Vec<ControlOption>
64}
65
66impl ControlTypes {
67 pub fn as_str(&self) -> String {
68 match self {
69 ControlTypes::Message => {"Message".to_string()}
70 }
71 }
72 pub fn to_definition(&self) -> ControlDefinition {
73 match self {
74 ControlTypes::Message => {
75 ControlDefinition {
76 display_name: "Send Message".to_string(),
77 name: self.as_str(),
78 options: vec![ControlOption {
79 display_name: "Text".to_string(),
80 name: "Text".to_string(),
81 default_value: "".to_string(),
82 }],
83 }
84 }
85 }
86 }
87}
88
89#[derive(Serialize, Deserialize, Clone)]
90pub enum ControlMessage {
91 Message {
92 text: String
93 }
94}
95
96
97
98#[derive(Serialize, Deserialize, Clone)]
99pub enum CommandType {
100 Welcome {uuid: String},
102 ActiveConnections { users: Vec<ConnectionInfo> },
103 UpdateConnection { connection_info: ConnectionInfo },
104 NotifyDisconnect { uuid: String },
105 GetConnections {reply_uuid: String},
107 SetConnectionInfo { info: ConnectionInfo },
108 Disconnect,
109 Control {
111 message_type: ControlMessage,
112 },
113 RequestCapabilities {
114 reply_uuid: String
115 },
116 ProvideCapabilities {
117 sender_uuid: String,
118 list: Vec<ControlTypes>
119 },
120 Ack
121}
122
123#[derive(Serialize, Deserialize, Clone)]
124pub enum Destination {
125 Single { destination_uuid: String},
126 Multi { destination_uuids: Vec<String>},
127 Type { destination_type: ConnectionType },
128 All,
129 None
130}
131
132
133impl Destination {
134 pub fn matches_destination(&self, connection_info: &ConnectionInfo) -> bool {
135 match self {
136 Destination::Single { destination_uuid } => { *destination_uuid == connection_info.uuid }
137 Destination::Multi { destination_uuids } => { destination_uuids.contains(&connection_info.uuid) }
138 Destination::Type { destination_type } => { destination_type == connection_info.connection_type }
139 Destination::All => { true }
140 Destination::None => { false }
141 }
142 }
143
144 pub fn matches_uuid(&self, uuid: &String) -> bool {
145 match self {
146 Destination::Single { destination_uuid } => { *destination_uuid == *uuid }
147 Destination::Multi { destination_uuids } => { destination_uuids.contains(uuid) }
148 Destination::Type { .. } => { false }
149 Destination::All => { true }
150 Destination::None => { false }
151 }
152
153 }
154}
155
156#[derive(Serialize, Deserialize, Clone)]
157pub struct WebSocketMessage {
158 pub command: CommandType,
159 pub destination: Destination
160}