From c7dfbd8a84fedb11e816b7ff2ca6ed06daff8ec4 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Fri, 3 Apr 2020 10:28:04 +0100 Subject: [PATCH] vmm: config: Implement fmt::Display for error Fixes: #367 Signed-off-by: Rob Bradford --- vmm/src/config.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/vmm/src/config.rs b/vmm/src/config.rs index 434bb6dd6..91eaed41a 100644 --- a/vmm/src/config.rs +++ b/vmm/src/config.rs @@ -9,6 +9,7 @@ use clap::ArgMatches; use net_util::MacAddr; use std::collections::HashMap; use std::convert::From; +use std::fmt; use std::net::Ipv4Addr; use std::path::PathBuf; use std::result; @@ -72,6 +73,52 @@ pub enum Error { /// Failed to parse vsock parameters ParseVsock(OptionParserError), } + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use self::Error::*; + match self { + ParseConsole(o) => write!(f, "Error parsing --console: {}", o), + ParseConsoleFileMissing => { + write!(f, "Error parsing --console: path missing when using file") + } + ParseConsoleInvalidModeGiven => { + write!(f, "Error parsing --console: invalid console mode given") + } + ParseCpus(o) => write!(f, "Error parsing --cpus: {}", o), + ParseCpusMaxLowerThanBoot => { + write!(f, "Error parsing --cpus: max CPUs greater than boot CPUs") + } + ParseDevice(o) => write!(f, "Error parsing --device: {}", o), + ParseDevicePathMissing => write!(f, "Error parsing --device: path missing"), + ParseDiskSocketAndPath => write!( + f, + "Error parsing --disk: vhost socket and disk path both provided" + ), + ParseFileSystem(o) => write!(f, "Error parsing --fs: {}", o), + ParseFsSockMissing => write!(f, "Error parsing --fs: sock missing"), + ParseFsTagMissing => write!(f, "Error parsing --fs: tag missing"), + InvalidCacheSizeWithDaxOff => { + write!(f, "Error parsing --fs: cache_size used with dax=on") + } + ParsePersistentMemory(o) => write!(f, "Error parsing --pmem: {}", o), + ParsePmemFileMissing => write!(f, "Error parsing --pmem: file missing"), + ParsePmemSizeMissing => write!(f, "Error parsing --pmem: size missing"), + ParseTTYParam => write!( + f, + "Console mode tty specified for both --serial and --console" + ), + ParseVsock(o) => write!(f, "Error parsing --vsock: {}", o), + ParseVsockCidMissing => write!(f, "Error parsing --vsock: cid missing"), + ParseVsockSockMissing => write!(f, "Error parsing --vsock: sock missing"), + ParseMemory(o) => write!(f, "Error parsing --memory: {}", o), + ParseNetwork(o) => write!(f, "Error parsing --net: {}", o), + ParseDisk(o) => write!(f, "Error parsing --disk: {}", o), + ParseRNG(o) => write!(f, "Error parsing --rng: {}", o), + } + } +} + pub type Result = result::Result; #[derive(Default)] @@ -91,6 +138,17 @@ pub enum OptionParserError { Conversion(String, String), } +impl fmt::Display for OptionParserError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + OptionParserError::UnknownOption(s) => write!(f, "unknown option: {}", s), + OptionParserError::InvalidSyntax(s) => write!(f, "invalid syntax:{}", s), + OptionParserError::Conversion(field, value) => { + write!(f, "unable to parse {} for {}", value, field) + } + } + } +} type OptionParserResult = std::result::Result; impl OptionParser {