1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! Contains consolidated `Error` type.

use std::error::Error as StdError;
use std::fmt;

use crate::compile::CompileError;
use crate::encode::{DecodeError, EncodeError};
use crate::exec::ExecError;
use crate::io::IoError;
use crate::name::{NameDisplay, NameStore};
use crate::parser::ParseError;
use crate::restrict::RestrictError;

macro_rules! error_type {
    ( $( #[$meta:meta] )* pub enum $name:ident
            { $( $( #[$var_meta:meta] )+ $var:ident($ty:ty) , )+ } ) => {
        $( #[$meta] )*
        pub enum $name {
            $( $( #[$var_meta] )+ $var($ty) ),+
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                match *self {
                    $( $name::$var(ref e) => fmt::Display::fmt(e, f) ),+
                }
            }
        }

        impl NameDisplay for $name {
            fn fmt(&self, names: &NameStore, f: &mut fmt::Formatter) -> fmt::Result {
                match *self {
                    $( $name::$var(ref e) => NameDisplay::fmt(e, names, f) ),+
                }
            }
        }

        $(
            impl From<$ty> for $name {
                fn from(e: $ty) -> $name {
                    $name::$var(e)
                }
            }
        )+
    }
}

error_type!{
    /// Consolidated error type; contains one of a category of errors.
    #[derive(Debug)]
    pub enum Error {
        /// Error in compiling code to bytecode
        CompileError(CompileError),
        /// Error in decoding bytecode file format
        DecodeError(DecodeError),
        /// Error in encoding bytecode file format
        EncodeError(EncodeError),
        /// Error in executing code
        ExecError(ExecError),
        /// Error in file I/O operation
        IoError(IoError),
        /// Error in scanning text or parsing syntax
        ParseError(ParseError),
        /// Code execution breached configured restrictions
        RestrictError(RestrictError),
        /// Customized error value implementing `std::error::Error`
        Custom(Box<dyn StdError>),
    }
}

impl Error {
    /// Returns an `Error` value wrapping a custom error type.
    pub fn custom<E: 'static + StdError>(e: E) -> Error {
        Error::Custom(Box::new(e))
    }

    /// Returns a string describing the nature of the error.
    pub fn description(&self) -> &'static str {
        match *self {
            Error::CompileError(_) => "compile error",
            Error::DecodeError(_) => "decode error",
            Error::EncodeError(_) => "encode error",
            Error::ExecError(_) => "execution error",
            Error::IoError(_) => "I/O error",
            Error::ParseError(_) => "parse error",
            Error::RestrictError(_) => "restriction error",
            Error::Custom(_) => "error",
        }
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        self.description()
    }
}

#[cfg(test)]
mod test {
    use std::error::Error as StdError;
    use std::fmt;

    use super::Error;

    #[derive(Debug)]
    struct E;

    impl fmt::Display for E {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.write_str("an error occurred")
        }
    }

    impl StdError for E {
        fn description(&self) -> &str { "error" }
    }

    #[test]
    fn test_custom_error() {
        let e = Error::custom(E);
        assert_eq!(e.description(), "error");
        assert_eq!(e.to_string(), "an error occurred");
    }
}