message: chore: Unfold device manager message nested matches
diff: @@ -134,32 +134,31 @@ impl DeviceManager {
  Ok(())
  }
  - pub async fn parse_message(
  - &mut self,
  - msg: ButtplugMessageUnion,
  + async fn parse_device_message(&mut self,
  + device_msg: ButtplugDeviceCommandMessageUnion,
  ) -> Result<ButtplugMessageUnion, ButtplugError> {
  - // If this is a device command message, just route it directly to the
  - // device.
  - if let Ok(device_msg) = ButtplugDeviceCommandMessageUnion::try_from(msg.clone()) {
  let mut dev;
  - if let Some(device) = self
  + match self
  .devices
  .read()
  .unwrap()
  .get(&device_msg.get_device_index())
  {
  + Some(device) => {
  dev = device.clone();
  - } else {
  - return Err(ButtplugDeviceError::new(&format!(
  + // TODO This should probably spawn or something
  + dev.parse_message(&device_msg).await
  + }
  + None => Err(ButtplugDeviceError::new(&format!(
  "No device with index {} available",
  device_msg.get_device_index()
  - ))
  - .into());
  + )).into())
  }
  - // TODO This should probably spawn or something
  - dev.parse_message(&device_msg).await
  - } else {
  - if let Ok(manager_msg) = ButtplugDeviceManagerMessageUnion::try_from(msg.clone()) {
  + }
  +
  + async fn parse_device_manager_message(&mut self,
  + manager_msg: ButtplugDeviceManagerMessageUnion,
  + ) -> Result<ButtplugMessageUnion, ButtplugError> {
  match manager_msg {
  ButtplugDeviceManagerMessageUnion::RequestDeviceList(msg) => {
  let devices = self
  @@ -191,8 +190,21 @@ impl DeviceManager {
  Ok(messages::Ok::new(msg.get_id()).into())
  }
  }
  - } else {
  - Err(ButtplugMessageError::new("Message type not handled by Device Manager").into())
  + }
  +
  + pub async fn parse_message(
  + &mut self,
  + msg: ButtplugMessageUnion,
  + ) -> Result<ButtplugMessageUnion, ButtplugError> {
  + // If this is a device command message, just route it directly to the
  + // device.
  + match ButtplugDeviceCommandMessageUnion::try_from(msg.clone()) {
  + Ok(device_msg) => self.parse_device_message(device_msg).await,
  + Err(_) => {
  + match ButtplugDeviceManagerMessageUnion::try_from(msg.clone()) {
  + Ok(manager_msg) => self.parse_device_manager_message(manager_msg).await,
  + Err(_) => Err(ButtplugMessageError::new("Message type not handled by Device Manager").into())
  + }
  }
  }
  }
  