odem-rs-core 0.3.0

Core components of the odem-rs simulation framework
Documentation
//! A module for core-specific error types.

pub use crate::continuation::error::*;

/* ************************************************************** Error Types */

/// Enumeration of errors for the functions and structures contained in this
/// module of the simulation library.
#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum Error {
	/// The simulation stopped when the simulator detected a deadlock.
	#[error("deadlock detected")]
	Deadlock,
	/// Continuation is already bound.
	#[error(transparent)]
	NotBorn(#[from] NotBorn),
	/// Continuation is not idle.
	#[error(transparent)]
	NotIdle(#[from] NotIdle),
	/// Continuation is not active.
	#[error(transparent)]
	NotBusy(#[from] NotBusy),
	/// Continuation is not scheduled.
	#[error(transparent)]
	NotNext(#[from] NotNext),
	/// Continuation hasn't completed yet.
	#[error(transparent)]
	NotDone(#[from] NotDone),
	/// Continuation hasn't been released yet.
	#[error(transparent)]
	NotGone(#[from] NotGone),
	/// Continuation hasn't been bound yet.
	#[error(transparent)]
	NotInit(#[from] NotInit),
	/// Continuation hasn't terminated.
	#[error(transparent)]
	NotTerm(#[from] NotTerm),
	/// Signals an attempt to schedule a continuation at an earlier model-time.
	#[error("causality error")]
	Causality,
}

/* ******************************************************** Minor Error Types */

/// Signals the detection of a deadlock during a simulation run.
#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub struct Deadlock<T>(pub T);

impl<T> From<Deadlock<T>> for Error {
	fn from(_: Deadlock<T>) -> Self {
		Error::Deadlock
	}
}

impl<T: crate::config::Time> core::fmt::Display for Deadlock<T> {
	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
		write!(f, "deadlock detected at {}", self.0.display())
	}
}

/// Signals that an event at the current model time caused a change in that
/// event's past, violating causality.
#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error(
	"continuation was scheduled at model-time {cause:?} (cause) for model-time \
         {effect:?} (effect), violating causality"
)]
pub struct CausalityError<T> {
	/// Source time of the caller.
	pub cause: T,
	/// Target model-time of the caller.
	pub effect: T,
}

impl<T> From<CausalityError<T>> for Error {
	fn from(_: CausalityError<T>) -> Self {
		Error::Causality
	}
}