use crate::{KOSValueParseError, OpcodeParseError};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum KSMParseError {
#[error("Error while decompressing KSM file: {0}")]
DecompressionError(std::io::Error),
#[error("Error while reading KSM file header: {0}")]
HeaderError(HeaderParseError),
#[error("Error while parsing KSM file argument section: {0}")]
ArgumentSectionParseError(ArgumentSectionParseError),
#[error("Error while parsing KSM file code section: {0}")]
CodeSectionParseError(CodeSectionParseError),
#[error("Error while parsing KSM file, encountered % after argument section at index {0}, expected a debug or code section, but found end of file")]
MissingSectionType(usize),
#[error("Error while parsing KSM file debug section: {0}")]
DebugSectionParseError(DebugSectionParseError),
}
#[derive(Debug, Error, Copy, Clone)]
pub enum HeaderParseError {
#[error("End of file reached while reading")]
EOF,
#[error("Not a KSM file. Expected first 4 bytes to be 0x6b035845, found 0x{0:x}")]
InvalidMagic(u32),
}
#[derive(Error, Debug, Copy, Clone)]
pub enum ArgumentSectionParseError {
#[error("End of file reached. Expected %A")]
MissingHeader,
#[error("Expected %A as the start of the argument section, which is the required first section in a KSM file, found: 0x{0:x}")]
InvalidHeader(u16),
#[error("End of file reached while trying to parse the number of argument index bytes")]
MissingNumArgIndexBytes,
#[error("Invalid value of {0} for NumArgIndexBytes after section header. Supported values are 1, 2, 3, and 4")]
InvalidNumArgIndexBytes(u8),
#[error("End of file reached while reading next KOSValue. The argument section ends when a % is found, and a code section begins")]
EOF,
#[error("Error while parsing KOSValue at byte offset {0}: {1}")]
KOSValueParseError(usize, KOSValueParseError),
}
#[derive(Error, Debug, Copy, Clone)]
pub enum CodeSectionParseError {
#[error("End of file reached while parsing code section type")]
MissingCodeSectionType,
#[error("Found 0x{0:x} after section header `%`, expected F, I, or M")]
InvalidCodeSectionType(u8),
#[error("End of file reached while reading next kOS instruction. The code section ends when a % is found, and a new code or debug section begins")]
EOF,
#[error("Error while parsing instruction: {0}")]
InstrParseError(InstrParseError),
}
#[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(Error, Debug, Copy, Clone)]
pub enum DebugSectionParseError {
#[error("Error while parsing debug entry at offset {0}: {1}")]
DebugEntryParseError(usize, DebugEntryParseError),
#[error("End of file reached while trying to parse the debug range size")]
MissingDebugRangeSize,
#[error("Invalid value of {0} for range size after section header. Supported values are 1, 2, 3, and 4")]
InvalidDebugRangeSize(u8),
}
#[derive(Error, Debug, Copy, Clone)]
pub enum DebugEntryParseError {
#[error("Reached EOF while reading line number")]
MissingLineNumber,
#[error("Reached EOF while reading the number of ranges")]
MissingNumRanges,
#[error("Reached EOF while reading debug range {0}")]
MissingRange(usize),
}