use core::marker::{ConstParamTy, PhantomData};
#[derive(ConstParamTy, Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum ObjectLifecyclePhase {
Created,
Active,
Modified,
Archived,
Deleted,
}
impl core::fmt::Display for ObjectLifecyclePhase {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Created => write!(f, "created"),
Self::Active => write!(f, "active"),
Self::Modified => write!(f, "modified"),
Self::Archived => write!(f, "archived"),
Self::Deleted => write!(f, "deleted"),
}
}
}
pub struct ObjectState<const PHASE: ObjectLifecyclePhase> {
_private: (),
}
pub struct LifecycleTransition<const FROM: ObjectLifecyclePhase, const TO: ObjectLifecyclePhase> {
_private: (),
}
pub struct ObjectLifecycleWitness;
pub struct LifecycledObject<T, const PHASE: ObjectLifecyclePhase> {
pub inner: T,
_state: PhantomData<ObjectState<PHASE>>,
}
impl<T, const PHASE: ObjectLifecyclePhase> LifecycledObject<T, PHASE> {
pub fn new(inner: T) -> Self {
Self {
inner,
_state: PhantomData,
}
}
}
impl<T> LifecycledObject<T, { ObjectLifecyclePhase::Created }> {
pub fn activate(self) -> LifecycledObject<T, { ObjectLifecyclePhase::Active }> {
LifecycledObject {
inner: self.inner,
_state: PhantomData,
}
}
}
impl<T> LifecycledObject<T, { ObjectLifecyclePhase::Active }> {
pub fn modify(self) -> LifecycledObject<T, { ObjectLifecyclePhase::Modified }> {
LifecycledObject {
inner: self.inner,
_state: PhantomData,
}
}
pub fn archive(self) -> LifecycledObject<T, { ObjectLifecyclePhase::Archived }> {
LifecycledObject {
inner: self.inner,
_state: PhantomData,
}
}
}
impl<T> LifecycledObject<T, { ObjectLifecyclePhase::Modified }> {
pub fn archive(self) -> LifecycledObject<T, { ObjectLifecyclePhase::Archived }> {
LifecycledObject {
inner: self.inner,
_state: PhantomData,
}
}
pub fn modify(self) -> LifecycledObject<T, { ObjectLifecyclePhase::Modified }> {
LifecycledObject {
inner: self.inner,
_state: PhantomData,
}
}
}
impl<T> LifecycledObject<T, { ObjectLifecyclePhase::Archived }> {
pub fn delete(self) -> LifecycledObject<T, { ObjectLifecyclePhase::Deleted }> {
LifecycledObject {
inner: self.inner,
_state: PhantomData,
}
}
}
pub type CreatedObject<T> = LifecycledObject<T, { ObjectLifecyclePhase::Created }>;
pub type ActiveObject<T> = LifecycledObject<T, { ObjectLifecyclePhase::Active }>;
pub type ModifiedObject<T> = LifecycledObject<T, { ObjectLifecyclePhase::Modified }>;
pub type ArchivedObject<T> = LifecycledObject<T, { ObjectLifecyclePhase::Archived }>;
pub type DeletedObject<T> = LifecycledObject<T, { ObjectLifecyclePhase::Deleted }>;