Skip to main content

saturnus_core/
error.rs

1//! Errors returned by the core library.
2
3use std::fmt;
4
5/// Something the caller handed the library was unusable.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Error {
8    /// The ROM image has the wrong size for the model.
9    RomSize {
10        /// Size the model needs, in bytes.
11        expected: usize,
12        /// Size that was given, in bytes.
13        actual: usize,
14    },
15    /// A card image is not a power of two between the minimum card size
16    /// and the port's maximum.
17    CardSize {
18        /// Size that was given, in bytes.
19        actual: usize,
20        /// Largest card the port takes, in bytes.
21        max: usize,
22    },
23    /// The model has no card port with this number.
24    NoSuchPort {
25        /// The port number.
26        port: u8,
27    },
28    /// No model has this name (see [`Model`](crate::Model)'s `FromStr`).
29    UnknownModel {
30        /// The name that was given.
31        name: String,
32    },
33    /// A saved state could not be parsed.
34    InvalidState {
35        /// What was wrong.
36        reason: &'static str,
37        /// Byte offset in the state where the problem was found.
38        offset: usize,
39    },
40    /// A saved state belongs to another model.
41    StateModelMismatch,
42    /// A saved state was made with a different system ROM.
43    StateRomMismatch,
44    /// A key the model's keyboard does not have (e.g. `prg` on the 49G).
45    KeyNotOnModel {
46        /// The key's script name.
47        key: &'static str,
48        /// Model name.
49        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 {}