Skip to main content

made_core/value_objects/ceremony/
ceremony_context.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::error::DomainError;
5use crate::value_objects::Attributes;
6
7use super::GuardName;
8
9#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(transparent)]
11pub struct CeremonyContext(Attributes);
12
13impl CeremonyContext {
14    #[must_use]
15    pub fn new(attributes: Attributes) -> Self {
16        Self(attributes)
17    }
18
19    #[must_use]
20    pub fn empty() -> Self {
21        Self::default()
22    }
23
24    pub fn with_guard_approval(self, guard_name: &GuardName) -> Result<Self, DomainError> {
25        let mut entries = self.0.into_inner();
26        entries.insert(guard_name.as_str().to_owned(), Value::Bool(true));
27        Ok(Self(Attributes::new(entries)?))
28    }
29
30    #[must_use]
31    pub fn is_guard_approved(&self, guard_name: &GuardName) -> bool {
32        self.0
33            .get(guard_name.as_str())
34            .and_then(Value::as_bool)
35            .unwrap_or(false)
36    }
37
38    #[must_use]
39    pub fn attributes(&self) -> &Attributes {
40        &self.0
41    }
42}