use super::Instruction;
pub trait ParseError {
fn describe(&self, line_breaks: bool) -> String;
}
#[derive(PartialEq, Debug)]
pub enum ValueParseError {
InvalidValue(String),
InvalidHex(String),
InvalidDecimal(String),
InvalidOctal(String),
InvalidBinary(String),
InvalidTagName(String),
}
impl ParseError for ValueParseError {
fn describe(&self, _line_breaks: bool) -> String {
match self {
ValueParseError::InvalidValue(v) => format!("The value: {}; is an invalid value. ", v),
ValueParseError::InvalidHex(v) => format!("The value: {}; is invalid hex value. ", v),
ValueParseError::InvalidDecimal(v) => format!("The value: {}; is invalid decimal value. ", v),
ValueParseError::InvalidOctal(v) => format!("The value: {}; is invalid octal value. ", v),
ValueParseError::InvalidBinary(v) => format!("The value: {}; is invalid binary value. ", v),
ValueParseError::InvalidTagName(v) => format!("The value: {}; is invalid tag name. ", v),
}
}
}
#[derive(PartialEq, Debug)]
pub enum InstructionError {
UnkownInstruction(String),
OperandValueParseError(Instruction, ValueParseError)
}
impl ParseError for InstructionError {
fn describe(&self, line_breaks: bool) -> String {
let line_break = if line_breaks { "\n" } else { "" };
match self {
InstructionError::UnkownInstruction(v) => format!("The specified instruction {} is not known. ", v),
InstructionError::OperandValueParseError(c, v) => format!("Failed to parse operand for {}. {line_break} {}", c.describe(), v.describe(line_breaks))
}
}
}
#[derive(PartialEq, Debug)]
pub enum AbsoluteError {
ValueError(ValueParseError)
}
impl ParseError for AbsoluteError {
fn describe(&self, line_breaks: bool) -> String {
let line_break = if line_breaks { "\n" } else { "" };
match self {
AbsoluteError::ValueError(v) => format!("Failed to parse value for absolute value declaration. {line_break} {}", v.describe(line_breaks))
}
}
}
#[derive(PartialEq, Debug)]
pub enum TagError {
TagNameWhitespace(String)
}
impl ParseError for TagError {
fn describe(&self, _line_breaks: bool) -> String {
match self {
TagError::TagNameWhitespace(v) => format!("The tag name `{}` is invalid. ", v)
}
}
}
#[derive(PartialEq, Debug)]
pub enum LineParseError {
TagError(TagError),
AbsoluteError(AbsoluteError),
InstructionError(InstructionError),
}
impl ParseError for LineParseError {
fn describe(&self, line_breaks: bool) -> String {
let line_break = if line_breaks { "\n" } else { "" };
match self {
LineParseError::TagError(v) => format!("Error parsing a tag line, {}", v.describe(line_breaks)),
LineParseError::AbsoluteError(v) => format!("Error parsing an absolute value line. {line_break} {}", v.describe(line_breaks)),
LineParseError::InstructionError(v) => format!("Error parsing a instruction line. {line_break} {}", v.describe(line_breaks)),
}
}
}