From 700ab47f0ef5098cc2b4d85191799f48a4169e29 Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 14 Sep 2019 21:21:55 +0200 Subject: [PATCH] improve output formatting --- firmware/src/command_parser.rs | 22 ++++++++++++++++++++++ firmware/src/main.rs | 4 ++-- firmware/src/session.rs | 12 ++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/firmware/src/command_parser.rs b/firmware/src/command_parser.rs index 728bfdb..b597336 100644 --- a/firmware/src/command_parser.rs +++ b/firmware/src/command_parser.rs @@ -1,3 +1,4 @@ +use core::fmt; use nom::{ IResult, branch::alt, @@ -39,6 +40,27 @@ impl From for Error { } } +impl fmt::Display for Error { + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self { + Error::Incomplete => + "incomplete input".fmt(fmt), + Error::UnexpectedInput(c) => { + "unexpected input: ".fmt(fmt)?; + c.fmt(fmt) + } + Error::Parser(e) => { + "parser: ".fmt(fmt)?; + (e as &dyn core::fmt::Debug).fmt(fmt) + } + Error::ParseInteger(e) => { + "parsing number: ".fmt(fmt)?; + e.fmt(fmt) + } + } + } +} + #[derive(Debug, Clone)] pub enum ShowCommand { ReportMode, diff --git a/firmware/src/main.rs b/firmware/src/main.rs index 42f49f9..b109f83 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -260,10 +260,10 @@ fn main() -> ! { Command::Quit => socket.close(), Command::Report(mode) => { - let _ = writeln!(socket, "Report mode: {:?}", mode); + let _ = writeln!(socket, "Report mode: {}", mode); } Command::Show(ShowCommand::ReportMode) => { - let _ = writeln!(socket, "Report mode: {:?}", session.report_mode()); + let _ = writeln!(socket, "Report mode: {}", session.report_mode()); } Command::Show(ShowCommand::Pid) => { let _ = writeln!(socket, "PID settings"); diff --git a/firmware/src/session.rs b/firmware/src/session.rs index 958f69c..84925fb 100644 --- a/firmware/src/session.rs +++ b/firmware/src/session.rs @@ -1,4 +1,5 @@ use core::ops::Deref; +use core::fmt; use super::command_parser::{Command, Error as ParserError}; const MAX_LINE_LEN: usize = 64; @@ -60,6 +61,17 @@ pub enum ReportMode { Continuous, } +impl fmt::Display for ReportMode { + fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self { + ReportMode::Off => "off", + ReportMode::Once => "once", + ReportMode::Continuous => "continuous", + _ => "", + }.fmt(fmt) + } +} + pub enum SessionOutput { Nothing, Command(Command),