use crate::ko::sections::SectionKind;
use crate::{KOSValueParseError, OpcodeParseError};
use thiserror::Error;
#[derive(Debug, Error, Clone)]
pub enum KOParseError {
#[error("Error while reading KO file header: {0}")]
HeaderError(HeaderParseError),
#[error(
"KO file section header tables MUST contain a null section header at index 0 in the table, found section kind {0:?}"
)]
MissingNullSectionHeader(SectionKind),
#[error(
"KO file null section header table entries must only be the first entry in the table, although the section header at index is of kind null {0}"
)]
StrayNullSectionHeader(u16),
#[error("Error while reading KO file string table: {0}")]
StringTableParseError(StringTableParseError),
#[error("Error while reading KO file symbol table: {0}")]
SymbolTableParseError(SymbolTableParseError),
#[error("Error while reading KO file data section: {0}")]
DataSectionParseError(DataSectionParseError),
#[error("Error while reading KO file function section: {0}")]
FunctionSectionParseError(FunctionSectionParseError),
#[error("Error while reading KO file relocation data section: {0}")]
ReldSectionParseError(ReldSectionParseError),
#[error("Debug sections are currently unsupported and undefined, but the section kind was used for section header {0}")]
DebugSectionUnsupportedError(u16),
#[error("Error while reading KO file section header table: {0}")]
SectionHeaderParseError(SectionHeaderParseError),
#[error("Error while reading KO file: .shstrtab section index must not be 0")]
NullShStrTabIndexError,
#[error("Error while reading KO file: .shstrtab section index `{0}` is out of bounds of the number of sections `{1}`")]
InvalidShStrTabIndexError(u16, u16),
}
#[derive(Debug, Error, Copy, Clone)]
pub enum HeaderParseError {
#[error("Input buffer was empty")]
MissingMagicError,
#[error("Input file does not appear to be a KO file. Expected the first four bytes to be {0:08x}, found {1:08x}")]
InvalidMagicError(u32, u32),
#[error("Reached end of file trying to read KerbalObject file version")]
MissingVersionError,
#[error("Only KerbalObject files version {0} can be read with this library version, tried to read file with version {1}")]
UnsupportedVersionError(u8, u8),
#[error("Reached end of file trying to read the number of headers in the header table")]
MissingNumHeaders,
#[error("Reached end of file trying to read section header string table index")]
MissingShStrTabIndex,
}
#[derive(Debug, Error, Clone)]
pub enum StringTableParseError {
#[error("Reached end of file at file byte {0}, expected total size: {1}")]
EOFError(usize, u32),
#[error("String number {0} that was read is invalid UTF-8 bytes: {1}")]
InvalidUtf8Error(usize, std::string::FromUtf8Error),
}
#[derive(Debug, Error, Copy, Clone)]
pub enum SectionHeaderParseError {
#[error("Reached end of file trying to read section name index")]
MissingNameIdxError,
#[error("Reached end of file trying to read section kind for section header at byte {0}")]
MissingSectionKindError(usize),
#[error(
"Section kind invalid for section header at byte {0}, expected 0, 1, 2, 3, 4, 5, or 6, found {1}"
)]
InvalidSectionKindError(usize, u8),
#[error("Reached end of file trying to read section size for section header at byte {0}")]
MissingSizeError(usize),
}
#[derive(Debug, Error, Copy, Clone)]
pub enum ReldEntryParseError {
#[error("Reached end of file trying to read entry section index")]
MissingSectionIndexError,
#[error("Reached end of file trying to read entry instruction index")]
MissingInstructionIndexError,
#[error("Reached end of file trying to read entry operand index")]
MissingOperandIndexError,
#[error("Entry has an invalid operand index of {0}, expected either 1 or 2")]
InvalidOperandIndexError(u8),
#[error("Reached end of file trying to read entry symbol index")]
MissingSymbolIndexError,
}
#[derive(Debug, Error, Copy, Clone)]
pub enum SymbolParseError {
#[error("Reached end of file trying to read symbol name index")]
MissingNameIndexError,
#[error("Reached end of file trying to read symbol value index")]
MissingValueIndexError,
#[error("Reached end of file trying to read symbol size")]
MissingSymbolSizeError,
#[error("Reached end of file trying to read symbol binding")]
MissingSymbolBindingError,
#[error("Symbol has an invalid symbol binding value {0}, expected 0, 1, or 2")]
InvalidSymbolBindingError(u8),
#[error("Reached end of file trying to read symbol type")]
MissingSymbolTypeError,
#[error("Symbol has an invalid symbol type value {0}, expected 0, 1, 2, 3, or 4")]
InvalidSymbolTypeError(u8),
#[error("Reached end of file trying to read symbol section index")]
MissingSectionIndexError,
}
#[derive(Debug, Error, Copy, Clone)]
pub enum SymbolTableParseError {
#[error("Failed to parse symbol number {0} at byte offset {1}: {2}")]
SymbolParseError(usize, usize, SymbolParseError),
}
#[derive(Debug, Error, Copy, Clone)]
pub enum DataSectionParseError {
#[error("Error reading KOSValue at file byte offset {0}: {1}")]
KOSValueParseError(usize, KOSValueParseError),
}
#[derive(Error, Debug, Copy, Clone)]
pub enum InstrParseError {
#[error("Error reading opcode at file byte offset {0}: {1}")]
OpcodeParseError(usize, OpcodeParseError),
#[error("Reached EOF while reading operand {0}")]
MissingOperand(usize),
}
#[derive(Debug, Error, Copy, Clone)]
pub enum FunctionSectionParseError {
#[error("Error reading instruction at file byte offset {0}: {1}")]
InstrParseError(usize, InstrParseError),
}
#[derive(Debug, Error, Copy, Clone)]
pub enum ReldSectionParseError {
#[error("Failed to parse relocation data entry {0} at byte offset {1}: {2}")]
ReldEntryParseError(usize, usize, ReldEntryParseError),
}
#[derive(Debug, Error, Clone)]
pub enum ValidationError {
#[error(
"Error validating KOFile: An inserted section of kind {0:?} has no corresponding section header entry at index {1}"
)]
InvalidSectionIndexError(SectionKind, u16),
#[error("Error validating KOFile: Section kind mismatch. An inserted section of kind {0:?} has a corresponding section header entry at index {1} that is of type {2:?}")]
SectionKindMismatchError(SectionKind, u16, SectionKind),
#[error("Error validating KOFile: An inserted section header at index {0} of kind {1:?} and name {2} has no corresponding section within the file")]
InvalidSectionHeaderError(u16, SectionKind, String),
#[error("Error validating KOFile: An inserted section header at index {0} of kind {1:?} has an invalid name index of {2}")]
InvalidSectionHeaderNameIndexError(u16, SectionKind, usize),
}