|
| 1 | +use rusty_mujoco::{obj, ObjectId}; |
| 2 | + |
| 3 | +pub enum Error { |
| 4 | + Mujoco(::rusty_mujoco::MjError), |
| 5 | + Mjs(String), |
| 6 | + NameNotFound(&'static str), |
| 7 | + PhysicsDiverged, |
| 8 | + JointTypeNotMatch { |
| 9 | + expected: ::rusty_mujoco::bindgen::mjtJoint, |
| 10 | + found: ::rusty_mujoco::bindgen::mjtJoint, |
| 11 | + }, |
| 12 | + ActuatorStateless(ObjectId<obj::Actuator>), |
| 13 | + PluginStateless(ObjectId<obj::Plugin>), |
| 14 | + BodyNotMocap(ObjectId<obj::Body>), |
| 15 | +} |
| 16 | + |
| 17 | +impl From<::rusty_mujoco::MjError> for Error { |
| 18 | + fn from(e: ::rusty_mujoco::MjError) -> Self { |
| 19 | + Error::Mujoco(e) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl std::fmt::Debug for Error { |
| 24 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 25 | + match self { |
| 26 | + Error::Mujoco(e) => write!(f, "Error::MuJoCo({e:?})"), |
| 27 | + Error::Mjs(msg) => write!(f, "Error::Mjs({msg})"), |
| 28 | + Error::NameNotFound(name) => write!(f, "Error::NameNotFound({name})"), |
| 29 | + Error::PhysicsDiverged => write!(f, "Error::PhysicsDiverged"), |
| 30 | + Error::JointTypeNotMatch { expected, found } => { |
| 31 | + write!(f, "Error::JointTypeNotMatch(expected: {expected:?}, found: {found:?})") |
| 32 | + } |
| 33 | + Error::ActuatorStateless(actuator_id) => { |
| 34 | + write!(f, "Error::ActuatorStateless({actuator_id:?})") |
| 35 | + } |
| 36 | + Error::PluginStateless(plugin_id) => { |
| 37 | + write!(f, "Error::PluginStateless({plugin_id:?})") |
| 38 | + } |
| 39 | + Error::BodyNotMocap(body_id) => { |
| 40 | + write!(f, "Error::BodyNotMocap({body_id:?})") |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl std::fmt::Display for Error { |
| 47 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 48 | + match self { |
| 49 | + Error::Mujoco(e) => write!(f, "MuJoCo error: {e}"), |
| 50 | + Error::Mjs(msg) => write!(f, "MuJoCo error: {msg}"), |
| 51 | + Error::NameNotFound(name) => write!(f, "Given name not found: `{name}`"), |
| 52 | + Error::PhysicsDiverged => write!(f, "Physics simulation diverged"), |
| 53 | + Error::JointTypeNotMatch { expected, found } => { |
| 54 | + write!(f, "Joint type mismatch: expected {expected:?}, found {found:?}") |
| 55 | + } |
| 56 | + Error::ActuatorStateless(actuator_id) => { |
| 57 | + write!(f, "Actuator with ID {actuator_id:?} is stateless unexpectedly") |
| 58 | + } |
| 59 | + Error::PluginStateless(plugin_id) => { |
| 60 | + write!(f, "Plugin with ID {plugin_id:?} is stateless unexpectedly") |
| 61 | + } |
| 62 | + Error::BodyNotMocap(body_id) => { |
| 63 | + write!(f, "Body with ID {body_id:?} is not a mocap body") |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl std::error::Error for Error { |
| 70 | + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 71 | + match self { |
| 72 | + Error::Mujoco(e) => Some(e), |
| 73 | + Error::Mjs(_) => None, |
| 74 | + Error::NameNotFound(_) => None, |
| 75 | + Error::PhysicsDiverged => None, |
| 76 | + Error::JointTypeNotMatch { .. } => None, |
| 77 | + Error::ActuatorStateless(_) => None, |
| 78 | + Error::PluginStateless(_) => None, |
| 79 | + Error::BodyNotMocap(_) => None, |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments