little/error/
little.rs

1use std::fmt;
2use std::io;
3use std::error;
4use {
5    Constant,
6    Call,
7    BuildError,
8};
9
10/// Runtime error.
11#[derive(Debug)]
12pub enum LittleError {
13    /// A parameter was required for an instruction, but it was not found.
14    ParameterMissing(String),
15    /// A constant was required for an instruction, but it was not found.
16    ConstantMissing(Constant),
17    /// A call was required for an instruction, but it was not found.
18    CallMissing(Call),
19    /// A call has returned an error.
20    CallError(Box<error::Error + Sync + Send>),
21    /// I/O error writing template result to output.
22    OutputError(io::Error),
23    /// Error building the template.
24    BuildError(BuildError),
25    /// Attempt to pop values on empty stack.
26    StackUnderflow,
27    /// Instruction has caused an interupt, it is up to user to know how to handle it.
28    Interupt,
29}
30
31impl From<io::Error> for LittleError {
32    fn from(other: io::Error) -> LittleError {
33        LittleError::OutputError(other)
34    }
35}
36
37impl From<BuildError> for LittleError {
38    fn from(other: BuildError) -> LittleError {
39        LittleError::BuildError(other)
40    }
41}
42
43impl fmt::Display for LittleError {
44    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45        match *self {
46            LittleError::ParameterMissing(ref p) => write!(f, "Parameter {:?} is missing.", p),
47            LittleError::ConstantMissing(c) => write!(f, "Constant {:?} is missing.", c),
48            LittleError::CallMissing(c) => write!(f, "Call {:?} is missing.", c),
49            LittleError::CallError(ref e) => e.fmt(f),
50            LittleError::BuildError(ref e) => e.fmt(f),
51            LittleError::OutputError(ref e) => write!(f, "Output error: {:?}", e),
52            LittleError::StackUnderflow => write!(f, "Attempt to pop empty stack."),
53            LittleError::Interupt => write!(f, "Interupt."),
54        }
55    }
56}
57
58impl error::Error for LittleError {
59    fn description(&self) -> &str {
60        match *self {
61            LittleError::ParameterMissing(_) => "parameter is missing",
62            LittleError::ConstantMissing(_) => "constant is missing",
63            LittleError::CallMissing(_) => "call is missing",
64            LittleError::CallError(ref e) => e.description(),
65            LittleError::BuildError(ref e) => e.description(),
66            LittleError::OutputError(_) => "output error",
67            LittleError::StackUnderflow => "stack underflow",
68            LittleError::Interupt => "interupt",
69        }
70    }
71}
72
73/// Runtime result.
74pub type LittleResult<V> = Result<V, Box<error::Error>>;