made_core/value_objects/ceremony/
ceremony_context.rs1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::error::DomainError;
5use crate::value_objects::Attributes;
6
7use super::{ContextKey, ContextPatch, 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 pub fn with_patch(self, patch: &ContextPatch) -> Result<Self, DomainError> {
31 let mut entries = self.0.into_inner();
32 for (key, value) in patch.entries() {
33 entries.insert(key.as_str().to_owned(), value.clone());
34 }
35 Ok(Self(Attributes::new(entries)?))
36 }
37
38 #[must_use]
39 pub fn get(&self, key: &ContextKey) -> Option<&Value> {
40 self.0.get(key.as_str())
41 }
42
43 #[must_use]
44 pub fn is_guard_approved(&self, guard_name: &GuardName) -> bool {
45 self.0
46 .get(guard_name.as_str())
47 .and_then(Value::as_bool)
48 .unwrap_or(false)
49 }
50
51 #[must_use]
52 pub fn attributes(&self) -> &Attributes {
53 &self.0
54 }
55}