1use std::{error, fmt, path::PathBuf};
2
3use cfd16_lib::InstructionError;
4
5#[derive(Debug, Clone)]
7pub struct AssemblerError {
8 pub line: Option<usize>,
10
11 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
21impl fmt::Display for AssemblerError {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 if let Some(l) = self.line {
24 write!(f, "Error (line {}): {}", l, self.kind)
25 } else {
26 write!(f, "Error: {}", self.kind)
27 }
28 }
29}
30
31impl error::Error for AssemblerError {}
32
33#[derive(Debug, Clone, PartialEq, Eq)]
35pub enum AssemErrorKind {
36 FileRead(PathBuf),
37 NoSuchSymbolFound(String),
38 InvalidLabel,
39 InvalidMnemonic(String),
40 InvalidInstruction(InstructionError),
41 InvalidArgument(String),
42 NoLabelBeforeDotLabel(String),
43 NoSuchBinaryFormat,
44 NonExecableInstr,
45 ElfNotYetSupported,
46}
47
48impl fmt::Display for AssemErrorKind {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 match self {
51 Self::FileRead(p) => write!(f, "Could not read file '{}'.", p.display()),
52 Self::NoSuchSymbolFound(s) => write!(f, "No such symbol could be found '{}'.", s),
53 Self::InvalidLabel => write!(f, "Invalid label."),
54 Self::InvalidMnemonic(m) => write!(f, "'{}' is not a valid mnemonic.", m),
55 Self::InvalidInstruction(i) => write!(f, "{}", i),
56 Self::InvalidArgument(a) => write!(f, "Argument '{}' is invalid.", a),
57 Self::NoLabelBeforeDotLabel(l) => write!(f, "No label before dot label '{}'.", l),
58 Self::NoSuchBinaryFormat => write!(f, "The binary format provided does not exist."),
59 Self::NonExecableInstr => write!(
60 f,
61 "The instruction is not executable for this section type."
62 ),
63 Self::ElfNotYetSupported => write!(f, "The ELF format cannot currently be assembled."),
64 }
65 }
66}