From 430fc12f541d00f8b1d7fef40078dafd5233a029 Mon Sep 17 00:00:00 2001 From: occheung Date: Wed, 30 Sep 2020 15:57:33 +0800 Subject: [PATCH] mqtt_mux: elaborate process error --- src/mqtt_mux.rs | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/src/mqtt_mux.rs b/src/mqtt_mux.rs index d00d9cb..3ee4643 100644 --- a/src/mqtt_mux.rs +++ b/src/mqtt_mux.rs @@ -35,7 +35,7 @@ pub enum MqttTopic { // Such that Urukul accepts the enum directly #[derive(Debug, Clone)] pub enum MqttCommand { - ProcessError, + ProcessError(&'static str), Reset, Switch(u8, bool), Attenuation(u8, f32), @@ -74,19 +74,19 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer { let topic = match self.parse_topic(topic) { Ok(t) => t, Err(_) => { - self.yet_to_respond = Some(MqttCommand::ProcessError); + self.yet_to_respond = Some(MqttCommand::ProcessError("Cannot prase MQTT topic")); return; } }; let command = match self.parse_message(topic, message) { Ok((_, cmd)) => cmd, Err(_) => { - self.yet_to_respond = Some(MqttCommand::ProcessError); + self.yet_to_respond = Some(MqttCommand::ProcessError("Cannot parse MQTT message")); return; } }; self.yet_to_respond = match self.execute(command.clone()) { - Err(_) => Some(MqttCommand::ProcessError), + Err(_) => Some(MqttCommand::ProcessError("Cannot execute MQTT command")), Ok(()) => Some(command) }; } @@ -102,7 +102,7 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer { match prev_cmd { Some(cmd) => match cmd { - MqttCommand::ProcessError => { + MqttCommand::ProcessError(e_str) => { vec.push(( { let mut topic_string = String::from(self.name); @@ -110,7 +110,7 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer { .map_err(|_| Error::StringOutOfSpace)?; topic_string }, - String::from("Cannot parse the previous command.") + String::from(e_str) )).map_err(|_| Error::VectorOutOfSpace)?; Ok(vec) } @@ -273,20 +273,6 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer { )).map_err(|_| Error::VectorOutOfSpace)?; Ok(vec) } - - // _ => { - // vec.push(( - // { - // let mut topic_string = String::from(self.name); - // topic_string.push_str("/Feedback/Unimplemented") - // .map_err(|_| Error::StringOutOfSpace)?; - // topic_string - // }, - // String::from("test") - // )) - // .map_err(|_| Error::VectorOutOfSpace)?; - // Ok(vec) - // } }, None => Ok(vec), } @@ -494,7 +480,7 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer { fn execute(&mut self, command: MqttCommand) -> Result<(), Error> { match command { - MqttCommand::ProcessError => Ok(()), + MqttCommand::ProcessError(_) => Ok(()), MqttCommand::Reset => self.urukul.reset(), MqttCommand::Switch(ch, state) => self.urukul.set_channel_switch(ch.into(), state), MqttCommand::Attenuation(ch, ampl) => self.urukul.set_channel_attenuation(ch, ampl),