From 2731df7813e14d432c6a3da8ef81d2b2059bf590 Mon Sep 17 00:00:00 2001 From: topquark12 Date: Mon, 7 Jun 2021 09:46:31 +0800 Subject: [PATCH] main refactor: add error enum --- src/command_handler.rs | 13 +++++++++++-- src/main.rs | 8 ++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/command_handler.rs b/src/command_handler.rs index 764824e..aa3b7d1 100644 --- a/src/command_handler.rs +++ b/src/command_handler.rs @@ -50,7 +50,9 @@ pub enum Handler { #[derive(Clone, Debug, PartialEq)] pub enum Error { - + ReportError, + PostFilterRateError, + FlashError } fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool { @@ -99,7 +101,7 @@ impl Handler { Err(e) => { error!("unable to serialize report: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); - + return Err(Error::ReportError); } } Ok(Handler::Handled) @@ -113,6 +115,7 @@ impl Handler { Err(e) => { error!("unable to serialize pid summary: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); + return Err(Error::ReportError); } } Ok(Handler::Handled) @@ -126,6 +129,7 @@ impl Handler { Err(e) => { error!("unable to serialize pwm summary: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); + return Err(Error::ReportError); } } Ok(Handler::Handled) @@ -139,6 +143,7 @@ impl Handler { Err(e) => { error!("unable to serialize steinhart-hart summaries: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); + return Err(Error::ReportError); } } Ok(Handler::Handled) @@ -152,6 +157,7 @@ impl Handler { Err(e) => { error!("unable to serialize postfilter summary: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); + return Err(Error::ReportError); } } Ok(Handler::Handled) @@ -262,6 +268,7 @@ impl Handler { None => { error!("unable to choose postfilter for rate {:.3}", rate); send_line(socket, b"{{\"error\": \"unable to choose postfilter rate\"}}"); + return Err(Error::PostFilterRateError); } } Ok(Handler::Handled) @@ -282,6 +289,7 @@ impl Handler { Err(e) => { error!("unable to load config from flash: {:?}", e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); + return Err(Error::FlashError); } } } @@ -301,6 +309,7 @@ impl Handler { Err(e) => { error!("unable to save channel {} config to flash: {:?}", c, e); let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e); + return Err(Error::FlashError); } } } diff --git a/src/main.rs b/src/main.rs index 90fa8be..246e20d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ #![cfg_attr(test, allow(unused))] // TODO: #![deny(warnings, unused)] -// use Handler::NewIPV4; #[cfg(not(any(feature = "semihosting", test)))] use panic_abort as _; #[cfg(all(feature = "semihosting", not(test)))] @@ -12,7 +11,6 @@ use panic_semihosting as _; use log::{error, info, warn}; -// use core::fmt::Write; use cortex_m::asm::wfi; use cortex_m_rt::entry; use stm32f4xx_hal::{ @@ -209,10 +207,8 @@ fn main() -> ! { Ok(SessionInput::Nothing) => {} Ok(SessionInput::Command(command)) => { match Handler::handle_command(command, &mut socket, &mut channels, session, &mut leds, &mut store, &mut ipv4_config) { - Ok(Handler::NewIPV4(ip)) => { - new_ipv4_config = Some(ip); - } - Ok(Handler::Handled) => {} + Ok(Handler::NewIPV4(ip)) => new_ipv4_config = Some(ip), + Ok(Handler::Handled) => {}, Ok(Handler::CloseSocket) => socket.close(), Ok(Handler::Reset) => should_reset = true, Err(_) => {},