1use std::fmt;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Error {
8 RomSize {
10 expected: usize,
12 actual: usize,
14 },
15 CardSize {
18 actual: usize,
20 max: usize,
22 },
23 NoSuchPort {
25 port: u8,
27 },
28 UnknownModel {
30 name: String,
32 },
33 InvalidState {
35 reason: &'static str,
37 offset: usize,
39 },
40 StateModelMismatch,
42 StateRomMismatch,
44 KeyNotOnModel {
46 key: &'static str,
48 model: &'static str,
50 },
51}
52
53impl fmt::Display for Error {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 match self {
56 Error::RomSize { expected, actual } => write!(
57 f,
58 "ROM image is {actual} bytes, this model needs {expected} bytes"
59 ),
60 Error::CardSize { actual, max } => write!(
61 f,
62 "card image is {actual} bytes, cards for this port must be a power of two from 1 KB to {} KB",
63 max / 1024
64 ),
65 Error::NoSuchPort { port } => write!(f, "this model has no card port {port}"),
66 Error::UnknownModel { name } => {
67 let names: Vec<&str> = crate::Model::ALL.iter().map(|m| m.name()).collect();
68 write!(
69 f,
70 "unknown model {name:?}; expected one of {}",
71 names.join(", ")
72 )
73 }
74 Error::InvalidState { reason, offset } => {
75 write!(f, "invalid saved state at byte {offset}: {reason}")
76 }
77 Error::StateModelMismatch => write!(f, "saved state is for another model"),
78 Error::StateRomMismatch => write!(f, "saved state was made with a different ROM"),
79 Error::KeyNotOnModel { key, model } => {
80 write!(f, "key \"{key}\" is not on the {model} keyboard")
81 }
82 }
83 }
84}
85
86impl std::error::Error for Error {}