lc3asm/
error.rs

1//! Provides [Error] type for error handling.
2use super::Rule;
3use pest::error::Error as PestError;
4use std::fmt::Error as FmtError;
5use std::io::Error as IOError;
6use std::num::ParseIntError;
7use std::str::Utf8Error;
8
9/// Assembler-related error type.
10#[derive(Debug)]
11pub enum Error {
12    Pest(PestError<Rule>),
13    ParseInt(ParseIntError),
14    Io(IOError),
15    Utf8(Utf8Error),
16    Fmt(FmtError),
17}
18
19impl From<PestError<Rule>> for Error {
20    fn from(e: PestError<Rule>) -> Error {
21        Error::Pest(e)
22    }
23}
24
25impl From<ParseIntError> for Error {
26    fn from(e: ParseIntError) -> Error {
27        Error::ParseInt(e)
28    }
29}
30
31impl From<IOError> for Error {
32    fn from(e: IOError) -> Error {
33        Error::Io(e)
34    }
35}
36
37impl From<Utf8Error> for Error {
38    fn from(e: Utf8Error) -> Error {
39        Error::Utf8(e)
40    }
41}
42
43impl From<FmtError> for Error {
44    fn from(e: FmtError) -> Error {
45        Error::Fmt(e)
46    }
47}
48
49impl std::fmt::Display for Error {
50    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
51        match self {
52            // FIXME: why intellij-rust tries to match fmt with Debug when err.fmt(f)?
53            Error::Pest(err) => std::fmt::Display::fmt(err, f),
54            Error::ParseInt(err) => err.fmt(f),
55            Error::Io(err) => err.fmt(f),
56            Error::Utf8(err) => err.fmt(f),
57            Error::Fmt(err) => err.fmt(f),
58        }
59    }
60}