brainfuck/
error.rs

1use std::{fmt, io};
2use super::tape;
3use super::program;
4
5/// A general error type for problems inside of the interpreter.
6#[derive(Debug)]
7pub enum Error {
8    /// Errors with reading or writing to IO.
9    Io(io::Error),
10    /// Errors with the underlying tape.
11    Tape(tape::Error),
12    /// Errors with the program.
13    Program(program::Error),
14    /// No program loaded.
15    NoProgram,
16    /// Interpreter cycle limit hit.
17    CycleLimit,
18}
19
20impl fmt::Display for Error {
21    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22        match *self {
23            Error::Io(ref e) => e.fmt(f),
24            Error::Tape(ref e) => e.fmt(f),
25            Error::Program(ref e) => e.fmt(f),
26            Error::NoProgram => write!(f, "{}", "No program loaded"),
27            Error::CycleLimit => write!(f, "{}", "Cycle limit hit"),
28        }
29    }
30}
31
32impl From<io::Error> for Error {
33    fn from(e: io::Error) -> Error {
34        Error::Io(e)
35    }
36}
37
38impl From<tape::Error> for Error {
39    fn from(e: tape::Error) -> Error {
40        Error::Tape(e)
41    }
42}
43
44impl From<program::Error> for Error {
45    fn from(e: program::Error) -> Error {
46        Error::Program(e)
47    }
48}