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
use std::fmt;
use std::io;
use std::error;
use {
    Constant,
    Call,
    BuildError,
};

/// Runtime error.
#[derive(Debug)]
pub enum LittleError {
    /// A parameter was required for an instruction, but it was not found.
    ParameterMissing(String),
    /// A constant was required for an instruction, but it was not found.
    ConstantMissing(Constant),
    /// A call was required for an instruction, but it was not found.
    CallMissing(Call),
    /// A call has returned an error.
    CallError(Box<error::Error + Sync + Send>),
    /// I/O error writing template result to output.
    OutputError(io::Error),
    /// Error building the template.
    BuildError(BuildError),
    /// Attempt to pop values on empty stack.
    StackUnderflow,
    /// Instruction has caused an interupt, it is up to user to know how to handle it.
    Interupt,
}

impl From<io::Error> for LittleError {
    fn from(other: io::Error) -> LittleError {
        LittleError::OutputError(other)
    }
}

impl From<BuildError> for LittleError {
    fn from(other: BuildError) -> LittleError {
        LittleError::BuildError(other)
    }
}

impl fmt::Display for LittleError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            LittleError::ParameterMissing(ref p) => write!(f, "Parameter {:?} is missing.", p),
            LittleError::ConstantMissing(c) => write!(f, "Constant {:?} is missing.", c),
            LittleError::CallMissing(c) => write!(f, "Call {:?} is missing.", c),
            LittleError::CallError(ref e) => e.fmt(f),
            LittleError::BuildError(ref e) => e.fmt(f),
            LittleError::OutputError(ref e) => write!(f, "Output error: {:?}", e),
            LittleError::StackUnderflow => write!(f, "Attempt to pop empty stack."),
            LittleError::Interupt => write!(f, "Interupt."),
        }
    }
}

impl error::Error for LittleError {
    fn description(&self) -> &str {
        match *self {
            LittleError::ParameterMissing(_) => "parameter is missing",
            LittleError::ConstantMissing(_) => "constant is missing",
            LittleError::CallMissing(_) => "call is missing",
            LittleError::CallError(ref e) => e.description(),
            LittleError::BuildError(ref e) => e.description(),
            LittleError::OutputError(_) => "output error",
            LittleError::StackUnderflow => "stack underflow",
            LittleError::Interupt => "interupt",
        }
    }
}

/// Runtime result.
pub type LittleResult<V> = Result<V, Box<error::Error>>;