use std::{error::Error as StdError, fmt, num::ParseIntError, str::Utf8Error};
#[derive(PartialEq, Clone, Debug)]
pub enum Error {
MissingStartCode,
RecordTooShort,
RecordTooLong,
RecordNotEvenLength,
ContainsInvalidCharacters,
ChecksumMismatch(u8, u8),
PayloadLengthMismatch,
UnsupportedRecordType {
record: String,
record_type: u8,
},
InvalidLengthForType,
DataExceedsMaximumLength(usize),
MissingEndOfFileRecord,
MultipleEndOfFileRecords(usize),
SynthesisFailed,
WrongByteCount(u32),
InvalidStartAddress,
EmptyIntelHexError,
OverSpecifiedStartEnd,
InvalideSize,
DuplicateStartAddressRecordError,
AddressOverlapError(u32),
WriterError,
NotEnoughData(u32),
AlignmentToSizeError {
size: usize,
alignment: usize,
},
AddDataError,
UnsupportedFileFormat,
AddressTooBig,
IoError,
InvalidAddressRange,
Utf8Error(std::str::Utf8Error),
RegExError(regex::Error),
}
unsafe impl Sync for Error {}
unsafe impl Send for Error {}
impl StdError for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::MissingStartCode => {
write!(f, "missing start code ':' on intel hex or 'S' on Srec")
}
Error::RecordTooShort => write!(f, "too short"),
Error::RecordTooLong => write!(f, "too long"),
Error::RecordNotEvenLength => {
write!(f, "record does not contain a whole number of bytes")
}
Error::ContainsInvalidCharacters => {
write!(f, "invalid characters encountered in record")
}
Error::ChecksumMismatch(found, expecting) => write!(
f,
"invalid checksum '{:02X}', expecting '{:02X}'",
found, expecting,
),
Error::PayloadLengthMismatch => {
write!(f, "payload length does not match record header")
}
Error::UnsupportedRecordType {
record,
record_type,
} => {
write!(
f,
"unsupported record type in {}, provided record type is {}",
record, record_type
)
}
Error::InvalidLengthForType => {
write!(f, "payload length invalid for record type")
}
Error::DataExceedsMaximumLength(bytes) => {
write!(f, "record has {} bytes (max 255)", bytes)
}
Error::MissingEndOfFileRecord => {
write!(f, "object is missing end of file record")
}
Error::MultipleEndOfFileRecords(eofs) => {
write!(f, "object contains {} end of file records", eofs)
}
Error::SynthesisFailed => {
write!(f, "unable to write string representation of record")
}
Error::WrongByteCount(byte_count) => {
write!(f, "byte count for hex file export is wrong. Value is {} and should be in between 1..255", byte_count)
}
Error::InvalidStartAddress => {
write!(f, "start address should be exported but is not available")
}
Error::EmptyIntelHexError => {
write!(
f,
"requested operation cannot be executed with empty object"
)
}
Error::OverSpecifiedStartEnd => {
write!(f, "start, End and size is speziefied for export")
}
Error::InvalideSize => {
write!(f, "specified size for export is invalid")
}
Error::DuplicateStartAddressRecordError => {
write!(f, "duplicated start address record")
}
Error::AddressOverlapError(address) => {
write!(f, "hex file has data overlap at address: {:0X}", address)
}
Error::WriterError => {
write!(f, "writer error")
}
Error::NotEnoughData(address) => {
write!(
f,
"Bad access at {:0X}. No data to read continues bytes",
address
)
}
Error::AlignmentToSizeError { size, alignment } => {
write!(
f,
"Size {} is not a multiple of aligment {}",
size, alignment
)
}
Error::AddDataError => {
write!(
f,
"data added to a segment must be adjacent to or overlapping with the original segment data"
)
}
Error::UnsupportedFileFormat => {
write!(f, "Unsupported file format")
}
Error::AddressTooBig => {
write!(f, "Address too big for selected format")
}
Error::IoError => {
write!(f, "General IO Error")
}
Error::InvalidAddressRange => {
write!(f, "Invalid Address Range")
}
Error::Utf8Error(utf8_error) => write!(f, "Error converting UTF8: {}", utf8_error),
Error::RegExError(error) => write!(f, "Regular Expression Error: {}", error),
}
}
}
impl From<regex::Error> for Error {
fn from(value: regex::Error) -> Self {
Self::RegExError(value)
}
}
impl From<Utf8Error> for Error {
fn from(value: Utf8Error) -> Self {
Self::Utf8Error(value)
}
}
impl From<ParseIntError> for Error {
fn from(_value: ParseIntError) -> Self {
Self::ContainsInvalidCharacters
}
}