1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
pub type ReadResult<T> = Result<T, ReadError>;

#[derive(Debug)]
pub enum ReadError {
    ConstantReadError(ConstantReadError),
    OpcodeReadError,
    BogusOpcodeReadError(u8),
    OperandReadError,
    SymBindReadError,
    UnknownSimBindReadError(u8),
    SymTypeReadError,
    UnknownSimTypeReadError(u8),
    KOSymbolConstantReadError(&'static str),
    KOSValueTypeReadError(u8),
    KOSValueReadError,
    SectionKindReadError,
    UnknownSectionKindReadError(u8),
    SectionHeaderConstantReadError(&'static str),
    StringTableReadError,
    KOHeaderReadError(&'static str),
    KSMHeaderReadError(&'static str),
    VersionMismatchError(u8),
    DebugSectionUnsupportedError,
    MissingSectionError(&'static str),
    InvalidKOFileMagicError,
    InvalidKSMFileMagicError,
    KSMDecompressionError(std::io::Error),
    MissingArgumentSectionError,
    ExpectedArgumentSectionError(u16),
    NumIndexBytesReadError,
    InvalidNumIndexBytesError(usize),
    ArgumentSectionReadError,
    CodeTypeReadError,
    UnknownCodeTypeReadError(char),
    MissingCodeSectionError,
    ExpectedCodeSectionError(u8),
    CodeSectionReadError,
    DebugRangeReadError,
    DebugEntryReadError,
    MissingDebugSectionError,
    ExpectedDebugSectionError(u8),
    DebugSectionReadError,
}

#[derive(Debug)]
pub enum ConstantReadError {
    BoolReadError,
    U8ReadError,
    I8ReadError,
    U16ReadError,
    I16ReadError,
    U32ReadError,
    I32ReadError,
    F32ReadError,
    F64ReadError,
    StringReadError,
}

impl std::error::Error for ReadError {}
impl std::error::Error for ConstantReadError {}

impl std::fmt::Display for ReadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ReadError::ConstantReadError(e) => {
                write!(f, "Error reading constant of type {}, ran out of bytes.", e)
            }
            ReadError::OpcodeReadError => {
                write!(f, "Error reading opcode for instruction, ran out of bytes.")
            }
            ReadError::BogusOpcodeReadError(v) => {
                write!(
                    f,
                    "Error reading opcode for instruction, value {:x} is not a valid opcode",
                    v
                )
            }
            ReadError::OperandReadError => {
                write!(
                    f,
                    "Error reading operand for instruction, ran out of bytes."
                )
            }
            ReadError::SymBindReadError => {
                write!(f, "Error reading symbol binding, ran out of bytes.")
            }
            ReadError::UnknownSimBindReadError(v) => {
                write!(
                    f,
                    "Error reading symbol binding, unknown binding with value {:x}",
                    v
                )
            }
            ReadError::SymTypeReadError => {
                write!(f, "Error reading symbol type, ran out of bytes.")
            }
            ReadError::UnknownSimTypeReadError(v) => {
                write!(
                    f,
                    "Error reading symbol type, unknown type with value {:x}",
                    v
                )
            }
            ReadError::KOSymbolConstantReadError(s) => {
                write!(
                    f,
                    "Error reading symbol while parsing constant for {}, ran out of bytes.",
                    s
                )
            }
            ReadError::KOSValueTypeReadError(v) => {
                write!(
                    f,
                    "Error reading kOS value, unknown type with value {:x}",
                    v
                )
            }
            ReadError::KOSValueReadError => {
                write!(f, "Error reading kOS value, ran out of bytes.")
            }
            ReadError::SectionKindReadError => {
                write!(f, "Error reading section kind, ran out of bytes.")
            }
            ReadError::UnknownSectionKindReadError(v) => {
                write!(
                    f,
                    "Error reading section kind, unknown kind with value {:x}",
                    v
                )
            }
            ReadError::SectionHeaderConstantReadError(s) => {
                write!(
                    f,
                    "Error reading section header while parsing constant {}, ran out of bytes.",
                    s
                )
            }
            ReadError::StringTableReadError => {
                write!(f, "Error reading string table, ran out of bytes.")
            }
            ReadError::KOHeaderReadError(s) => {
                write!(
                    f,
                    "Error reading kerbal object file header, while parsing constant {}, ran out of bytes.",
                    s
                )
            }
            ReadError::KSMHeaderReadError(s) => {
                write!(f, "Error reading kerbal machine code file, while parsing constant {}, ran out of bytes.", s)
            }
            ReadError::VersionMismatchError(v) => {
                write!(
                    f,
                    "Error reading KerbalObject file, unsupported KO file version: {}",
                    v
                )
            }
            ReadError::DebugSectionUnsupportedError => {
                write!(
                    f,
                    "Error reading KerbalObject file, debug sections unsupported in this version."
                )
            }
            &ReadError::MissingSectionError(s) => {
                write!(
                    f,
                    "Error reading KerbalObject file, expected section: {}",
                    s
                )
            }
            ReadError::InvalidKOFileMagicError => {
                write!(
                    f,
                    "Error reading KerbalObject file, input file does not appear to be a KO file"
                )
            }
            ReadError::InvalidKSMFileMagicError => {
                write!(f, "Error reading Kerbal Machine Code file, input file does not appear to be a KSM file")
            }
            ReadError::KSMDecompressionError(e) => {
                write!(
                    f,
                    "Error while trying to decompress Kerbal Machine File: {}",
                    e
                )
            }
            ReadError::MissingArgumentSectionError => {
                write!(f, "Error reading Kerbal Machine Code file, expected argument section, ran out of bytes.")
            }
            ReadError::ExpectedArgumentSectionError(v) => {
                write!(f, "Error reading Kerbal Machine Code file, expected argument section, instead found {:x}", v)
            }
            ReadError::NumIndexBytesReadError => {
                write!(f, "Error reading Kerbal Machine Code argument section num index bytes, ran out of bytes.")
            }
            ReadError::InvalidNumIndexBytesError(n) => {
                write!(f, "Error reading Kerbal Machine Code argument section num index bytes, invalid number: {}, only values 1-4 permitted.", n)
            }
            ReadError::ArgumentSectionReadError => {
                write!(f, "Error reading Kerbal Machine Code argument section, expected argument, ran out of bytes.")
            }
            ReadError::CodeTypeReadError => {
                write!(f, "Error reading KSM section type, ran out of bytes")
            }
            ReadError::UnknownCodeTypeReadError(c) => {
                write!(
                    f,
                    "Error reading KSM section type, unknown type with value: {}",
                    c
                )
            }
            ReadError::MissingCodeSectionError => {
                write!(f, "Error reading code section, ran out of bytes")
            }
            ReadError::ExpectedCodeSectionError(b) => {
                write!(
                    f,
                    "Error reading KSM file, expected code section, found {:?}",
                    b
                )
            }
            ReadError::CodeSectionReadError => {
                write!(f, "Error reading code section, ran out of bytes")
            }
            ReadError::DebugRangeReadError => {
                write!(f, "Error reading debug section, ran out of bytes")
            }
            ReadError::DebugEntryReadError => {
                write!(f, "Error reading debug entry, ran out of bytes")
            }
            ReadError::MissingDebugSectionError => {
                write!(f, "Error reading KSM file debug section, expected debug section, ran out of bytes")
            }
            ReadError::ExpectedDebugSectionError(b) => {
                write!(
                    f,
                    "Error reading KSM file, expected debug section header, found {:?}",
                    b
                )
            }
            ReadError::DebugSectionReadError => {
                write!(f, "Error reading KSM file debug section, ran out of bytes")
            }
        }
    }
}

impl std::fmt::Display for ConstantReadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            &ConstantReadError::BoolReadError => {
                write!(f, "boolean")
            }
            &ConstantReadError::U8ReadError => {
                write!(f, "unsigned byte")
            }
            &ConstantReadError::I8ReadError => {
                write!(f, "signed byte")
            }
            &ConstantReadError::U16ReadError => {
                write!(f, "unsigned 16-bit integer")
            }
            &ConstantReadError::I16ReadError => {
                write!(f, "signed 16-bit integer")
            }
            &ConstantReadError::U32ReadError => {
                write!(f, "unsigned 32-bit integer")
            }
            &ConstantReadError::I32ReadError => {
                write!(f, "signed 32-bit integer")
            }
            &ConstantReadError::F32ReadError => {
                write!(f, "float")
            }
            &ConstantReadError::F64ReadError => {
                write!(f, "double float")
            }
            &ConstantReadError::StringReadError => {
                write!(f, "string")
            }
        }
    }
}