Skip to main content

rustynes_core/
host_warning.rs

1//! v3.5.0 (`FE-06`) — the host-facing warning codes every frontend shares.
2//!
3//! A [`HostWarning`] is something a host (the desktop, the browser, the
4//! Android and iOS apps, a downstream embedder) should tell the player, as a
5//! stable machine-readable code rather than a pre-composed sentence, so each
6//! host can render it in the player's language. [`HostWarning::message`] is
7//! the canonical English text for a host that does not localise.
8//!
9//! The enum was introduced in the mobile bridge (v2.0.3, `rustynes-mobile`),
10//! which exports it to Kotlin and Swift. Until v3.5.0 it lived there, and the
11//! desktop and browser frontends, which cannot depend on the `UniFFI` bridge,
12//! were said to stay "in parity" by carrying identical English literals of
13//! their own. They carried none: neither warning has a desktop or browser
14//! producer (see below), so there was nothing to keep in parity, only a
15//! second place a future warning's text would have had to be copied to. The
16//! definition now lives here, at the surface every host already depends on;
17//! the bridge re-exports this type to Kotlin / Swift unchanged (a `UniFFI`
18//! remote enum; the generated bindings differ only in doc comments), and a desktop or browser
19//! warning added later takes its code and its English from this one place.
20//!
21//! **Which hosts produce which code today:**
22//!
23//! | code | mobile | desktop / browser |
24//! |---|---|---|
25//! | [`HostWarning::PreTimebaseMovie`] | no longer (v2.9.8: older movies are refused) | never |
26//! | [`HostWarning::RecoveredFromInternalError`] | the bridge's poisoned-lock recovery | never: release builds abort on panic (`panic = "abort"`), so there is no lock left to recover |
27//!
28//! Grow it by adding variants (each with its own `message` arm); never
29//! repurpose one, so a host's per-code localisation table cannot silently
30//! change meaning under it. The bridge's remote declaration must list the
31//! same variants, which the compiler enforces.
32
33use alloc::string::String;
34
35/// A machine-readable code for a host-facing warning. See the module docs.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37pub enum HostWarning {
38    /// A `.rnm` recorded on a pre-v2.0.0 "Timebase" build was loaded: its
39    /// recorded input replays faithfully, but exact framebuffer / audio
40    /// reproduction is not guaranteed across the engine-timebase boundary
41    /// (ADR 0028).
42    ///
43    /// **No longer produced (v2.9.8).** `.rnm` format 3 records the emulation
44    /// options and every older movie is refused with an error that says to
45    /// re-record it, so a pre-v2.0.0 movie never reaches playback. The variant
46    /// stays so hosts' localisation tables keep their meaning.
47    PreTimebaseMovie,
48    /// v2.7.4 (frontend audit MOB-07) — a call panicked inside the mobile
49    /// bridge. The panic was contained (the mobile libraries unwind), but it
50    /// may have left the emulator mid-update, so any netplay session was ended
51    /// and the machine is frozen: the app stays open, the picture holds, and
52    /// no further emulated cycle runs until the player reopens the game, power
53    /// cycles, or loads a save state.
54    RecoveredFromInternalError,
55}
56
57impl HostWarning {
58    /// Every code, in declaration order (for hosts building a localisation
59    /// table, and for the tests that pin each message).
60    pub const ALL: [Self; 2] = [Self::PreTimebaseMovie, Self::RecoveredFromInternalError];
61
62    /// The canonical English text for this code.
63    ///
64    /// Byte-identical to what the mobile bridge queued before v2.0.3 (and has
65    /// returned from its legacy `drain_warnings` since), so a host that does
66    /// not localise observes no change.
67    #[must_use]
68    pub fn message(&self) -> String {
69        match self {
70            Self::PreTimebaseMovie => "this movie was recorded on a pre-v2.0.0 build -- \
71                 input replay proceeds, but exact framebuffer/audio reproduction is not \
72                 guaranteed across the engine-timebase boundary (see ADR 0028)"
73                .into(),
74            Self::RecoveredFromInternalError => "RustyNES stopped the game after an internal \
75                 error. Any netplay session was ended. Reopen the game or load a save \
76                 state to continue."
77                .into(),
78        }
79    }
80}
81
82impl core::fmt::Display for HostWarning {
83    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
84        f.write_str(&self.message())
85    }
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91    use alloc::format;
92
93    /// The English text is part of the contract (a non-localising host shows
94    /// it verbatim), so each one is pinned in full, as written before the
95    /// enum moved here from the mobile bridge.
96    #[test]
97    fn each_code_keeps_its_english_text() {
98        assert_eq!(
99            HostWarning::PreTimebaseMovie.message(),
100            "this movie was recorded on a pre-v2.0.0 build -- input replay proceeds, but exact \
101             framebuffer/audio reproduction is not guaranteed across the engine-timebase \
102             boundary (see ADR 0028)"
103        );
104        assert_eq!(
105            HostWarning::RecoveredFromInternalError.message(),
106            "RustyNES stopped the game after an internal error. Any netplay session was ended. \
107             Reopen the game or load a save state to continue."
108        );
109        for w in HostWarning::ALL {
110            assert_eq!(format!("{w}"), w.message(), "Display is the message");
111        }
112    }
113}