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
use core::{
    fmt::{self, Display, Formatter},
    num::ParseIntError,
};
use std::{error, io};

/// An error.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Error {
    /// I/O failure.
    Io(String),
    /// A missing call record.
    MissingCallRecord,
    /// A missing record type.
    MissingProcedureOperation,
    /// A missing stack.
    MissingStack,
    /// A missing time.
    MissingTime,
    /// Integer parse failure.
    ParseInt(ParseIntError),
    /// An unknown record type.
    UnknownRecordType,
}

impl Display for Error {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        match self {
            Self::Io(error) => write!(formatter, "{error}"),
            Self::MissingCallRecord => write!(formatter, "missing call record"),
            Self::MissingProcedureOperation => write!(formatter, "missing record type"),
            Self::MissingStack => write!(formatter, "missing stack"),
            Self::MissingTime => write!(formatter, "missing time"),
            Self::ParseInt(error) => write!(formatter, "{error}"),
            Self::UnknownRecordType => write!(formatter, "unknown record type"),
        }
    }
}

impl error::Error for Error {}

impl From<io::Error> for Error {
    fn from(error: io::Error) -> Self {
        Self::Io(error.to_string())
    }
}

impl From<ParseIntError> for Error {
    fn from(error: ParseIntError) -> Self {
        Self::ParseInt(error)
    }
}