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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use getset::{CopyGetters, Getters};

#[derive(CopyGetters, Getters, Default, Debug, Clone)]
pub struct EngineOptions {
  #[getset(get = "pub")]
  device_config_json: Option<String>,
  #[getset(get = "pub")]
  user_device_config_json: Option<String>,
  #[getset(get = "pub")]
  server_name: String,
  #[getset(get_copy = "pub")]
  websocket_use_all_interfaces: bool,
  #[getset(get_copy = "pub")]
  websocket_port: Option<u16>,
  #[getset(get = "pub")]
  websocket_client_address: Option<String>,
  #[getset(get_copy = "pub")]
  frontend_websocket_port: Option<u16>,
  #[getset(get_copy = "pub")]
  frontend_in_process_channel: bool,
  #[getset(get_copy = "pub")]
  max_ping_time: u32,
  #[getset(get_copy = "pub")]
  allow_raw_messages: bool,
  #[getset(get_copy = "pub")]
  use_bluetooth_le: bool,
  #[getset(get_copy = "pub")]
  use_serial_port: bool,
  #[getset(get_copy = "pub")]
  use_hid: bool,
  #[getset(get_copy = "pub")]
  use_lovense_dongle_serial: bool,
  #[getset(get_copy = "pub")]
  use_lovense_dongle_hid: bool,
  #[getset(get_copy = "pub")]
  use_xinput: bool,
  #[getset(get_copy = "pub")]
  use_lovense_connect: bool,
  #[getset(get_copy = "pub")]
  use_device_websocket_server: bool,
  #[getset(get_copy = "pub")]
  device_websocket_server_port: Option<u16>,
  #[getset(get_copy = "pub")]
  crash_main_thread: bool,
  #[getset(get_copy = "pub")]
  crash_task_thread: bool,
  #[getset(get_copy = "pub")]
  broadcast_server_mdns: bool,
  #[getset(get = "pub")]
  mdns_suffix: Option<String>,
  #[getset(get_copy = "pub")]
  repeater_mode: bool,
  #[getset(get_copy = "pub")]
  repeater_local_port: Option<u16>,
  #[getset(get = "pub")]
  repeater_remote_address: Option<String>,
}

#[derive(Default, Debug, Clone)]
pub struct EngineOptionsExternal {
  pub device_config_json: Option<String>,
  pub user_device_config_json: Option<String>,
  pub server_name: String,
  pub websocket_use_all_interfaces: bool,
  pub websocket_port: Option<u16>,
  pub websocket_client_address: Option<String>,
  pub frontend_websocket_port: Option<u16>,
  pub frontend_in_process_channel: bool,
  pub max_ping_time: u32,
  pub allow_raw_messages: bool,
  pub use_bluetooth_le: bool,
  pub use_serial_port: bool,
  pub use_hid: bool,
  pub use_lovense_dongle_serial: bool,
  pub use_lovense_dongle_hid: bool,
  pub use_xinput: bool,
  pub use_lovense_connect: bool,
  pub use_device_websocket_server: bool,
  pub device_websocket_server_port: Option<u16>,
  pub crash_main_thread: bool,
  pub crash_task_thread: bool,
  pub broadcast_server_mdns: bool,
  pub mdns_suffix: Option<String>,
  pub repeater_mode: bool,
  pub repeater_local_port: Option<u16>,
  pub repeater_remote_address: Option<String>,
}

impl From<EngineOptionsExternal> for EngineOptions {
  fn from(other: EngineOptionsExternal) -> Self {
    Self {
      device_config_json: other.device_config_json,
      user_device_config_json: other.user_device_config_json,
      server_name: other.server_name,
      websocket_use_all_interfaces: other.websocket_use_all_interfaces,
      websocket_port: other.websocket_port,
      websocket_client_address: other.websocket_client_address,
      frontend_websocket_port: other.frontend_websocket_port,
      frontend_in_process_channel: other.frontend_in_process_channel,
      max_ping_time: other.max_ping_time,
      allow_raw_messages: other.allow_raw_messages,
      use_bluetooth_le: other.use_bluetooth_le,
      use_serial_port: other.use_serial_port,
      use_hid: other.use_hid,
      use_lovense_dongle_serial: other.use_lovense_dongle_serial,
      use_lovense_dongle_hid: other.use_lovense_dongle_hid,
      use_xinput: other.use_xinput,
      use_lovense_connect: other.use_lovense_connect,
      use_device_websocket_server: other.use_device_websocket_server,
      device_websocket_server_port: other.device_websocket_server_port,
      crash_main_thread: other.crash_main_thread,
      crash_task_thread: other.crash_task_thread,
      broadcast_server_mdns: other.broadcast_server_mdns,
      mdns_suffix: other.mdns_suffix,
      repeater_mode: other.repeater_mode,
      repeater_local_port: other.repeater_local_port,
      repeater_remote_address: other.repeater_remote_address,
    }
  }
}

#[derive(Default)]
pub struct EngineOptionsBuilder {
  options: EngineOptions,
}

impl EngineOptionsBuilder {
  pub fn device_config_json(&mut self, value: &str) -> &mut Self {
    self.options.device_config_json = Some(value.to_owned());
    self
  }

  pub fn user_device_config_json(&mut self, value: &str) -> &mut Self {
    self.options.user_device_config_json = Some(value.to_owned());
    self
  }

  pub fn server_name(&mut self, value: &str) -> &mut Self {
    self.options.server_name = value.to_owned();
    self
  }

  #[cfg(debug_assertions)]
  pub fn crash_main_thread(&mut self, value: bool) -> &mut Self {
    #[cfg(debug_assertions)]
    {
      self.options.crash_main_thread = value;
    }
    self
  }

  #[cfg(debug_assertions)]
  pub fn crash_task_thread(&mut self, value: bool) -> &mut Self {
    #[cfg(debug_assertions)]
    {
      self.options.crash_main_thread = value;
    }
    self
  }

  pub fn websocket_use_all_interfaces(&mut self, value: bool) -> &mut Self {
    self.options.websocket_use_all_interfaces = value;
    self
  }

  pub fn allow_raw_messages(&mut self, value: bool) -> &mut Self {
    self.options.allow_raw_messages = value;
    self
  }

  pub fn use_bluetooth_le(&mut self, value: bool) -> &mut Self {
    self.options.use_bluetooth_le = value;
    self
  }

  pub fn use_serial_port(&mut self, value: bool) -> &mut Self {
    self.options.use_serial_port = value;
    self
  }

  pub fn use_hid(&mut self, value: bool) -> &mut Self {
    self.options.use_hid = value;
    self
  }

  pub fn use_lovense_dongle_serial(&mut self, value: bool) -> &mut Self {
    self.options.use_lovense_dongle_serial = value;
    self
  }

  pub fn use_lovense_dongle_hid(&mut self, value: bool) -> &mut Self {
    self.options.use_lovense_dongle_hid = value;
    self
  }

  pub fn use_xinput(&mut self, value: bool) -> &mut Self {
    self.options.use_xinput = value;
    self
  }

  pub fn use_lovense_connect(&mut self, value: bool) -> &mut Self {
    self.options.use_lovense_connect = value;
    self
  }

  pub fn use_device_websocket_server(&mut self, value: bool) -> &mut Self {
    self.options.use_device_websocket_server = value;
    self
  }

  pub fn websocket_port(&mut self, port: u16) -> &mut Self {
    self.options.websocket_port = Some(port);
    self
  }

  pub fn websocket_client_address(&mut self, address: &str) -> &mut Self {
    self.options.websocket_client_address = Some(address.to_owned());
    self
  }

  pub fn frontend_websocket_port(&mut self, port: u16) -> &mut Self {
    self.options.frontend_websocket_port = Some(port);
    self
  }

  pub fn frontend_in_process_channel(&mut self, value: bool) -> &mut Self {
    self.options.frontend_in_process_channel = value;
    self
  }

  pub fn device_websocket_server_port(&mut self, port: u16) -> &mut Self {
    self.options.device_websocket_server_port = Some(port);
    self
  }

  pub fn max_ping_time(&mut self, value: u32) -> &mut Self {
    self.options.max_ping_time = value;
    self
  }

  pub fn broadcast_server_mdns(&mut self, value: bool) -> &mut Self {
    self.options.broadcast_server_mdns = value;
    self
  }

  pub fn mdns_suffix(&mut self, name: &str) -> &mut Self {
    self.options.mdns_suffix = Some(name.to_owned());
    self
  }

  pub fn use_repeater_mode(&mut self) -> &mut Self {
    self.options.repeater_mode = true;
    self
  }

  pub fn repeater_local_port(&mut self, port: u16) -> &mut Self {
    self.options.repeater_local_port = Some(port);
    self
  }

  pub fn repeater_remote_address(&mut self, addr: &str) -> &mut Self {
    self.options.repeater_remote_address = Some(addr.to_owned());
    self
  }

  pub fn finish(&mut self) -> EngineOptions {
    self.options.clone()
  }
}