Skip to main content

ssruntime/
error.rs

1//! The runtime's error type and its stable integer codes.
2//!
3//! [`SSRuntimeError::as_code`] maps each variant to a small integer that is part of the
4//! C ABI: across FFI a call reports failure by return value and the caller retrieves the
5//! code with `ss_runtime_get_last_error`. **These numbers are a compatibility surface** —
6//! existing variants keep their codes, and new ones take fresh numbers rather than
7//! reusing a retired one.
8//!
9//! [`SSRuntimeError::as_str`] is the other half, reached across FFI as
10//! `ss_runtime_error_message(code)`. A code tells a Player's *code* what happened; the
11//! text is what it shows its user, so it is written to be read by someone holding a
12//! `.sspj` rather than a debugger. Keep a new variant's wording in that register.
13//!
14//! **The text carries no payload.** `InvalidVersion` knows two version numbers, but a
15//! Player that wants them should read `ss_runtime_ssab_schema_version` and the pack's own
16//! `version` field — two integers it can act on, rather than a sentence it would have to
17//! parse. [`core::fmt::Display`] does append them, for Rust callers and the wasm face.
18
19/// Everything that can go wrong loading or playing back an animation.
20#[derive(Debug, Clone, PartialEq)]
21pub enum SSRuntimeError {
22    /// A resource whose pack did not parse was bound to a context or an effect slot.
23    /// Neither the C nor the web face hands out such a resource, so nothing reports
24    /// this today. Empty or truncated bytes are not caught here: loading does not
25    /// validate the `.ssab`.
26    EmptyData,
27    /// SSAB version mismatch.
28    InvalidVersion { expected: u32, got: u32 },
29    /// Animation not found.
30    AnimationNotFound,
31    /// Data is not loaded.
32    NotLoaded,
33    /// Invalid parameter (e.g. frame number out of range).
34    InvalidParameter,
35    /// Reserved: an allocation failure aborts rather than returning this.
36    MemoryError,
37    /// Effect data is malformed (e.g. missing required fields).
38    MalformedEffect,
39    /// Other/unknown error (reserved for future extension).
40    Unknown,
41}
42
43impl SSRuntimeError {
44    /// The stable integer code reported across the C ABI. See the module docs.
45    pub fn as_code(&self) -> i32 {
46        match self {
47            SSRuntimeError::EmptyData => 1,
48            SSRuntimeError::InvalidVersion { .. } => 2,
49            SSRuntimeError::AnimationNotFound => 3,
50            SSRuntimeError::NotLoaded => 4,
51            SSRuntimeError::InvalidParameter => 5,
52            SSRuntimeError::MemoryError => 6,
53            SSRuntimeError::MalformedEffect => 7,
54            SSRuntimeError::Unknown => 99,
55        }
56    }
57}
58
59impl SSRuntimeError {
60    /// The wording, without the payload any variant carries.
61    ///
62    /// Static, so it crosses the C ABI as a plain pointer with no context to
63    /// borrow from and no lifetime to respect — which is what
64    /// `ss_runtime_error_message` hands out. [`core::fmt::Display`] builds on
65    /// this rather than repeating it, so there is one wording per variant.
66    pub fn as_str(&self) -> &'static str {
67        match self {
68            SSRuntimeError::EmptyData => "SSAB data is empty",
69            SSRuntimeError::InvalidVersion { .. } => "Invalid SSAB version",
70            SSRuntimeError::AnimationNotFound => "Animation not found",
71            SSRuntimeError::NotLoaded => "SSAB data is not loaded",
72            SSRuntimeError::InvalidParameter => "Invalid parameter",
73            SSRuntimeError::MemoryError => "Memory allocation failed",
74            SSRuntimeError::MalformedEffect => "Effect data is malformed",
75            SSRuntimeError::Unknown => "Unknown error occurred",
76        }
77    }
78}
79
80impl core::fmt::Display for SSRuntimeError {
81    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
82        match self {
83            SSRuntimeError::InvalidVersion { expected, got } => {
84                write!(f, "{}. expected: {}, got: {}", self.as_str(), expected, got)
85            }
86            _ => f.write_str(self.as_str()),
87        }
88    }
89}
90
91#[cfg(feature = "std")]
92impl std::error::Error for SSRuntimeError {}
93
94#[cfg(not(feature = "std"))]
95impl core::error::Error for SSRuntimeError {}