improve output formatting

This commit is contained in:
Astro 2019-09-14 21:21:55 +02:00
parent b6af43feda
commit 700ab47f0e
3 changed files with 36 additions and 2 deletions

View File

@ -1,3 +1,4 @@
use core::fmt;
use nom::{
IResult,
branch::alt,
@ -39,6 +40,27 @@ impl From<ParseIntegerError> 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,

View File

@ -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");

View File

@ -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",
_ => "<INVALID>",
}.fmt(fmt)
}
}
pub enum SessionOutput {
Nothing,
Command(Command),