cfd16_assembler/
error.rs

1use std::{error, fmt, path::PathBuf};
2
3use cfd16_lib::InstructionError;
4
5/// An error that may occur while assembling CFD-16 instructions.
6#[derive(Debug, Clone)]
7pub struct AssemblerError {
8    /// Information about where the error occurred.
9    pub line: Option<usize>,
10
11    /// Kind of error being reported.
12    pub kind: AssemErrorKind,
13}
14
15impl AssemblerError {
16    pub(crate) fn new(line: Option<usize>, kind: AssemErrorKind) -> Self {
17        Self { line, kind }
18    }
19
20    pub(crate) fn included(line: usize, suberr: AssemblerError) -> Self {
21        Self {
22            line: Some(line),
23            kind: AssemErrorKind::IncludedError(suberr.to_string()),
24        }
25    }
26}
27
28impl fmt::Display for AssemblerError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        if let Some(l) = self.line {
31            write!(f, "Error (line {}): {}", l, self.kind)
32        } else {
33            write!(f, "Error: {}", self.kind)
34        }
35    }
36}
37
38impl error::Error for AssemblerError {}
39
40/// A kind of error that can occur during assembly of CFD-16 instructions.
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub enum AssemErrorKind {
43    FileRead(PathBuf),
44    NoSuchSymbolFound(String),
45    IncludedError(String),
46    IncludePathInvalid,
47    InvalidLabel,
48    InvalidMnemonic(String),
49    InvalidInstruction(InstructionError),
50    InvalidArgument(String),
51    NoLabelBeforeDotLabel(String),
52    NoSuchBinaryFormat,
53    NonExecableInstr,
54    ElfNotYetSupported,
55    UnalignedInstruction,
56    StringNotAscii,
57}
58
59impl fmt::Display for AssemErrorKind {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            Self::FileRead(p) => write!(f, "Could not read file '{}'.", p.display()),
63            Self::NoSuchSymbolFound(s) => write!(f, "No such symbol could be found '{}'.", s),
64            Self::IncludedError(s) => write!(f, "Error from included file...\n{s}"),
65            Self::IncludePathInvalid => write!(
66                f,
67                "The provided path for the include directive cannot be read."
68            ),
69            Self::InvalidLabel => write!(f, "Invalid label."),
70            Self::InvalidMnemonic(m) => write!(f, "'{}' is not a valid mnemonic.", m),
71            Self::InvalidInstruction(i) => write!(f, "{}", i),
72            Self::InvalidArgument(a) => write!(f, "Argument '{}' is invalid.", a),
73            Self::NoLabelBeforeDotLabel(l) => write!(f, "No label before dot label '{}'.", l),
74            Self::NoSuchBinaryFormat => write!(f, "The binary format provided does not exist."),
75            Self::NonExecableInstr => write!(
76                f,
77                "The instruction is not executable for this section type."
78            ),
79            Self::ElfNotYetSupported => write!(f, "The ELF format cannot currently be assembled."),
80            Self::UnalignedInstruction => {
81                write!(f, "The instruction must be aligned to a word-boundary.")
82            }
83            Self::StringNotAscii => write!(f, "The string contains non-ascii characters."),
84        }
85    }
86}