use lgwks_macros::Error;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MotionErrorCode {
EasingDomainError,
InterpolatorNaN,
InterpolatorPanic,
NodeNotFound,
PatchPathImmutable,
PatchPathUnknown,
PatchInvalid,
SpringUnstable,
ClockBackwards,
GraphVersionConflict,
}
impl core::fmt::Display for MotionErrorCode {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let code = match *self {
Self::EasingDomainError => "EASING_DOMAIN_ERROR",
Self::InterpolatorNaN => "INTERPOLATOR_NAN",
Self::InterpolatorPanic => "INTERPOLATOR_PANIC",
Self::NodeNotFound => "NODE_NOT_FOUND",
Self::PatchPathImmutable => "MOTION_PATCH_PATH_IMMUTABLE",
Self::PatchPathUnknown => "MOTION_PATCH_PATH_UNKNOWN",
Self::PatchInvalid => "MOTION_PATCH_INVALID",
Self::SpringUnstable => "SPRING_UNSTABLE",
Self::ClockBackwards => "CLOCK_BACKWARDS",
Self::GraphVersionConflict => "GRAPH_VERSION_CONFLICT",
};
formatter.write_str(code)
}
}
#[derive(Clone, Debug, Error, PartialEq)]
#[non_exhaustive]
#[error("{code}: {detail}")]
pub struct MotionError {
code: MotionErrorCode,
detail: String,
}
impl MotionError {
#[must_use]
pub fn new(code: MotionErrorCode) -> Self {
Self {
code,
detail: String::new(),
}
}
#[must_use]
pub fn with_detail(mut self, detail: &str) -> Self {
self.detail = String::from(detail);
self
}
#[must_use]
pub const fn code(&self) -> MotionErrorCode {
self.code
}
#[must_use]
pub fn detail(&self) -> &str {
&self.detail
}
pub(crate) fn require(valid: bool, code: MotionErrorCode, detail: &str) -> Result<(), Self> {
if valid {
Ok(())
} else {
Err(Self::new(code).with_detail(detail))
}
}
}