rsbullet_core/
error.rs

1use std::ffi::NulError;
2use std::fmt::{Display, Formatter};
3
4/// Represents failures that can occur while interacting with the Bullet physics server.
5#[derive(Debug)]
6pub enum BulletError {
7    /// Attempted to connect but Bullet returned a null pointer.
8    NullPointer(&'static str),
9    /// Bullet reported that the physics server is not responsive.
10    ServerUnavailable(&'static str),
11    /// The command completed with an unexpected status code.
12    UnexpectedStatus {
13        expected: i32,
14        actual: i32,
15    },
16    /// A low-level FFI command returned an error code.
17    CommandFailed {
18        message: &'static str,
19        code: i32,
20    },
21    UnknownType(&'static str),
22    /// Converting Rust strings into C strings failed.
23    CString(NulError),
24}
25
26impl Display for BulletError {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        match self {
29            BulletError::NullPointer(msg) => write!(f, "{msg}"),
30            BulletError::ServerUnavailable(msg) => write!(f, "{msg}"),
31            BulletError::UnexpectedStatus { expected, actual } => write!(
32                f,
33                "Unexpected Bullet status. expected={expected} actual={actual}"
34            ),
35            BulletError::CommandFailed { message, code } => {
36                write!(f, "{message} (code={code})")
37            }
38            BulletError::UnknownType(msg) => write!(f, "{msg}"),
39            BulletError::CString(err) => err.fmt(f),
40        }
41    }
42}
43
44impl std::error::Error for BulletError {}
45
46impl From<NulError> for BulletError {
47    fn from(value: NulError) -> Self {
48        BulletError::CString(value)
49    }
50}
51
52pub type BulletResult<T> = Result<T, BulletError>;