odem-rs 0.3.0

Object-based Discrete-Event Modelling in Rust using async/await
Documentation
//! This module contains the definition of the combined error-type of the
//! simulation library.

/// Type-alias for the general result-type for functions and structures of the
/// simulation library.
pub type SimResult<Ok = ()> = Result<Ok, Error>;

/* ******************************************************** Simulation Errors */

/// General error-type for the functions and structures contained in the
/// simulation library.
#[derive(Copy, Clone, Debug, thiserror::Error)]
pub enum Error {
	/// Variant for errors specific to [`odem_rs_core`].
	#[error(transparent)]
	Base(#[from] odem_rs_core::error::Error),
	/// Variant for errors specific to [`odem_rs_sync`].
	#[error(transparent)]
	Sync(#[from] odem_rs_sync::error::Error),
	/// Variant for errors specific to [`odem_rs_util`].
	#[error(transparent)]
	Util(#[from] odem_rs_util::error::Error),
}

/* ********************************************************* Error Conversion */

/// Helper-macro to generate error conversion routines for the error types
/// contained within the submodules.
macro_rules! derive_error_conversion {
	(@plain ($($T:tt)*) $E:ident) => {
		#[automatically_derived]
		impl From<$($T)*::$E> for Error {
			#[inline]
			fn from(err: $($T)*::$E) -> Self {
				$($T)*::Error::from(err).into()
			}
		}
	};

	(@generic ($($T:tt)*) $E:ident <$($P:ident)*>) => {
		#[automatically_derived]
		impl<$($P),*> From<$($T)*::$E<$($P),*>> for Error {
			#[inline]
			fn from(err: $($T)*::$E<$($P),*>) -> Self {
				$($T)*::Error::from(err).into()
			}
		}
	};

	(@analyze $T:tt) => {};

	(@analyze $T:tt $E:ident <$($P:ident)*> $(, $($R:tt)*)?) => {
		derive_error_conversion!(@generic $T $E <$($P),*>);
		derive_error_conversion!(@analyze $T $($($R)*)*);
	};

	(@analyze $T:tt $E:ident $(, $($R:tt)*)?) => {
		derive_error_conversion!(@plain $T $E);
		derive_error_conversion!(@analyze $T $($($R)*)*);
	};

	($($T:ident)::+ => $($R:tt)*) => {
		derive_error_conversion!(@analyze ($($T)::*) $($R)*);
	};
}

/* ************************************************************** Core Errors */

derive_error_conversion!(
	odem_rs_core::error => Deadlock<T>, NotBorn, NotIdle, NotBusy, NotNext,
		NotDone, NotGone, NotInit, NotTerm
);

/* ************************************************************** Sync Errors */

derive_error_conversion!(
	odem_rs_sync::error => SendError<T>, TrySendError<T>, RecvError,
		TryRecvError, FacilityOccupied
);

/* ************************************************************** Stat Errors */

derive_error_conversion!(
	odem_rs_util::error => Causality<T>, NegativeWeight<W>, InsufficientSpace<B>
);