1use std::ffi::NulError;
2use std::fmt::{Display, Formatter};
3
4#[derive(Debug)]
6pub enum BulletError {
7 NullPointer(&'static str),
9 ServerUnavailable(&'static str),
11 UnexpectedStatus {
13 expected: i32,
14 actual: i32,
15 },
16 CommandFailed {
18 message: &'static str,
19 code: i32,
20 },
21 UnknownType(&'static str),
22 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>;