made-core 0.8.0

Domain core of MADE: entities, value objects, events, ports. No IO.
Documentation
use serde::{Deserialize, Serialize};

use super::{CeremonyStateKind, StateExecution, StateId, StateRepeatPolicy};
use crate::value_objects::Attributes;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CeremonyState {
    id: StateId,
    kind: CeremonyStateKind,
    #[serde(default, skip_serializing_if = "StateExecution::is_sequential")]
    execution: StateExecution,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    repeat: Option<StateRepeatPolicy>,
    #[serde(default, skip_serializing_if = "Attributes::is_empty")]
    annotations: Attributes,
}

impl CeremonyState {
    #[must_use]
    pub fn new(id: StateId, kind: CeremonyStateKind) -> Self {
        Self {
            id,
            kind,
            execution: StateExecution::Sequential,
            repeat: None,
            annotations: Attributes::empty(),
        }
    }

    #[must_use]
    pub fn initial(id: StateId) -> Self {
        Self::new(id, CeremonyStateKind::Initial)
    }

    #[must_use]
    pub fn intermediate(id: StateId) -> Self {
        Self::new(id, CeremonyStateKind::Intermediate)
    }

    #[must_use]
    pub fn terminal(id: StateId) -> Self {
        Self::new(id, CeremonyStateKind::Terminal)
    }

    #[must_use]
    pub fn id(&self) -> &StateId {
        &self.id
    }

    #[must_use]
    pub fn kind(&self) -> CeremonyStateKind {
        self.kind
    }

    #[must_use]
    pub fn with_execution(mut self, execution: StateExecution) -> Self {
        self.execution = execution;
        self
    }

    #[must_use]
    pub fn execution(&self) -> StateExecution {
        self.execution
    }

    #[must_use]
    pub fn with_repeat_policy(mut self, repeat: StateRepeatPolicy) -> Self {
        self.repeat = Some(repeat);
        self
    }

    #[must_use]
    pub fn repeat_policy(&self) -> Option<&StateRepeatPolicy> {
        self.repeat.as_ref()
    }

    /// Opaque authoring and rendering metadata. The state machine never
    /// interprets these values when deciding or folding commands.
    #[must_use]
    pub fn with_annotations(mut self, annotations: Attributes) -> Self {
        self.annotations = annotations;
        self
    }

    #[must_use]
    pub fn annotations(&self) -> &Attributes {
        &self.annotations
    }

    #[must_use]
    pub fn is_initial(&self) -> bool {
        self.kind.is_initial()
    }

    #[must_use]
    pub fn is_terminal(&self) -> bool {
        self.kind.is_terminal()
    }
}