Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::convert::From;
- use std::error::Error;
- use std::fmt::{Display, Formatter, Result};
- #[derive(Debug)]
- pub enum ErrorKind {
- InvalidOption(InvalidOptionError),
- MissingOptArg(MissingOptArgError),
- MissingArgument(MissingArgumentError),
- }
- impl From<InvalidOptionError> for ErrorKind {
- fn from(err: InvalidOptionError) -> ErrorKind {
- ErrorKind::InvalidOption(err)
- }
- }
- impl From<MissingOptArgError> for ErrorKind {
- fn from(err: MissingOptArgError) -> ErrorKind {
- ErrorKind::MissingOptArg(err)
- }
- }
- impl From<MissingArgumentError> for ErrorKind {
- fn from(err: MissingArgumentError) -> ErrorKind {
- ErrorKind::MissingArgument(err)
- }
- }
- impl Error for ErrorKind {
- fn description(&self) -> &str {
- match *self {
- ErrorKind::InvalidOption(ref err) => err.description(),
- ErrorKind::MissingOptArg(ref err) => err.description(),
- ErrorKind::MissingArgument(ref err) => err.description(),
- }
- }
- fn cause(&self) -> Option<&Error> {
- Some(match *self {
- ErrorKind::InvalidOption(ref err) => err as &Error,
- ErrorKind::MissingOptArg(ref err) => err as &Error,
- ErrorKind::MissingArgument(ref err) => err as &Error,
- })
- }
- }
- impl Display for ErrorKind {
- fn fmt(&self, f: &mut Formatter) -> Result {
- match *self {
- ErrorKind::InvalidOption(ref err) => Display::fmt(err, f),
- ErrorKind::MissingOptArg(ref err) => Display::fmt(err, f),
- ErrorKind::MissingArgument(ref err) => Display::fmt(err, f),
- }
- }
- }
- #[derive(Debug)]
- pub struct InvalidOptionError {
- desc: String,
- }
- impl InvalidOptionError {
- pub fn new(opt: &str) -> InvalidOptionError {
- InvalidOptionError {
- desc: format!("An invalid option was passed to the program: {}", opt)
- }
- }
- }
- impl Error for InvalidOptionError {
- fn description(&self) -> &str {
- &self.desc
- }
- }
- impl Display for InvalidOptionError {
- fn fmt(&self, f: &mut Formatter) -> Result {
- write!(f, "{}", &self.desc)
- }
- }
- #[derive(Debug)]
- pub struct MissingOptArgError {
- desc: String,
- }
- impl MissingOptArgError {
- pub fn new(opt: &str) -> MissingOptArgError {
- MissingOptArgError {
- desc: format!("A required argument is missing after the option '{}'", opt)
- }
- }
- }
- impl Error for MissingOptArgError {
- fn description(&self) -> &str {
- &self.desc
- }
- }
- impl Display for MissingOptArgError {
- fn fmt(&self, f: &mut Formatter) -> Result {
- write!(f, "{}", &self.desc)
- }
- }
- #[derive(Debug)]
- pub struct MissingArgumentError {
- desc: String,
- }
- impl MissingArgumentError {
- pub fn new() -> MissingArgumentError {
- MissingArgumentError {
- desc: format!("A required program argument is missing")
- }
- }
- }
- impl Error for MissingArgumentError {
- fn description(&self) -> &str {
- &self.desc
- }
- }
- impl Display for MissingArgumentError {
- fn fmt(&self, f: &mut Formatter) -> Result {
- write!(f, "{}", &self.desc)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement